- This topic has 11 replies, 2 voices, and was last updated 4 years, 4 months ago by Leo.
-
AuthorPosts
-
November 10, 2019 at 9:32 am #1058760Aaron
Hello,
How can I set the home page (index) of my blog to full content but change archive, categories, and search results to display only an excerpt (or even just links)?
Thanks!
November 10, 2019 at 10:04 am #1058781LeoStaffCustomer SupportHi there,
Set the customizer option to show excerpt, then add this PHP snippet to make the front page show full post:
add_filter( 'generate_show_excerpt', function( $show ) { if ( is_front_page() && is_home() ) { $show = false; } return $show; } );
Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know if this helps 🙂
November 10, 2019 at 10:39 am #1058825AaronThat did the trick, thanks Leo! 🙂
As a follow up question, is there a way to change the “Blog pages show at most“ (under WordPress reading settings) to 5 for the home page, but some higher number for archives and search results?
November 10, 2019 at 10:56 am #1058834LeoStaffCustomer SupportThat setting is handled by WordPress itself but something like this should help:
https://generatepress.com/forums/topic/how-to-increase-post-snippets-on-a-category-page/#post-1035160November 10, 2019 at 11:12 am #1058841AaronHmm, that doesn’t seem to work for the search query results archives page (still stuck at 5), any ideas?
November 10, 2019 at 11:46 am #1058852LeoStaffCustomer SupportYou will need to add the a if statement for the search query:
https://codex.wordpress.org/Conditional_Tags#A_Search_Result_PageNovember 10, 2019 at 11:58 am #1058855AaronI’m no PhP expert, but would this snippet work?
function number_of_posts_on_archive($query){ if ($query->is_search && is_archive) { $query->set('posts_per_page', 25); } return $query; } add_filter('pre_get_posts', 'number_of_posts_on_archive');
November 10, 2019 at 12:03 pm #1058857LeoStaffCustomer SupportShould be ok. Give it a shot first?
November 10, 2019 at 12:08 pm #1058861AaronIt works for search, but not archive. I think my “&&” is somehow wrong.
The below is a less elegant solution, but I created three separate functions and it seems to work.
function number_of_posts_on_author($query){ if ($query->is_author) { $query->set('posts_per_page', 25); } return $query; } add_filter('pre_get_posts', 'number_of_posts_on_author'); function number_of_posts_on_search($query){ if ($query->is_search) { $query->set('posts_per_page', 25); } return $query; } add_filter('pre_get_posts', 'number_of_posts_on_search'); function number_of_posts_on_archive($query){ if ($query->is_archive) { $query->set('posts_per_page', 25); } return $query; } add_filter('pre_get_posts', 'number_of_posts_on_archive');
November 10, 2019 at 12:26 pm #1058872LeoStaffCustomer SupportWeird that the
&&
doesn’t work.Should be able to combine them like this at least:
function number_of_posts_on_archive($query){ if ($query->is_archive) { $query->set('posts_per_page', 25); } if ($query->is_search) { $query->set('posts_per_page', 25); } if ($query->is_author) { $query->set('posts_per_page', 25); } return $query; } add_filter('pre_get_posts', 'number_of_posts_on_archive');
November 10, 2019 at 12:34 pm #1058879AaronThanks Leo, and I think I figured it out. We can’t use the “&&” operator in this instance and should be using “||”
https://www.php.net/manual/en/language.operators.logical.php
This seems to work:
function number_of_posts_on_archive($query){ if ($query->is_search || is_archive || is_author) { $query->set('posts_per_page', 25); } return $query; } add_filter('pre_get_posts', 'number_of_posts_on_archive');
November 10, 2019 at 12:46 pm #1058887LeoStaffCustomer SupportAhh yeah right. It should be or instead of and for sure.
Good catch!
-
AuthorPosts
- You must be logged in to reply to this topic.