Archived topic
Index and Archive Content Types
11 replies · Started by Aaron on November 10, 2019
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!
Hi 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 :)
That 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?
That 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-1035160
Hmm, that doesn't seem to work for the search query results archives page (still stuck at 5), any ideas?
You will need to add the a if statement for the search query:
https://codex.wordpress.org/Conditional_Tags#A_Search_Result_Page
I'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');
Should be ok. Give it a shot first?
It 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');
Weird 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');
Thanks 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');
Ahh yeah right. It should be or instead of and for sure.
Good catch!