使用 wp_get_recent_posts

发布于 2024-11-15 17:08:11 字数 520 浏览 0 评论 0原文

我正在编写自己的主题,在侧边栏中我想列出带有特定标签(“特色”)的三个帖子的详细信息。最初我尝试了这个:

$args = array(
    'posts_per_page' => 3,
    'tag' => 'featured'); 

$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $post ){
    ...
}

但这不起作用,相反我只得到了一个没有标签的帖子(在这种情况下是最新的帖子)。

我也尝试过使用 numberposts,只是尝试列出带有标签的帖子,指定更多参数,并尝试定位类别而不是标签,但这些都对我不起作用,结果范围在没有列表,只有一篇帖子之间,有时是全部。

理想情况下,我想继续使用 wp_get_recent_posts,因为它要简单得多,而且绝对是适合这项工作的函数。因此,我想将这个问题具体说明为什么我无法正确使用该函数,而不是使用 get_posts 或更直接查询的替代解决方案。

I am writing my own theme, and in the sidebar I want to list details on three posts with a specific tag ('featured'). Initially I tried this:

$args = array(
    'posts_per_page' => 3,
    'tag' => 'featured'); 

$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $post ){
    ...
}

But that didn't work, instead I got only a single post, that didn't have the tag (the most recent post in this case).

I have also tried it with using numberposts, just trying to list posts with the tag, specifying more parameters, and trying to target categories rather than tags, none of which has worked for me, results ranging between getting no listing, just the one post, and sometimes all of them.

Ideally I'd like to stay using wp_get_recent_posts as it is a lot simpler and is definitely the right function for the job. As a result I'd like to keep this question specific to why I'm failing to use the function properly, rather than alternative solutions using get_posts or querying more directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神也荒唐 2024-11-22 17:08:11

我遇到了同样的问题,尽管我并没有尝试通过标签进行限制。这是我修复它的方法。查看 wp_get_recent_posts 的实际函数签名(位于 /wp-includes/post.php 中),至少在我的 WordPress 版本中显示:

function wp_get_recent_posts($num = 10)

当然,这与 WordPress 的不同法典说。但是当我使用 wp_get_recent_posts(5) 调用该函数时,我实际上获得了 5 个最新帖子。

所以看起来你想要用这个函数做的事情是不可能的。

I had the same problem, although I wasn't trying to limit by tag. Here's how I fixed it. Looking at the actual function signature for wp_get_recent_posts (which is in /wp-includes/post.php), at least in my version of Wordpress, shows:

function wp_get_recent_posts($num = 10)

This is, of course, different from what the Wordpress Codex says. But when I called the function with wp_get_recent_posts(5), I actually got the 5 most recent posts.

So it doesn't look like what you wanted to do is possible with this function.

酒解孤独 2024-11-22 17:08:11

可能并不容易,因为函数参考不显示任何标签参数:
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

可能需要使用 is_tag 进一步选择: http://codex.wordpress.org/Function_Reference/is_tag

Might not be easy, as the function reference doesn't show any tag parameters:
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

Might have to further select with is_tag: http://codex.wordpress.org/Function_Reference/is_tag

白首有我共你 2024-11-22 17:08:11

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

}

In WP 3.9.2 i have this working function:

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;

}

弥枳 2024-11-22 17:08:11

好吧,由于似乎没有答案,我使用 get_posts 编写了一个解决方案,其工作方式完全相同。但我仍然不知道 wp_get_recent_posts 做错了什么。

Ok, since there doesn't seem to be an answer, I wrote a solution using get_posts that works in exactly the same way. I still don't know what I was doing wrong with wp_get_recent_posts though.

金橙橙 2024-11-22 17:08:11

是的!您可以在传递给 wp_get_recent_posts() 的属性中使用 tag 参数和标签段来过滤该标签。这似乎完全没有记录。例子:

$args = array('tag' => 'my-tag-slug');
$recent_posts = wp_get_recent_posts($args);

Yes! You can use a tag parameter with a tag slug in the attributes passed to wp_get_recent_posts() to filter on that tag. This seems to be entirely undocumented. Example:

$args = array('tag' => 'my-tag-slug');
$recent_posts = wp_get_recent_posts($args);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文