• Resolved korythewebguy

    (@korythewebguy)


    I’m sure this is a rather frequently asked question, and probably extremely simple to solve, but I can’t seem to find the answer even after scouring the forum and even trying Google.

    Essentially, I have a site with 5 main Pages, and would like each Page to show a list of Posts from a specific category on the right-hand side.

    Using the following code works fine, except that I want the Page’s content to appear after the list of Posts – but moving the_content to the end of the code then returns content from the Posts, and not the Page they’re on.

    <? the_content('Read more...'); ?>
    <div id="IOB_theInkWellBlog">
    	<?php
    		switch ($post->ID) {
    		case '3': $postslist = get_posts('category=5&numberposts=5&order=ASC&orderby=post_date'); break;
    		case '4': $postslist = get_posts('category=6&numberposts=5&order=ASC&orderby=post_date'); break;
    		case '5': $postslist = get_posts('category=7&numberposts=5&order=ASC&orderby=post_date'); break;
    		case '6': $postslist = get_posts('category=8&numberposts=5&order=ASC&orderby=post_date'); break;
    		case '7': $postslist = get_posts('category=9&numberposts=5&order=ASC&orderby=post_date'); break;
    		default: break;}
    	if ( $postslist ) {
    		foreach ($postslist as $post) :
    		setup_postdata($post);
    		echo "";?>
    	<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    		<?php the_excerpt();
    	endforeach; } ?>
    </div>

    How can I fix my code so that the_content will display content from the current Page rather than from the list of Posts?

    All comments, thoughts, criticism and help are GREATLY appreciated – Thanks in advance to all who reply!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter korythewebguy

    (@korythewebguy)

    After a lot of reading & monkeying around with code to try figuring things out myself, I’m still rather bewildered by how I can reset the_content() to pull data from the current Page rather than from the Posts retrieved by get_posts()…

    Is this a simple fix, or am I going way beyond the scope of WordPress?

    Again, I can’t say ‘Thanks!’ enough to those who offer any assistance! ??

    I always like to give those with “web guy” in their name a hard time around here, but lets not and say we did, in this case.

    the deal is this … each “view” (category/single/page/index/date archive/ yadda..) has a default query which is presented to the page template rather than performed IN the page template, you dig?

    what I mean is, you don’t have to get_posts or query_posts in order to display a simple template of the current view.

    the problem is you’re trashing the default query (in particular the default $post variable many times) with your excerpts by the time you get to displaying what SHOULD be contained in the default query later.

    you can go about this two ways.

    You can either backup your query variables before you nuke them with get_posts, or you can just pull the current page ID again later with get_posts. Up to you which you prefer.

    Backing up your query:

    before you do your get_posts magic, do this (if you have php5 it may be slightly different):

    <?php $temp_query = $wp_query; ?>

    then you do your get posts stuff. When that’s done you can:

    <?php $wp_query = $temp_query; ?>

    then execute the while loop which normally sits in that page template.

    That’s the method in the Codex, but I’ve had limited success with that, so I tend to just pull the post again, in the rare instance that I need to do a loop before the actual page.

    Pulling the page again:

    Before anything else you do, do this, so you can use the ID later:

    <?php $this_page = $post->ID; ?>

    You can then use the $this_page variable in your switch statement if you like. Either way now it’s not going to be clobbered by your excerpts. Once they’re done, you can:

    <?php
       $postslist = get_posts("post_type=page&include=$this_page");
       if ($postslist) foreach ($postslist as $post) {
          setup_postdata($post);
          the_content('Read more...');
       }

    tada!

    you also want to bookmark some codex pages, in particular
    the one about The Loop™.

    Every web guy needs to sleep with that under his pillow.

    Thread Starter korythewebguy

    (@korythewebguy)

    Thanks for the tough love Ivonic.

    I had figured something was demolishing the_content()’s default query and just wasn’t sure how to restore it, but this has me back up & going… and hey, my ego is even back in check. What can I say? You’re fantastic – Thanks tons!

    lol, cheers mate ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using the_content after get_posts on a Page’ is closed to new replies.