Site logo

[Resolved] Custom Archive Page Template Issues

Home Forums Support [Resolved] Custom Archive Page Template Issues

Home Forums Support Custom Archive Page Template Issues

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #714315
    Andy

    Hi,
    I’ve setup a custom page template so I can list posts from 2 categories on a particular page. However, although all the correct posts are displayed, the featured image thumbnails are not displaying and neither is the pagination at the bottom of the page. I used the category.php as a starting point and just edited it and renamed it page-archive.php, see my code below:

    <?php
    /**
     * Template Name: All Routes
     *
     * @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' );
    
    			$args = array(
    					'cat' => '1,4',
    					'posts_per_page' => 8
    
    			);
    
    			$routes_posts = new WP_Query( $args );
    
    			if ( $routes_posts->have_posts() ) :
    
    				/**
    				 * generate_archive_title hook.
    				 *
    				 * @since 0.1
    				 *
    				 * @hooked generate_archive_title - 10
    				 */
    				do_action( 'generate_archive_title' );
    
    				while ( $routes_posts->have_posts() ) : $routes_posts->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;
    
    				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();

    Any help appreciated.

    #714982
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Any chance you can link me to one of those pages? The featured images should display.

    For the pagination, try this:

    add_action( 'generate_after_main_content', function() {
        if ( ! is_page_template( 'page-your-template.php' ) ) {
            return;
        }
        ?>
        <nav id="nav-below" class="post-navigation">
            <?php
                the_posts_pagination( array(
                    'mid_size' => apply_filters( 'generate_pagination_mid_size', 1 ),
                    'prev_text' => apply_filters( 'generate_previous_link_text', __( '&larr; Previous', 'generatepress' ) ),
                    'next_text' => apply_filters( 'generate_next_link_text', __( 'Next &rarr;', 'generatepress' ) ),
                ) );
            ?>
        </nav>
        <?php
    } );

    You’ll need to change page-your-template.php to your template name.

    #714999
    Andy

    Hi Tom,

    I’ve edited my original post with the URL to the page I’m using the custom template on. It’s setup as a private page so you’ll need to login as admin to view it, I’ve included login details with the URL.

    I tried adding that code to my functions file and purging cache but pagination is still not displaying.

    The reason I’m doing this is so I can use an ajax filtering plugin with categories on the page but it will only work using the standard wordpress loop arguments.

    #715017
    Tom
    Lead Developer
    Lead Developer

    In your code, try replacing:

    generate_content_nav( 'nav-below' );

    With:

    the_posts_pagination();

    And remove the code I gave you above.

    For the featured image, try this:

    add_action( 'generate_after_entry_header', function() {
        if ( is_page_template( 'your-template.php' ) ) {
            echo '<div class="post-image">';
                the_post_thumbnail();
            echo '</div>';
        }
    } );
    #715028
    Andy

    ok I replaced the code in the template and functions file but still not getting any pagination.

    I can see that the thumbnail html is loading in the browser console but for some reason it’s not visible on the page and I can’t see any CSS hiding it.

    Sorry about this Tom, I thought as I was using standard wordpress loop practices and the themes archive template as a starting point that this would be straightforward, but it seems it isn’t.

    #715081
    Tom
    Lead Developer
    Lead Developer

    Maybe you need to set up the paged variable in your query?: https://wordpress.stackexchange.com/a/233208

    As for the featured image, that’s my bad. I just edited the code above – give it another shot: https://generatepress.com/forums/topic/custom-archive-page-template-issues/#post-715017

    #715416
    Andy

    Almost there, featured images are displaying but they don’t link to the article. I’ve added ‘paged’ => $paged to the array but still no pagination.

    #715446
    Andy

    Ok I’ve changed the loop and its displaying pagination and featured images with links now but it’s also pulling in the page title and page description from one of the post categories. Is there anyway of preventing these from getting pulled in?

    Heres the new code:

    <?php
    /**
     * Template Name: All Routes
     *
     * @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' );
    
    			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    			$args = array(
    					'cat' => '1,4',
    					'posts_per_page' => 8,
    					'paged' => $paged
    
    			);
    			query_posts($args); ?>
    			
    			<?php if ( have_posts() ) :
    
    				/**
    				 * generate_archive_title hook.
    				 *
    				 * @since 0.1
    				 *
    				 * @hooked generate_archive_title - 10
    				 */
    				do_action( 'generate_archive_title' );
    
    				while ( have_posts() ) : 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;
    
    				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();
    #715472
    Andy

    Sorry Tom I realise I’m approaching this in the wrong way. I should be using the filtering plugin to determin which posts are displayed on page load.

    Closing thread.

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