[英]wp-query category and has tag
I am trying to query posts in wordpress using wp_query. 我正在尝试使用wp_query在wordpress中查询帖子。 I would like to place the posts in a category and query by using has_tag. 我想将帖子放在一个类别中,并使用has_tag进行查询。 I tried to use 我尝试使用
$args = array (
'post_type' => array( 'post' ),
'category_name' => 'footer',
);
// The Query
$social = new WP_Query( $args );
// The Loop
if ( $social->have_posts()&&has_tag('social') ) {
while ( $social->have_posts() ) {
$social->the_post();
the_content();
}
} rewind_posts();
?>
but this loads all of the posts and doesn't just show the one with the tag. 但这会加载所有帖子,而不仅仅是显示带有标签的帖子。
The correct way to do this would be limit the WP_Query itself. 正确的方法是限制WP_Query本身。
Eg 例如
$args = array(
'post_type' => array( 'post' ),
'category_name' => 'footer',
'tag' => 'social'
);
$social = new WP_Query( $args );
if ( $social->have_posts() ) {
while ( $social->have_posts() ) {
$social->the_post();
the_content();
}
}
wp_reset_postdata();
To use has_tag you'd need to set your global post data up first, eg setup_postdata( $social ), which would create a lot of overhead with conditions and loops just to filter your results down when you could do it within the query itself. 要使用has_tag,您需要首先设置全局发布数据,例如setup_postdata($ social),这会产生大量的条件和循环开销,只是在您可以在查询本身中进行过滤时才过滤掉结果。
you need to check within each post called rather than at the start (note you might need to pass in the 2nd arg of has_tag, the post object. 您需要在每个调用的帖子中进行检查,而不是在开始时进行检查(请注意,您可能需要传递has_tag的第二个参数,即post对象。
if ( $social->have_posts() ) {
while ( $social->have_posts() ) {
$social->the_post();
if( has_tag('social') {
the_content();
}
}
} rewind_posts();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.