• Resolved Jeffro

    (@jeffr0)


    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; ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • 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!

    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.

    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

    krez

    (@krez)

    i’m getting the exact same error…

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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.

    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>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Recent Posts From Specific Category’ is closed to new replies.