Wordpress:尝试按标签获取帖子 [英] Wordpress: trying to get posts by tag

查看:31
本文介绍了Wordpress:尝试按标签获取帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一些代码,可以自动创建一些帖子并为它们添加标签.我可以在所有帖子"管理面板中看到标签,我可以点击帖子标签"链接以获取带有标签的帖子.

I've written some code which automatically creates some posts and adds a tag to them. I can see the tags in the 'All posts' admin panel and I can click on the posts 'Tag' link to get just those posts with the tags.

然而,在我使用 $wp_query 编写的插件中,无论我传入什么参数,我都会得到完整的帖子列表,无论它们是否有我正在寻找的标签.

However, in a plugin that I'm writing using $wp_query no matter what parameters I pass in, I just get the complete list of posts back whether they have the tag that I am looking for or not.

这是我的代码:

// Now retrieve all items matching this brand name . . .
$query=new WP_Query(array('posts_per_page=5', array('tag' => array($brand_name))));

// The Loop
while ( $query->have_posts() ) : $query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

当我告诉它只返回 5 个结果时,这会产生 10 个结果.实际上我应该只返回 2 个帖子,因为这是带有标签的总数.

This produces 10 results when I've told it only to return 5. In reality I should only get 2 posts back as that's the total number with the tag.

在网上环顾四周,似乎有很多人遇到了同样的问题,但没有解决方案.我必须尝试过大约 10 种不同的方式来指定标签,但返回的帖子数量错误这一事实表明我要么完全错误,要么存在某种错误.如果有帮助,Wordpress 版本是 3.4.1.

Looking around on the web there seems to be a lot of people having the same problem but no solutions. I must have tried about 10 different ways of specifying the tag but the fact that the number of posts returned is wrong suggests I've either got something completely wrong or there is some kind of bug. Wordpress version is 3.4.1 if it helps.

任何 Wordpress 专业人士都可以对此进行说明吗?

Can any Wordpress pro's shed light on this ?

提前致谢!

推荐答案

试试这个

$original_query = $wp_query;
$wp_query = null;
$args = array('posts_per_page' => 5, 'tag' => $brand_name);
$wp_query = new WP_Query($args);

if (have_posts()) :
    while (have_posts()) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
endif;

$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();

这篇关于Wordpress:尝试按标签获取帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆