0

I have a page called 'blog' and I want to have the blog pages content at the top of the page as an intro and then underneath a list of all the posts. I've run into quite a few issues I'm not sure how to get around. Here they are...

  • I have three posts at the moment, only post 1 and 3 are shown (post two isn't)
  • I'm including the excerpts of the posts but post 1 displays nothing and post 3 shows the correct excerpt of that post
  • On the search page (search.php) the same thing as above happens except that post 1 displays the content of the blog page itself as it's excerpt...?

So I was wondering how I could get around this all?

I'm using the following (I've removed the HTML/CSS so you can see the PHP better but kept the same layout of everything):

<?php wp_reset_postdata(); wp_reset_query(); ?>
<?php if (have_posts()) : ?>

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

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

         <?php the_post_thumbnail('thumbs'); ?>

      <?php endif; ?>

      <?php echo get_the_date('jS F Y'); ?>

      <?php the_title(); ?>

      <?php the_post(); ?> <?php echo substr(get_the_excerpt(), 0,200); ?>

   <?php endwhile; ?>

<?php else : ?>

<?php endif; ?>
<?php wp_reset_postdata(); wp_reset_query(); ?>

Any help on this please?

  • I'm not sure I fully understand - why would you want both the excerpt and the content of a post on a page? If you could clarify that would be great. – David Gard Feb 5 '15 at 16:09
  • Hi, Sorry I don't think I explained it very well. I'm after the content of the page (the blog page) and then the list of posts with their excerpts in a list below. The above code is for the posts list as I wanted to have that working before I tackled the blog intro as it's not as important. – user67087 Feb 5 '15 at 16:12
  • Ok, I get what you mean. Will edit my answer shortly... – David Gard Feb 5 '15 at 16:13
  • Updated my answer, give it a shot. – David Gard Feb 5 '15 at 16:39
  • Any feedback on this issue. Why are you using wp_reset_postdata() and wp_reset_query() and why do you use it above and below your loop – Pieter Goosen Feb 6 '15 at 6:40
1

Edited answer

Thank you for you clarification above, and please give this a try -

Note - This code assumes that you are using page.php, a variation of the page templage or some other custom template - it will not work on your index page (index.php).

<div id="page-<?php the_ID(); ?>">
<?php
if (have_posts()) : while (have_posts()) : the_post();

        if ( has_post_thumbnail() ) :
            the_post_thumbnail('thumbs');
        endif;


        echo get_the_date('jS F Y');

        the_title();

        the_content();

    endwhile;
endif;
?>
</div>

<?php
/** Query the list of posts to display */
$args = array(
    'post_type'     => 'post',
    'post_status'   => 'publish'
);
$posts_query = new WP_Query($args);
?>

<div id="post-list">
<?php
if($posts_query->have_posts()) : while ($posts_query->have_posts()) : $posts_query->the_post();
?>
        <div id="<?php the_ID(); ?>" <?php post_class(); ?>>

            <h1 class="post-title">
                <a href="<?php the_permalink(); ?>" title="View post '<?php the_title_attribute(); ?>'">
                    <?php the_title(); ?>
                </a>
            </h1>

            <p class="post-content">
                <?php echo substr(get_the_excerpt(), 0,200); ?>
            </p>

        </div>
<?php
    endwhile;

    wp_reset_postdata();

endif;
?>
</div>

Before/after the main Loop you have no need of the wp_reset_postdata() and wp_reset_query() fucntions at all.

However, after the secondary Loop to list the posts, just before the final endif; you'll need just wp_reset_postdata().

You'll see that I've added some HTML in the secondary Loop to give you an idea of how to do this - obviously you'll need to do somehting similar to the primary Loop so that you can apply styling.

|improve this answer|||||
  • Thank you very much for this! My only issue now is that it seems to be forcing index.php. I went into the Reading section in the ACP and made the posts page/list as 'Blog' and then in the edit page section for the page 'Blog' changed the template to template_blog.php but it's still using index.php...? :S – user67087 Feb 5 '15 at 16:46
  • 1
    wp_reset_query() here is wrong. It is used with query_posts which you should never use. If you are using WP_Query you should make use of wp_reset_postdata() – Pieter Goosen Feb 6 '15 at 6:45
  • 1
    @user67087 if this answer solved this issue, then you should accept this answer by clicking on the checkmark next to this answer. The checkmark will go green. The issue that you have with the templates does not have any relevance to this question and you should start a new question as such with this new issue. This is not a discussion forum, but a simple Q&A which have a one issue per post policy. Two issues mean two questions. Thank you – Pieter Goosen Feb 6 '15 at 6:49
  • @PieterGoosen - Of course, I'm getting my functions mixed up. I have updated the answer to reflect this. – David Gard Feb 6 '15 at 9:46
  • I'm still stuck with this because even though I've said to use the blog page as the page with a list of posts, it's still using the index.php file even though I've set the template to temaplate_blog.php and selected that. It seems to be ignoring the template that has been set for the blog page... :/ – user67087 Feb 6 '15 at 10:36
0

before your code, try and add:

<?php 
if( is_home() && get_option( 'page_for_posts' ) ) {
  $posts_page = get_post( 'get_option( 'page_for_posts' ) );
  echo apply_filters( 'the_content', $posts_page->post_content );
} 
?>

http://codex.wordpress.org/Function_Reference/is_home http://codex.wordpress.org/Option_Reference#Reading http://codex.wordpress.org/Function_Reference/apply_filters

|improve this answer|||||

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.