- This topic has 8 replies, 2 voices, and was last updated 7 years, 7 months ago by
Andy.
-
AuthorPosts
-
October 30, 2018 at 7:34 am #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.
October 30, 2018 at 4:44 pm #714982Tom
Lead DeveloperLead DeveloperHi 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', __( '← Previous', 'generatepress' ) ), 'next_text' => apply_filters( 'generate_next_link_text', __( 'Next →', 'generatepress' ) ), ) ); ?> </nav> <?php } );You’ll need to change
page-your-template.phpto your template name.October 30, 2018 at 4:59 pm #714999Andy
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.
October 30, 2018 at 5:19 pm #715017Tom
Lead DeveloperLead DeveloperIn 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>'; } } );October 30, 2018 at 5:39 pm #715028Andy
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.
October 30, 2018 at 7:30 pm #715081Tom
Lead DeveloperLead DeveloperMaybe you need to set up the
pagedvariable in your query?: https://wordpress.stackexchange.com/a/233208As 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
October 31, 2018 at 5:51 am #715416Andy
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.
October 31, 2018 at 6:32 am #715446Andy
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();October 31, 2018 at 7:02 am #715472Andy
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.
-
AuthorPosts
- You must be logged in to reply to this topic.