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?
<?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; ?>
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
}
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.