tags:

views:

86

answers:

2

I've tried to code a recent posts script for my custom WP theme, however, it occurs to me that since WP ships with a recent posts widget, ideally I should just be able to call that from within my sidebar.php script, passing it the "Number of posts to show" parameter.

Anyone know how to do this?

A: 
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink('$link') ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php comments_number('0 Answers', '1 Answer', '% Answers'); ?>
<?php endwhile; ?>
tehsilverdollar
A: 

Use the query API WordPress offers: http://codex.wordpress.org/Function_Reference/WP_Query

Example:

<?php
    $myQuery = new WP_Query(
        array(
            'nopaging'    => true,
            'post_type'   => 'post',
            'post_status' => 'publish',
            'post_count'  => 5
        )
    );

    if ( $myQuery->have_posts() )
    {
        while ( $myQuery->have_posts() )
        {
            $post = $myQuery->next_post();
            ?>
    Do whatever you want …
    To test for the current page:
    <a href="<?php the_permalink(); ?>"
    <?php
    if ( $_SERVER['REQUEST_URI'] == str_replace(
            'http'
                . ( empty ( $_SERVER['HTTPS'] ) ? '' : 's' )
                . '://' . $_SERVER['HTTP_HOST'], '',
            get_permalink()
        ) )
    {
        print ' class="current"';
    }
    ?>
    ><?php the_title(); ?></a>
            <?php
        }
    }
?>
toscho
Thanks toscho. Do you have any idea how to make the script flag the current_page_item css like wp_list_pages does?
Scott B
I added this check. BUT … I strongly recommend not to link to the current page in the first place. No one has any benefit from such links. I replace all redundant links in my WP blogs with <span class="current" title="You are here">Text</span>See this article http://toscho.de/2009/deppenlink-entfernen/ (in German) for the code.
toscho