WordPress>通过自定义主题的脚本调用“Recent Posts”小部件

时间:2010-03-23 19:58:02

标签: php wordpress

我已经尝试为我的自定义WP主题编写最近的帖子脚本,但是,我发现由于WP附带了最近的帖子小部件,理想情况下我应该能够从我的sidebar.php中调用它脚本,传递“要显示的帖子数”参数。

任何人都知道怎么做?

2 个答案:

答案 0 :(得分:0)

<?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; ?>

答案 1 :(得分:0)

使用查询API WordPress提供:http://codex.wordpress.org/Function_Reference/WP_Query

示例:

<?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
        }
    }
?>