[Resolved] Add posts list after page content, with featured images

Home Forums Support [Resolved] Add posts list after page content, with featured images

Home Forums Support Add posts list after page content, with featured images

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #663216
    Bidegato

    Hello,

    My final goal is to add a grid of posts of a given CPT, after the content of a page, that uses a given template.
    For this, I created a GP child theme and added a new template on it. This template uses the default loop for the page content, and a custom query for the posts list, and its code is just a simplified merge of both “page.php” and “archive.php”, so letting GeneratePress to take care of all the magic it is able to do.

    The template adds the list of custom posts after its content as intended, but without featured image (Screen capture).
    I expected it to include the featured image, as it is shown in the CPT archive, both using or not using “blog” module (Screen capture).

    Why is GP template system not showing the featured image as I expected? What do I need to to in order to include the featured image on every CPT post of this list?

    I’m sure I’m missing something, so thank you for your guide.

    Template code follows:

    <?php
    /**
     * Template Name: academia
     * The template for displaying all pages.
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages
     * and that other 'pages' on your WordPress site will use a
     * different template.
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header();
     ?>
    
    	<div id="primary" <?php generate_content_class();?>>
    		<main id="main" <?php generate_main_class(); ?>>
    			<?php
    			/**
    			 * generate_before_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_before_main_content' );
    
    			
    			while ( have_posts() ) : the_post();
    
    				get_template_part( 'content', 'page' );
    
    			endwhile;
    			
    			
    			//***********************************************************************
    			// Use a custom query to add the course grid after the page content
    			//***********************************************************************
    
    			$args = array (
    				'post_type'		=>	'bdg_curso', //my own custom post type
    				'post_parent'	=>	0, //query only top level parents
    						);
    						       
                $the_query = new WP_Query($args);
           
    	   		if ($the_query->have_posts() ) :
    			
    				?> 
    					<div class="cursosgrid">
    				<?php
    				while ( $the_query->have_posts() ) : $the_query->the_post();
    
    					/*
    					 * Include the Post-Format-specific template for the content.
    					 * If you want to override this in a child theme, then include a file
    					 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
    					 */
    					 get_template_part( 'content', get_post_format() );
    
    				endwhile; wp_reset_postdata();
    				
    				?> 
    					</div>
    				<?php
    				
    				generate_content_nav( 'nav-below' );
    
    			else :
    
    				get_template_part( 'no-results', 'archive' );
    
    			endif;
    			//***********************************************************************
    			
    			
    
    			/**
    			 * generate_after_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_after_main_content' );
    			?>
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    	<?php
    	/**
    	 * generate_after_primary_content_area hook.
    	 *
    	 * @since 2.0
    	 */
    	//do_action( 'generate_after_primary_content_area' );
    
    	generate_construct_sidebars();
    
    get_footer();
    ?>
    GeneratePress 2.1.4
    GP Premium 1.7.2
    #663274
    David
    Staff
    Customer Support

    Hi there,

    have you considered using a plugin to display your CPT posts? Tom’s own WP Show Posts does this nicely:

    https://wordpress.org/plugins/wp-show-posts/

    You could then use the GP Hooks Element to add the content where you need:

    https://docs.generatepress.com/article/hooks-element-overview/

    #664214
    Bidegato

    Hello,

    Yes, I’ve tested WP Show Posts, but it does not totally fulfill my needs, because I need to control some query parameters (e.g: query only top-level parent posts) and filter the output based on some variables (e.g: add extra text under the featured image of every posts having a custom taxonomy term). I do that using standard WordPress filter hooks, that works well on default WordPress CPT archives, but that WP Show Posts plugins seems to ignore.

    That is why I’m looking to use my own custom query, and add its output under a page content, using custom template. The problem is that creating that custom template, as a merge of GP owns “page.php” and “archive.php”, does not show featured images of the posts, as I expected.
    I’d like to know why it does not show them, and appreciate any guidance to achive that.

    Thank you!

    #664761
    Tom
    Lead Developer
    Lead Developer

    Is there any chance you can link me to the page?

    #665468
    Bidegato

    Sure! I’ve created a copy of my development site, and have attached its URL to this support thread.

    Should you require it, you have admin access. Just use your name and surname as username and password respectively (all lowercase).

    Thank you for your support.

    #665643
    Tom
    Lead Developer
    Lead Developer

    So in your custom query, replace this:

    /*
     * Include the Post-Format-specific template for the content.
     * If you want to override this in a child theme, then include a file
     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
     */
     get_template_part( 'content', get_post_format() );

    With the contents of this file: https://github.com/tomusborne/generatepress/blob/2.1.4/content.php

    #666079
    Bidegato

    Thanks, Tom, I’ve done it (the proposal is to include content.php inline into my custom template, right?).
    The featured images are still not shown in the post list after the page content.
    I’ve traced down to generate_post_image() function in featured_images.php, and checked that following condition is always false on every count of the custom query loop:

    if ( ! is_singular() && ! is_404() ) {...}

    so that seems to be why generate_featured_image_output is not fired.

    Is this the expected behavior? Am I using custom query in a wrong way?

    Thank you!

    #666313
    Tom
    Lead Developer
    Lead Developer

    Ahh, right. The page you’re using is template is_singular(), so that’s failing.

    Try adding this function:

    add_action( 'generate_after_entry_header', function() {
    	if ( is_page_template( 'your-template.php' ) ) {
    		if ( ! has_post_thumbnail() ) {
    			return;
    		}
    		
    		printf( // WPCS: XSS ok.
    			'<div class="post-image">
    				<a href="%1$s">
    					%2$s
    				</a>
    			</div>',
    			esc_url( get_permalink() ),
    			get_the_post_thumbnail(
    				get_the_ID(),
    				apply_filters( 'generate_page_header_default_size', 'full' ),
    				array(
    					'itemprop' => 'image',
    				)
    			)
    		);
    	}
    } );

    Then just update the is_page_template call to your template file.

    #666565
    Bidegato

    I see.. now all the pieces make sense to me ๐Ÿ™‚ Now I just have to work on visual layout.

    Thank you very much. It definitively works.

    #666716
    Tom
    Lead Developer
    Lead Developer

    Awesome, glad I could help ๐Ÿ™‚

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