• Resolved Cas Dekkers

    (@casdekkers)


    Hi everyone,

    I am currently experiencing some issues with creating Specialized Page Templates. Seems simple enough, even to me, but I just can not get it to work. I was hoping someone here could help me out!

    So this is what I’m trying to achieve: I have created a homepage, index.php, and I have created several Pages I want to customize, including the Articles Page, http://www.mysite.com/articles/ (permalink). The goal is to display the 3 latest posts on index.php, among other things, and to display all posts, let’s say, 10 per page, on the /articles/ Page.

    My idea was to create a custom template for the /articles/ Page in my theme, page-articles.php, and paste in the same code as for my index.php file and simply change the while (have_posts()) condition in my loop (I figured I could easily bypass the global “X posts per page” setting under Dashboard > Settings > Reading > Blog pages show at most X posts by using the technique on this tutorial (method 1)). This, however, did not work; the /articles/ Page did display ‘test, test’ HTML content I added in page-articles.php, but did not show any posts from The Loop, which I had definitely called in that file; instead, the page layout was formatted exactly like the standard page.php theme template file. That’s strange, where did it get this formatting? I did not define this in page-articles.php?

    Just so I did not have to deal with the above issues, I decided to change my tactics and try and use Conditional Tags to do the trick by editing the content.php theme file, which contains The Loop:

    <div id="content" role="main">
    	<?php
    		if ( !is_home() ) {
    			if ( have_posts() ) :
    				while ( have_posts() ) :
    					the_post();
    					get_template_part( 'entry' );
    					comments_template();
    				endwhile;
    			endif;
    			get_template_part( 'nav', 'below' );
    		}
    		else {
    			$i = 0;
    			while ( have_posts() && $i < 3) :
    				the_post();
    				get_template_part( 'entry' );
    				comments_template();
    				$i++;
    			endwhile;
    			get_template_part( 'nav', 'below' );
    		}
    	?>
    </div>

    However, this yields not only my index.php, but also my /articles/ Page to display only 3 posts (whereas /articles/ should display 10, because I changed the Blog pages show at most X posts setting to this value).

    I am at a loss. Does anybody know how to solve the issues with Specialized Theme Templates, or does anybody have a solution for the problem I’m having with Conditional Tags? Any help would be sincerely appreciated. Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • It seems that the template page-articles.php is not even being used. You should try to echo something in each template to see exactly which one ends up being used. You might also want to give the page-{ID}.php format a try.

    Thread Starter Cas Dekkers

    (@casdekkers)

    Thank you, your comment did not solve the problem, but it did help me a lot!

    Using Specialized Page Templates I figured out that page-articles.php is the template file being used (hooray!) for my /articles/ Page. I have set the Blog pages show at most X posts setting to 10, and in my index.php file, I shortened The Loop to show at most 3 blog posts. This all works nicely.

    Now, on the Articles Page (/articles/), I want to display all posts with 10 posts per page. So, in my page-articles.php file, which is the template for /articles/, I have put the following code:

    <?php get_header(); ?>
    <div id="content" role="main">
    	<?php
    		if ( have_posts() ) :
    			while ( have_posts() ) :
    				the_post();
    				get_template_part( 'entry' );
    				comments_template();
    			endwhile;
    		endif;
    		get_template_part( 'nav', 'below' );
    	?>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    I thought this was the right way to show all posts from my blog on the Articles Page, but it is not. Currently, the articles page only displays itself, just like I filled it in under Dashboard > Pages > Articles. As it turns out, The Loop in the above code does not loop over all blog posts, but over one post: the Articles Page (which technically is a post, too) I created under Dashboard > Pages > Articles.

    So, my question is: how can I make my Loop show all blog posts on this Page with a custom template?

    Thread Starter Cas Dekkers

    (@casdekkers)

    I solved my problem by using this reference: http://digwp.com/2013/01/display-blog-posts-on-page-with-navigation/

    Setting up a new WP_Query() seems to do the trick. My code is now as follows:

    <?php get_header(); ?>
    <div id="content" role="main">
    	<?php
    		$temp = $wp_query;
    		$wp_query= null;
    		$wp_query = new WP_Query();
    		$wp_query->query('posts_per_page=5' . '&paged='.$paged);
    		if ( $wp_query->have_posts() ) :
    			while ( $wp_query->have_posts() ) :
    				$wp_query->the_post();
    				get_template_part( 'entry' );
    				comments_template();
    			endwhile;
    		endif;
    		get_template_part( 'nav', 'below' );
    		wp_reset_postdata();
    	?>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    It’s important not to forget calling wp_reset_postdata(); at the end to reset your query.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problems with Specialized Page Templates’ is closed to new replies.