开发者

Page template, single page display

I've built a custom page.php template. Very simple, essentially:

<?php get_header(); ?>

<?php get_sidebar(); ?>

<?php the_post(); ?>

<h1><?php the_title(); ?></h1>

<?php get_footer(); ?>

I've created a few pages, and if I visit their url, I just get the same page title.

I read up in the documentation, and it says to use the_title() and such only when in "the loop".

So presumably, I'm just being shown the first page in t开发者_StackOverflowhe "array".

Is there any way I get get the contents of a single page based on the url?

Edit: In fact, should I even need to do this? Refering to example templates, it looks like I'm doing everything right?


In my sidebar I was using a custom query.

This was called before trying to access the main page content, without resetting.

When doing a custom query you must reset after you've finished your loop like so:

$originalPost = $post;
$sidePosts = get_posts($queryArgs);
foreach($sidePosts as $post) {
    setup_postdata($post);
    // echo it out like a normal post.
}
$post = $originalPost;

or if you are using query_posts() (which you shouldn't in a sidebar):

wp_reset_query();

Which will take your post back to it's previous value.


For a custom page template please use the following to get everything correct

<?php
/* Template name: My custom template */

get_header(); 


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

the_title(); 
the_content()

endwhile; 
get_sidebar(); 
get_footer();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜