WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Recent Posts From Specific Category (8 posts)

  1. I've searched the forums and Google high and low and it seems as though I can't find a solution to this problem. I am currently using a block of code that shows the most recent 5 posts to my blog. Can anyone show me what they would change in this block of code which would allow me to show the 5 recent articles from a specific category?

    <? $today = current_time('mysql', 1);
    if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 0, 5")):
    		?><h2>Recent Articles:</h2>
    		<ul id="category_articles">
    			<?php foreach ($recentposts as $post) { if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
    			echo "<li><a href='".get_permalink($post->ID)."'>"; the_title(); echo"</a><small>"; the_date(); echo "</small></li>"; } ?>
    		</ul>
    				<?php endif; ?>
  2. MichaelH
    Member
    Posted 6 years ago #

  3. lloydarmbrust
    Member
    Posted 6 years ago #

    You actually don't need a plugin for this as the WordPress Codex includes this feature.

    Just use the following code:

    <ul>
     <?php
     global $post;
     $myposts = get_posts('numberposts=5&offset=1&category=1');
     foreach($myposts as $post) :
     setup_postdata($post);
     ?>
    
    <li><a>"><?php the_title(); ?></a> --- <?php the_excerpt(); ?></li>
     <?php endforeach; ?>
     </ul>

    If you don't want it to post a digest, than use this code:

    <ul>
     <?php
     global $post;
     $myposts = get_posts('numberposts=5&offset=1&category=1');
     foreach($myposts as $post) :
     setup_postdata($post);
     ?>
    
    <li><a>"><?php the_title(); ?></a>
     </li>
    </ul>

    I didn't make this up, the smart dudes at WordPress and nerds from around the world did. I always suggest searching the WordPress documentation before relying on a plug in--those things are so annoying to update!

  4. lloydarmbrust
    Member
    Posted 6 years ago #

    I should also mention that you need to change the category=1 to = the category ID number of your desire.

    I'd really like to know how to make the category ID generate automatically. As in, if you added this code to a specific page within a category, it would pull that category ID and display more posts related to that category. I've been trying to figure this out, but apparently my PHP is lacking.

  5. Roar
    Member
    Posted 6 years ago #

    The second bit of code lloyd provides returns an error?

    parse error: syntax error, unexpected $end in /home/ddddd/public_html/dddd/blog/wp-content/plugins/exec-php/runtime.php(41) : eval()'d code on line 11

  6. krez
    Member
    Posted 6 years ago #

    i'm getting the exact same error...

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    plugins/exec-php

    The given bits of code above will not work using the ExecPHP plugin. They're meant to be used in a template.

    Don't use PHP Execution plugins. Avoid them if possible. They are bad hacky ways to solve problems, at best.

  8. bnylus
    Member
    Posted 6 years ago #

    The error for the 2nd code example is because the foreach is not closed.

    Add this to the end and it should work.
    <?php endforeach; ?>

    So the whole thing should look like:

    <ul>
    <?php
     global $post;
     $myposts = get_posts('numberposts=5&offset=1&category=3');
     foreach($myposts as $post) :
     setup_postdata($post);
     ?>
    
    <li><?php the_title(); ?> </li>
    <?php endforeach; ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic