[Resolved] Index and Archive Content Types

Home Forums Support [Resolved] Index and Archive Content Types

Home Forums Support Index and Archive Content Types

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1058760
    Aaron

    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!

    #1058781
    Leo
    Staff
    Customer Support

    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 🙂

    #1058825
    Aaron

    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?

    #1058834
    Leo
    Staff
    Customer Support

    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

    #1058841
    Aaron

    Hmm, that doesn’t seem to work for the search query results archives page (still stuck at 5), any ideas?

    #1058852
    Leo
    Staff
    Customer Support

    You will need to add the a if statement for the search query:
    https://codex.wordpress.org/Conditional_Tags#A_Search_Result_Page

    #1058855
    Aaron

    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');
    #1058857
    Leo
    Staff
    Customer Support

    Should be ok. Give it a shot first?

    #1058861
    Aaron

    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');
    #1058872
    Leo
    Staff
    Customer Support

    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');
    #1058879
    Aaron

    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');
    #1058887
    Leo
    Staff
    Customer Support

    Ahh yeah right. It should be or instead of and for sure.

    Good catch!

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.