WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_recent_posts (3 posts)

  1. aliwebdesarrollo
    Member
    Posted 8 months ago #

    Hi there, i'm making my own theme and i have a little problem with one function.

    I just want to show the last 5 post (publish post), without taking in count the category. So when the visitor goes to a category at the sidebar could see the last X (could be 5 or 10) last post published.

    To make that i'm using the function wp_get_recent_posts but there is a problem apparently because doesnt work. It crash when try to load the file sidebar.php with that function. And i know that the problem is wp_get_recent_posts because if i use a loop with query_post works fine (but i dont want to use this function!).

    I paste here the code, its almost the same that it s in the Codex page.

    <ul id="LastPostList">
    <?php
    $args = array(
        'numberposts' => 5,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
        'post_status' => 'publish',
        'suppress_filters' => true );
    
        $recent_posts = wp_get_recent_posts( $args, $output = ARRAY_A );
        foreach ($recent_posts as $recent)
        {
        	?>
    	<li class="LastPost"><a href="<?php get_permalink({$recent['ID']}) ?>" title="<?php esc_attr({$recent['post_title']})?>" ><?=$recent["post_title"]?></a></li>
        	<?php
        }
    ?>
    
    </ul>

    Maybe there is something that i cant see... i also try to enable the debug option but didnt shows anything, and the server error file nether.

  2. graphical_force
    Member
    Posted 8 months ago #

  3. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    caused by the curly brackets you have in your code which are not in the Codex examples;

    change the one line to:

    <li class="LastPost"><a href="<?php get_permalink($recent['ID']) ?>" title="<?php echo esc_attr($recent['post_title']); ?>" ><?php echo $recent['post_title']; ?></a></li>

    to get error messages while developing your site, consider to enable DEBUG http://codex.wordpress.org/Debugging_in_WordPress

Reply

You must log in to post.

About this Topic