0

I read a tutorial about how to Display the contents of static page Posts page.

Basically I had to place this code right before the Loop:

<?php //This is the code from the tutorial
if ( 'page' == get_option('show_on_front') && get_option('page_for_posts') && is_home() ) : the_post();
 $page_for_posts_id = get_option('page_for_posts');
 setup_postdata(get_page($page_for_posts_id));
?>
 <div id="post-<?php the_ID(); ?>" class="page">
  <div class="entry-content">
   <?php the_content(); ?>
   <?php edit_post_link('Edit', '', '', $page_for_posts_id); ?>
  </div>
 </div>
<?php
 rewind_posts();
endif;
?>

I wanted to also include the_content, but instead the the_title of the first post (blog) entry was shown:

<div class="entry-content">
       <?php the_title(); ?>
       <?php the_content(); ?>
       <?php edit_post_link('Edit', '', '', $page_for_posts_id); ?>
      </div>

picture: enter image description here

Code:

loop.php

<?php
if ( 'page' == get_option('show_on_front') && get_option('page_for_posts') && is_home() ) : the_post();
    $page_for_posts_id = get_option('page_for_posts');
    setup_postdata(get_page($page_for_posts_id));
?>
<div class="shadow-top">
    <!-- Shadow at the top of the slider -->
</div>
<div id="intro2">
    <div class="container">
        <h2><?php the_title(); ?></h2>
        <p><?php the_content(); ?></p>
        <?php edit_post_link('Edit', '', '', $page_for_posts_id); ?>
    </div><!-- .container -->
</div><!-- #featured -->
<div class="shadow-bottom">
    <!-- Shadow at the bottom of the slider -->
</div>
<div id="content">
    <div class="container">
        <div id="blog">
            <div class="posts">
                <?php
                    rewind_posts();
                endif;
                ?>
            </div>

<?php
    /* Start the Loop.
     *
     * In Twenty Ten we use the same loop in multiple contexts.
     * It is broken into three main parts: when we're displaying
     * posts that are in the gallery category, when we're displaying
     * posts in the asides category, and finally all other posts.
     *
     * Additionally, we sometimes check for whether we are on an
     * archive page, a search page, etc., allowing for small differences
     * in the loop on each template without actually duplicating
     * the rest of the loop that is shared.
     *
     * Without further ado, the loop:
     */ ?>
<?php while ( have_posts() ) : the_post(); ?> 

(...)

blog.php:

<?php
/**
 * Template Name: Blog
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>
        <?php // Start the Main Loop
        if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
        <div class="posts">
                 <h2><?php the_content(); ?></h2>
            </div><!-- .content -->
        </div><!-- .posts -->
            <?php endwhile; ?>
        <?php endif; ?>
        <?php get_sidebar(); ?>
    </div><!-- .container -->
</div><!-- #content-bottom -->
<?php get_footer(); ?>

Any suggestions?

EDIT:

Right now I'm using the following code. It retrieves the page's specific the_content but when retrieving the_title, it still retrieving the first blog post.

<?php
/**
 * Template Name: Blog
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>
<div class="shadow-top">
    <!-- Shadow at the top of the slider -->
</div>
<div id="intro2">
    <div class="container">
        <?php /* Display the contents of static page Posts page */
        if ( 'page' == get_option('show_on_front') && get_option('page_for_posts') && is_home() ) : the_post();
            $page_for_posts_id = get_option('page_for_posts');
            setup_postdata(get_page($page_for_posts_id));
        ?>
            <div id="post-<?php the_ID(); ?>" class="page">
                <div class="content">
                    <h2><?php the_content(); ?></h2>
                    <?php edit_post_link('Edit', '', '', $page_for_posts_id); ?>
                </div>
            </div>
        <?php
            rewind_posts();
        endif;
        ?>
    </div><!-- .container -->
</div><!-- #featured -->
<div class="shadow-bottom">
    <!-- Shadow at the bottom of the slider -->
</div>
<div id="content">
    <div class="container">
        <div id="blog">
    <div class="container">

        <?php // find all content that has the type of video and then to loop through them. ?>

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

                        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <?php if ( is_front_page() ) { ?>
                                <h2 class="entry-title"><?php the_title(); ?></h2>
                            <?php } else { ?>
                                <h1 class="entry-title"><?php the_title(); ?></h1>
                            <?php } ?>

                            <div class="entry-content">
                                <?php the_content(); ?>
                                <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                                <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                            </div><!-- .entry-content -->
                        </div><!-- #post-## -->

                        <?php comments_template( '', true ); ?>

        <?php endwhile; ?>
        </div>
        <?php get_sidebar(); ?>
    </div><!-- .container -->
</div><!-- #content-bottom -->
<?php get_footer(); ?>
3
  • Paste all if the code
    – Bainternet
    Commented Jan 26, 2011 at 8:50
  • @בניית אתרים OK i did.
    – wyc
    Commented Jan 26, 2011 at 8:56
  • use <?php echo get_the_title( $page_for_posts_id ); ?> for title
    – zeo
    Commented Jun 22, 2011 at 13:00

2 Answers 2

2

This is a relatively simple task, and you only need to attach an appropriately coded page template to your chosen post's page.

When the page first loads, the main query holds data on the page, such as the title, content and so forth, we can shortcut to that by calling the_post which will then allow us to simply call the template tags to produce the title(etc) for the page. Following that we can then call query_posts and refill the main query object with a selection of posts.

This is very similar to the examples given on the codex, here.
http://codex.wordpress.org/Pages#A_Page_of_Posts

The main difference here, is that rather than creating a second query object, i'm just going to re-use the main one(why not, it makes sense in this context, imho).

Adjust the code(Query parameters/HTML/Classes/IDs) as appropriate.

<?php
/**
 * Template Name: Page of Posts
 *
 * Selectable from a dropdown menu on the edit page screen.
 */
get_header(); 
?>

<div id="container">
    <div id="content">

        <?php the_post(); // Setup post data so it's possible to call the regular loop template tags ?>
        <h2><?php the_title(); ?><h2>
        <div <?php post_class(); ?>><?php the_content(); ?></div>

        <?php 
        $args = array( 
            'paged' => (int) get_query_var('paged'), 
            'post_type' => 'post', 
            'order' => 'desc', 
            'posts_per_page' => 3, 
            'caller_get_posts' => 1 
        );
        // Now re-define the main query
        query_posts( $args ); 
        ?>

        <?php if( have_posts() ) : ?>

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

                <h2><?php the_title(); ?></h2>
                <div <?php post_class(); ?>><?php the_content(); ?></div>

            <?php endwhile; ?>

        <?php endif; ?>

    </div>
</div>

<?php //get_sidebar(); ?>
<?php get_footer(); ?>

Hope that helps..

Updated response follows

The template that deals with a post's page will differ if you use the page_for_posts setting, don't try to use and a page template, use one or the other, since both will not work together.

When you want to show the post list/index on a page, and let your native theme files deal with rendering, set the page_for_posts setting(admin > settings > reading).

When you need to control how a page of posts renders, and do something custom with it, rather than fiddle around in the theme files trying to work out which file is dealing with the output, then adding conditional logic, instead create a page(or choose an existing one) and attach a page template to it. Only by not setting the page_for_posts setting do you allow this page to render using the attached template.

enter image description here

Ignore my front page setting, obviously you may have a more appropriate page set for that, simply notice how i've not set the posts page setting. If you set yours as done in the screenshot, the template provided will work correctly, and the posts page will behave just as it would when setting the posts page setting(except it will obviously do some custom stuff to).

Summary
- When you set the page_for_posts setting you fore-go any ability to attach a template to that page, unset it and the template will work as expected.

Edit: Rarst actually pointed out this behaviour to, in your other question...
Template file for static posts page does not get loaded

4
  • Sorry it didn't work. For some reason the part above displays the first posts not the page title or content. (by the way, this page is set to be the posts page).
    – wyc
    Commented Jan 26, 2011 at 16:41
  • Please check my EDIT above to see waht I've done so far.
    – wyc
    Commented Jan 26, 2011 at 16:49
  • @janoChen - I've updated my answer to explain more.
    – t31os
    Commented Jan 26, 2011 at 18:30
  • @janoChen The only thing that I've managed to do is to display the_content of the specific page (with the edit code above). I think it do some stuff before the loop starts.
    – wyc
    Commented Jan 27, 2011 at 7:08
0

You could skip setting up the post data and just go directly to the queried object.

$queried_object = get_queried_object();

if( ! is_wp_error( $queried_object ) ) {

    if ( isset( $queried_object->post_title ) )
        echo '<h1>' . $queried_object->post_title . '</h1>';

    if ( isset( $queried_object->post_content ) )
        echo '<p>' . $queried_object->post_content . '</p>';

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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