1

I'm trying to create a "Start here" page on my blog. I use a template for that, but the content for it is created using the "add new page" wizard in WordPress.

I know how I can display the most discussed posts (with most comments), but PHP doesn't work in pages. Of course I can just paste the code into the page template instead, but look at my template:

<?php
/*
  Template Name: Test Layout
*/
?>
<?php get_header(); ?>
<div id="start-here-wrapper">
    <ul>
        <?php
        $pc = new WP_Query( 'orderby=comment_count&ignore_sticky_posts=1&posts_per_page=6' );
        while ( $pc->have_posts() ) : $pc->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php 
                    the_title(); ?>"><?php the_title(); ?></a>
            </li>
        <?php endwhile; ?>
    </ul>
    <?php the_content(); ?>
</div>
<?php get_footer(); ?>

I use "the content" so that everything I write via the "page wizard" will be in there. I don't want to copy and paste everything into the template itself for various reasons, so I need to find a way to make this work with the code above.

If I call the popular posts below "the content", it works without any problem. If I call it before it (like in the code above), the actual content of my page won't show up and instead the last blog post in the list of popular posts will be displayed.

  • You could add the code to a separate file and use get_template_part() – Brad Dalton Aug 3 '14 at 23:33
1

If I call the popular posts below "the content" it works without any problem. If I call it before it (like in the code above), the actual content of my page won't show up and instead the last blog post in the list of popular posts will be displayed.

If I understand you, that is because the_content depends upon the global $post value, which is set very early in the page load but then reset by the the_post method in your Loop. Early in the page it is set to your page's content. The secondary loop then overwrites it at each iteration. After the Loop completes $post is populated with the last post in the Loop.

If you need the original post data after you run that Loop you should be using wp_reset_postdata. That should reset $post back to the current post in the main query. In your case that would be your page content.

Also, you should not be running the_content outside of a proper Loop. It is meant to run inside the Loop will not always behave as you expect otherwise.

| improve this answer | |
1

s_ha_dum has said it all.

    <?php
    /**
     * Template Name: Test Layout
     *
     */
    ?>
    <?php get_header(); ?>

    <div id="start-here-wrapper">
    <?php while ( have_posts() ) : the_post(); ?>
            <ul>
                <?php
                $pc = new WP_Query('orderby=comment_count&ignore_sticky_posts=1&posts_per_page=6'); ?>
                <?php while ($pc->have_posts()) : $pc->the_post(); ?>
                    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                <?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
            </ul>
        <?php the_content(); ?>
    <?php endwhile; ?>
    </div>

    <?php get_footer(); ?>
| 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.