• Resolved jwesseldyke

    (@jwesseldyke)


    I’d like to add the most recent post from one specific category on a page. I had done this some years back, but I know a lot has changed recently with the newer versions of WP…

    Would someone be willing to post a snippet for me?

    Your help is greatly appreciated…
    J

Viewing 7 replies - 1 through 7 (of 7 total)
  • See the Codex on The Loop, especially the section ‘Multiple Loops in Action’.

    Thread Starter jwesseldyke

    (@jwesseldyke)

    Thanks for the link to the right section of the codex…

    As I’m not much of a coder, I took the snippet and changed the category name using the slug to grab a post from a specific category.

    I must have it in the wrong place because I either end up with the post and not the content of the “page” or the content of the page and not the post entry…

    Do you have two Loops now? Can you post the section of code involved and a link to your site?

    Thread Starter jwesseldyke

    (@jwesseldyke)

    OK… Here’s the relevant snippet… I’m working with a template file, not the main page.php, if that’s relevant I’m not sure…

    <?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    
    				<h1><?php the_title(); ?></h1>
    
    <table width="100%" cellpadding="5" cellspacing="0" border="1">
     <tr>
      <td>static content on the page</td></tr></table>
    
    				<div class="entry">
    					<?php the_content('Read the rest of this entry &raquo;'); ?>
    
    				</div>
    			</div>
    
    		<?php endwhile; ?>

    The snippet from the codex isn’t here, but if I put it in after the endwhile it doesn’t pull the post, if I put it above, the post pulls in but not the static content I typed in on the edit/create page.

    Seems like I didn’t use query’s before, it was just a one line snippet that looked for x number of posts from a category… all this query stuff is beyond my comprehension… if it matters, I’m using php5.

    This is TOTALLY UNTESTED, but it should be close. Put this part just ahead of the code you posted above. It is the first loop (while loop), your code above is the second loop.

    <?php $my_query = new WP_Query('category_name=featured&posts_per_page=1');
       while ($my_query->have_posts()) : $my_query->the_post();
          $do_not_duplicate = $post->ID;?>
          <div class="post" id="post-<?php the_ID(); ?>">
             <h1><?php the_title(); ?></h1>
             <table width="100%" cellpadding="5" cellspacing="0" border="1">
                <tr>
                   <td>static content on the page</td></tr></table>
             <div class="entry">
                <?php the_content('Read the rest of this entry &raquo;'); ?>
             </div>
          </div>
       <?php endwhile; ?>

    Then put this line just after the line containing ‘while (have_posts())’ in your code:

    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

    Thread Starter jwesseldyke

    (@jwesseldyke)

    Thanks so much for all your help, I sincerely appreciate it.
    J

    If that fixed the problem, please mark this topic Resolved.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘posts from category on a page’ is closed to new replies.