使用wp_get_recent_posts

问题描述

| 我正在编写自己的主题,并且在边栏中我想列出三个带有特定标签(\'featured \')的帖子的详细信息。最初我尝试过:
$args = array(
    \'posts_per_page\' => 3,\'tag\' => \'featured\'); 

$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $post ){
    ...
}
但这没有用,相反,我只有一条帖子,没有标签(本例中为最新帖子)。 我也尝试过使用数字帖子,只是尝试列出带有标签的帖子,指定更多参数,并尝试定位类别而不是标签,但这些标签对我都不起作用,结果在没有列表的情况下,只有一篇帖子,有时是全部。 理想情况下,我想继续使用wp_get_recent_posts,因为它要简单得多,而且绝对是适合该工作的功能。因此,我想针对这个问题,具体说明为什么我无法正确使用该功能,而不是使用get_posts或直接查询的替代解决方案。     

解决方法

        我也有同样的问题,尽管我没有试图通过标签来限制。这是我的解决方法。至少在我的Wordpress版本中,查看ѭ1的实际功能签名(位于/wp-includes/post.php中)显示:
function wp_get_recent_posts($num = 10)
当然,这与Wordpress Codex所说的有所不同。但是,当我用
wp_get_recent_posts(5)
调用函数时,我实际上得到了5个最新职位。 因此,使用此功能似乎无法实现您想做的事情。     ,        可能并不容易,因为函数参考未显示任何标签参数: http://codex.wordpress.org/Function_Reference/wp_get_recent_posts 可能需要使用is_tag进行进一步选择:http://codex.wordpress.org/Function_Reference/is_tag     ,        在WP 3.9.2中,我具有以下工作功能:
function posts_by_tag($tag,$numberposts = 0) {

    $args = array( \'numberposts\' => $numberposts,\'post_status\' => \'publish\',\'tag\' => $tag );
    $recent_posts = wp_get_recent_posts( $args );

    foreach( $recent_posts as $recent ){
            $posts = $posts . \'<a href=\"\' . get_permalink($recent[\"ID\"]) . \'\">\'
                    . $recent[\"post_title\"]
                    . get_the_post_thumbnail($recent[\"ID\"],\"full\")
                    . \'</a>\';
    }
    return $posts;
}     ,        好的,因为似乎没有答案,所以我使用get_posts编写了一种以完全相同的方式工作的解决方案。我仍然不知道wp_get_recent_posts在做什么。     ,        是!您可以在传递给
wp_get_recent_posts()
的属性中使用带有标签slug的
tag
参数来对该标签进行过滤。这似乎完全没有记录。例:
$args = array(\'tag\' => \'my-tag-slug\');
$recent_posts = wp_get_recent_posts($args);