0

Okay So I want to create a Custom Page Template that displays my posts exactly the same as my index.php does. You can see the code of my index.php at the bottom.

So I create a .php file named customindex.php and use the exact same code of my index.php and the code on the top to mark it as custom page template:

<?php
/*
Template Name: Custom Index
*/

Problem: The page with the Custom Index page template won't display any posts and I don't know why.

Code of my index.php:

<?php get_header(); ?>
<h1 class="heading" style="width:100%"><?php _e( 'Beliebte Zitate', 'html5blank'); ?></h1>

<?php get_sidebar(); ?>

<section id="home">

    <?php get_template_part('loop'); ?>

</section>

    <div id="pagination">
        <?php html5wp_pagination(); ?><br>
    </div>

<?php get_footer(); ?>

Code of my customindex.php (nearly the same as index.php):

 <?php
/*
Template Name: Custom Index
*/ ?>
<?php get_header(); ?>
<h1 class="heading" style="width:100%"><?php _e( 'Beliebte Zitate', 'html5blank');   ?></h1>

 <?php get_sidebar(); ?>

<section id="home">

<?php get_template_part('loop'); ?>

</section>

<div id="pagination">
    <?php html5wp_pagination(); ?><br>
</div>

<?php get_footer(); ?>

My loop.php looks like this:

<?php

 if (have_posts()): while (have_posts()) : the_post(); ?>


<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
    LOTS OF STUFF
</article>

<?php endwhile;  ?>

<?php else: ?>

<article>
    <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>

 <?php endif; ?>
  • 1
    Where is the code of your custom page template? (Note: also: if applicable, where is the code of loop.php?) The code in your index.php file isn't really relevant. – Chip Bennett Apr 14 '14 at 14:06
  • 1
    Are you 100% sure that you're hitting that specific page template? – Howdy_McGee Apr 14 '14 at 14:06
  • edited - what I don't get is why the code is working fine in index.php, but not with the custom page template – ndru Apr 14 '14 at 14:14
6

You haven't created a query to return your post index results.

Let me back up...

In WordPress something called the "Main" query runs very early in the page load, and well before your template files load. That query retrieves the posts to display and also (more or less) determines which template file to use to display the results. That "Main" query retrieves different results based upon the URL requested, or you'd get exactly the same thing on all pages. The results on the index page are going to be different from the results on your custom page-- the results on your custom page is going to be the single page itself and not the index. That means that if you want an index on some secondary page, you need to create a query.

It could be as simple as:

$p = new WP_Query(array('post_type' => 'post'));
if ($p->have_posts()) {
  while ($p->have_posts()) {
    $p->the_post();
    // Display
  }
}

However, the peculiarities of your theme could complicate things, so be warned.

| improve this answer | |
  • Just started to type this up. +1 to you instead. – Chip Bennett Apr 14 '14 at 14:15
  • @ChipBennett : I guessed at the problem before the "Page" code was posted :) – s_ha_dum Apr 14 '14 at 14:16
  • Same here; just wanted to try to improve the question quality before answering. :) – Chip Bennett Apr 14 '14 at 14:22
  • @s_ha_dum using above mentioned query style on my custom template paging is not being displayed.... my paging code is as below: <div class="paging"> <?php global $wp_query; $big = 999999999; echo paginate_links(array( 'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 'format' => '?paged=%#%', 'current' => max(1, get_query_var('paged')), 'total' => $wp_query->max_num_pages )); ?> </div> – Mohsin Saeed Sep 8 '16 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.