Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Blog Template instead of index.php

  • SOLVED

I've created a page template named "Blog". I created a main nav item called Blog and set it to use the Blog page template. I then went into settings > reading and set my posts page to the Blog page.

Problem is, WordPress is using the "index.php" file as the template for my Blog, rather than the custom page template I created.

Normally it wouldn't be much of an issue. I could just modify the index.php to be the same as the custom template. However in this case I have created a custom write panel, and I need the data that a user enters into that field to get displayed on the blog page. (this already works fine on all the other pages)

So, my question is: How do I modify my theme so that it uses my custom page template named "Blog" for the main posts page, rather than using the default index.php?

Answers (3)

2010-03-22

Nathan Parikh answers:

WP is using it's index.php file because of the reading settings you specified.

To make the Blog page use your template, changing the reading settings in the admin for your Posts page from the Blog page to nothing (change it back to -Select-) should do the trick.

Let me know if that works or not.


WP Answers comments:

Thank you for your response.

When I change the read settings, the blog page uses the correct template, but it does not display any of the posts.


Nathan Parikh comments:

Thanks for the update.
If it's still not displaying the posts, then you need to include the WordPress loop in the page template.


WP Answers comments:

Hi,

I've pasted the loop I'm using below. I know it works, because when I turn my "index.php" page into a replica of my custom page template, it displays fine. (only thing that doesn't display is the custom info at the top of the page that gets pulled in via a custom write panel).

Any ideas?

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="post_wrap">
<div class="post_top">
<div class="post_date_wrap">
<p class="post_date"><?php the_time('j'); ?></p>
<p class="post_month"><?php echo strtoupper(get_the_time('M')); ?></p>
</div><!-- end post_date_wrap -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p><strong>Posted by:</strong> <?php the_author_posts_link(); ?> / <strong>Tags:</strong> <?php the_tags('', ', '); ?> / <?php comments_number('0', '1', '%'); ?></p>
</div><!-- end post_top -->

<div class="post_content">
<div class="post_image_wrap">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
</div><!-- end post_image_wrap -->
<?php the_content("Read More &rarr;"); ?>
</div><!-- end post_content -->
</div><!-- end post_wrap -->
<?php endwhile; endif; ?>


Nathan Parikh comments:

For the loop to work in your page template, here's what you have to do:

Replace:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>

with:
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>


Change:
<?php endwhile; endif; ?>

to:
<?php endwhile;?>
<?php $wp_query = null; $wp_query = $temp;?>


That should display the posts properly.


WP Answers comments:

Hi Nathan,

This is working fine, but for some reason the "more" break isn't working. When I put the more break in the post, it just displays the whole post rather than breaking it with a read more link. Any ideas why this is so? Thanks for your help.

2010-03-22

Buzu B answers:

A very simple (and crude maybe) solutions is to just erase everything from your index.php and put

<?php
include("path/to/blog.php");
?>

Then it doesn't mater whether wp loads index.php o blog.php

NOTE: leave your blog.php page.


WP Answers comments:

Hi,

I need it to be dynamic, so would something like the below work?

How would I write it to be a proper PHP include? (since i can't put php inside the include quotes)

<?php bloginfo('template_url'); ?>/template-blog.php

Thanks for your help


Buzu B comments:

Sorry, I don;t think I quite understand what you mean when you say you need it to be dynamic. Do you need that index pulls out the informations from the custom write panel for every post when it is performing the loop? Or what exactly it is you need?

2010-03-22

Michael Fields answers:

<blockquote>So, my question is: How do I modify my theme so that it uses my custom page template named "Blog" for the main posts page, rather than using the default index.php?So, my question is: How do I modify my theme so that it uses my custom page template named "Blog" for the main posts page, rather than using the default index.php?</blockquote>

There is no easy way to accomplish this exactly as you are proposing. Basically, Wordpress is just using you "Blog" page as a temporary container so that it can easily be included into dynamically generated navigation elements created by functions like wp_list_pages().

If you look at the [[LINK href="http://codex.wordpress.org/images/1/18/Template_Hierarchy.png"]]Template Hierarchy Diagram[[/LINK]], you will see that there is no template for blog.php

The best solution that I can offer is to work with WordPress allowing index.php to display your blog view while using [[LINK href="http://codex.wordpress.org/Conditional_Tags"]]conditional functions[[/LINK]] to display the data from your custom write panel. is_home() should do the trick for you.

Hope this helps,
-Mike


WP Answers comments:

Thanks for your response. So, I should modify my index.php page to include everything I put in my custom blog page template?

Here is the code that pulls in the content from the custom write panel:
<p><?php if(get_post_meta($post->ID, "text_value", $single = true) != "") : echo get_post_meta($post->ID, "text_value", $single = true); ?><?php endif; ?></p>

How would I edit that to use conditional comments?

Also, If a user goes into the back-end and goes to Pages > Edit and chooses the blog page, and then they input text into the custom write panel, will it show up with the conditional tags in place?