- This topic has 17 replies, 3 voices, and was last updated 5 years, 10 months ago by Tom.
-
AuthorPosts
-
May 9, 2018 at 4:34 am #571308George
Ok, this thing is doing my head in.
I currently have a custom post type category archive as my homepage by essentially setting a redirect from an “empty” home page that I have created on the backend to the actual archive’s slug. It works fine but I am getting warnings from Google page Speed that this is not a good practice and that it could cause unnecessary delays on the homepage loading time. At the same time, I like the Load More functionality that I have on the blog layout and the fact that I can also search so I don’t want to use WP Show Posts just to display the posts.
I tried this plugin Display Posts Shortcode plugin but I think it’s just using it’s own template to list the blog items. Is there a shortcode I could use that would just list the category archive as it is currently set in the Customizer settings?
May 9, 2018 at 8:03 am #571536DavidStaffCustomer SupportHi George, Tom may have a snippet to fix this, there is this plugin that may be worth a try.
May 9, 2018 at 8:23 am #571562GeorgeThanks David but with this plugin functionality, I lose the blog page, comes out blank.
May 9, 2018 at 8:27 am #571567DavidStaffCustomer SupportOK scrap that then! lol I think it might be best to get Tom’s input
May 9, 2018 at 8:30 pm #571986TomLead DeveloperLead DeveloperSo what if we had a function which would make it so your posts page (Settings > Reading) only displayed posts from that one custom post type? Would that work for you?
May 10, 2018 at 1:05 am #572098GeorgeEh, no because the blog page already displays the normal posts.
May 10, 2018 at 6:00 am #572298GeorgeOk Tom, I think I’ve sorted that. I found some code online that loads a custom post archive on the homepage:
/** * Load custom post type archive on home page * * Reference: http://www.wpaustralia.org/wordpress-forums/topic/pre_get_posts-and-is_front_page/ * Reference: http://wordpress.stackexchange.com/questions/30851/how-to-use-a-custom-post-type-archive-as-front-page */ function prefix_downloads_front_page( $query ) { // Only filter the main query on the front-end if ( is_admin() || ! $query->is_main_query() ) { return; } global $wp; $front = false; // If the latest posts are showing on the home page if ( ( is_home() && empty( $wp->query_string ) ) ) { $front = true; } // If a static page is set as the home page if ( ( $query->get( 'page_id' ) == get_option( 'page_on_front' ) && get_option( 'page_on_front' ) ) || empty( $wp->query_string ) ) { $front = true; } if ( $front ) : $query->set( 'post_type', 'video' ); $query->set( 'page_id', '' ); // Set properties to match an archive $query->is_page = 0; $query->is_singular = 0; $query->is_post_type_archive = 1; $query->is_archive = 1; endif; } add_action( 'pre_get_posts', 'prefix_downloads_front_page' );
I just had to apply the style for the category to the archive but it seems to be working.
Thanks!
May 10, 2018 at 9:10 am #572530TomLead DeveloperLead DeveloperAwesome 🙂
May 20, 2018 at 9:37 am #580174GeorgeHi Tom, sorry for opening this up again. Can you plese have a look at the above code I provided and tell me why the Load More keeps loading the same group of items every time? The code is supposed to load the video custom post category archive on the homepage so you will only be able to see the issue when you hit the logo at the top and then clicking the Load More at the bottom.
May 20, 2018 at 9:46 pm #580454TomLead DeveloperLead DeveloperHmm, what happens if you go to yourblog.com/page/2? Does it load the same posts as the first page?
May 21, 2018 at 3:30 am #580577GeorgeYes, it loads the same posts as the first page:
https://bit.ly/2kacN3gMay 21, 2018 at 10:18 am #580947TomLead DeveloperLead DeveloperThen there’s definitely something broken with your function.
What if you just did this?:
function prefix_downloads_front_page( $query ) { // Only filter the main query on the front-end if ( is_admin() || ! $query->is_main_query() ) { return; } if ( is_home() ) { $query->set( 'post_type', 'video' ); } } add_action( 'pre_get_posts', 'prefix_downloads_front_page' );
May 21, 2018 at 1:36 pm #581114GeorgeIt loads an empty page(which the Video page is anyway). With the previous function at least the video custom post type category archive was loaded on the page when clicking the logo homepage. I think in my case I need to use
is_front_page()
instead ofis_home()
(don’t want it to load it on the blog home page)but it doesn’t work either.May 21, 2018 at 9:01 pm #581334TomLead DeveloperLead DeveloperAh, so the front page isn’t set as the posts page. In that case pagination probably isn’t set up.
I found this page: https://github.com/10up/ElasticPress/issues/222
Perhaps try adding this part to your function?:
//Fix pagination if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } $query->set( 'paged', $paged );
May 22, 2018 at 3:30 am #581508GeorgeOh that was sweet, Tom! Works fine now man. Thanks again!
-
AuthorPosts
- You must be logged in to reply to this topic.