[Resolved] What is the best way to override the GP template used for blog listings?

Home Forums Support [Resolved] What is the best way to override the GP template used for blog listings?

Home Forums Support What is the best way to override the GP template used for blog listings?

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1568864
    Samuel

    Here’s what I want to do:

    I am using a child theme.

    On many archive pages (main blog, search results, archives, etc) I want to provide my own template for how posts are listed. In my case I think it would be cleaner than using several filters to override the HTML GP creates.

    My understanding is that context.php is the template file being loaded so I could simply put content.php in my child theme. However, I’m wondering if there is an easy way to hook into generate_do_template part so when the page loads I can check if it’s a page where I want to override GP and then provide my own code for the post listing instead of overriding the content.php in every case. I figure the less I override the better for compatibility with future releases.

    I hope this makes sense?

    #1568906
    Samuel

    After looking more I ended up implementing a solution based on this sample code. Is there a better way with the latest GP / GPP / GB? If not I’ll stay with this.

    // Disable the core template part on the blog page.
    add_filter( 'generate_do_template_part', function( $do ) {
        if ( is_home() ) {
            return false;
        }
        return $do;
    } );
    
    // Add our own.
    add_action( 'generate_before_do_template_part', function() {
        if ( is_home() ) : ?>
            <article <?php post_class(); ?>>
                <div class="inside-article">
                    <h1><?php the_title(); ?></h1>
                    <a class="button" href="<?php the_permalink(); ?>">Read more!</a>
                </div>
            </article>
        <?php endif;
    } );
    #1569129
    David
    Staff
    Customer Support

    Hi there,

    that snippet is the right way of using the do_template part hooks, and is the ‘simplest’ method for what your want πŸ™‚

    #1571062
    Samuel

    I used the solution above to customize the loop for searches and archives. It works perfectly with one exception. If there are no results there is no output at all.

    What is strange is that, for example, if I run a search I get the standard search title information on the page and then my custom code outputs the loop of posts the match the search. However, if the search finds no results I do not get anything on the page–no search title at all.

    Obviously I can check the global $post variable for empty, but I’m wondering why the title information for the page is only showing up if there are results? Shouldn’t the title be generated regardless? How is it being generated on an archive with results but not one without results?

    #1571927
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    The code you shared should target the main blog page only. Have you tweaked it to apply to search results/archives? If so, can you share the updated code?

    Let me know πŸ™‚

    #1571955
    Samuel

    Here is the all the relevant code:

    /**
    * Indicates if we are on an archive page that should use the simple archive.
    */
    function mb_is_simple_archive_page() {
    	return ( is_home() || is_archive() || is_search() )  ? true : false;
    }
    /**
    * Bypass the default html generated on archive pages.
    */
    add_filter( 'generate_do_template_part', function( $do ) {
        return ( mb_is_simple_archive_page() )? false : $do;
    });
    
    /**
    * Replace the html generated on archive pages.
    */
    add_action( 'generate_before_do_template_part', function() {
        if ( mb_is_simple_archive_page() ) {
    		global $post;
    		echo mb_post_archive_link_with_metadata_html( $post, array() );
    	}
    });
    /**
    * Outputs the message title with some key meta data for a simple archive. Can also be used as a filter for the ett_post_archive_html shortcode/function.
    */
    function mb_post_archive_link_with_metadata_html( $post, $display_atts ) {
    	$html = '<div class="ett-post-archive-item"><h3><a href="' . get_the_permalink( $post ) . '">' . get_the_title( $post ) . '</a></h3><ul class="metadata-links">';
    	$html .= '<li>' . get_the_date( '', $post ) . '</li>';
    	//TODO get event info
    	$html .= '<li>Event: <a class="event-link" href="">FCF</a></li>';
    	//TODO get series info
    	$html .= '<li>Series: <a class="series-link" href="">Apostlic Prayers</a></li>';
    	$html .= '</ul></div>';
    	return $html;
    }
    #1573214
    Tom
    Lead Developer
    Lead Developer

    So the issue right now is there is no “no results” page?

    If so, you likely need to check for posts like this: https://github.com/tomusborne/generatepress/blob/3.0.2/archive.php#L25

    And output content if no posts exist: https://github.com/tomusborne/generatepress/blob/3.0.2/archive.php#L53

    Let me know if that helps or not πŸ™‚

    #1573227
    Samuel

    That’s basically what I did. I added a check for have_posts(). I thought I’d check in case there was a bug or something else I needed to adjust. Thanks!

    #1574667
    Tom
    Lead Developer
    Lead Developer

    Did adding that check work? If there are no posts (that check is false), there should be text saying so in the function πŸ™‚

    #1574952
    Samuel

    Here is what I ended up having to do:

    /**
    * Bypass the default html generated on archive pages.
    */
    add_filter( 'generate_do_template_part', function( $do ) {
    	global $post;
        return ( mb_is_simple_archive_page() && ( ! empty( $post) ) ) ? false : $do;
    });
    
    /**
    * Replace the html generated on archive pages.
    */
    add_action( 'generate_before_do_template_part', function() {
        if ( mb_is_simple_archive_page() ) {
    		global $post;
    		if ( ! empty( $post ) ) {
    			echo mb_post_archive_link_with_metadata_html( $post, array() );
    		}
    	}
    });

    I tried a solution with have_posts() in it but found a massive problem: when there are results have a check that called have_posts() messed with the loop and created a massive performance problem (php worker taking 99% of CPU).

    If there is a better solution that would be great, but checking for an empty $post variable was the best I could think of.

    #1576502
    Tom
    Lead Developer
    Lead Developer

    That looks good to me πŸ™‚

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