[英]Wordpress: Get posts by categories
我是一个wordpress的新手,正在尝试寻找好的解决方案。 从本质上讲,我得到了具有特定标记“目标”的类别列表。 完成此操作后,我也想使用上述过滤器查询所有帖子。
所以我现在所拥有的是这样的:
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
$posts = get_posts($destCategories);
但是,get_categories返回一个类别特定信息的数组,该数组不能真正用作get_posts的过滤器。 有人对如何解决这个问题有建议吗? 实际上,我可以手动遍历destCategories数组并构造一个包含所有类别名称的字符串,并将其用作get帖子中的过滤器,但是我想知道是否有更优雅的解决方案可用。
谢谢你的帮助!
get_categories()没有标签参数: http ://codex.wordpress.org/Function_Reference/get_categories#Parameters我想您可以尝试传递分类法数组,但我不知道它如何解决。 如果要按标签获取帖子,则可以使用get_posts(): http : //codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters
您可以尝试这样的事情:
$args = array(
'post_type' => 'post',
'tag' => 'destination',
);
$query = new WP_Query( $args );
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post(); ?>
<?php the_title(); ?>
<?php
endwhile;
endif;
?>
好的,所以我最终得到了这样的东西,它似乎可以工作:
$query = get_posts( $myfilter );
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
?>
<div id="main">
<div id="content clearfix">
<p>this is using front-page.php</p>
<?php //echo var_dump($destCategories) ?>
<?php
if ( have_posts( $myfilter ) ) {
foreach ( $destCategories as $category ) :
$posts = get_posts( array('category_name' => $category->slug) );
foreach ( $posts as $post ) :
?>
<h3><a href="<?php the_permalink(); ?>"> <?php echo the_title(); ?> </a></h3>
<?php
endforeach;
wp_reset_postdata();
//the_content();
endforeach;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.