首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Wordpress进行自定义分类过滤

使用Wordpress进行自定义分类过滤
EN

Stack Overflow用户
提问于 2013-10-21 17:30:01
回答 3查看 11.3K关注 0票数 2

我已经尝试了三天来解决这个问题,甚至在这个网站上使用了解决方案。我还是不能让它工作。

我有一个wordpress循环,它使用过滤器来按帖子类型显示帖子。现在,帖子类型被称为“案例研究”,因此类型案例研究中的所有帖子都会显示出来。

但是我需要在这个循环中隐藏一个特定的分类法术语。分类被称为“行业”,术语是“医疗保健”。我尝试了所有的组合,但仍然不能得到这个。我需要这个非常紧急的东西。任何能帮忙的人都会救我的命。

下面是查询和循环

代码语言:javascript
复制
<?php
// The Query
$the_query = new WP_Query( 'post_type=case-studies&posts_per_page=-1' );

// The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post(); 


?>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-21 17:43:35

代码语言:javascript
复制
    $args = array(
    'post_type' => 'case-studies',
    'tax_query' => array(
        array(
            'taxonomy' => 'sectors',
            'field' => 'slug',
            'terms' => array('comercial', 'personal', 'etc') //excluding the term you dont want.
        )
    )
);
$query = new WP_Query( $args );

我不想尝试,但你可以只做一个查询调用你想要的术语,你可以以前填充terms数组,列出分类上的所有术语,并排除你想要的,我认为这有点老生常谈它应该是另一种直接的方法,但尝试一下,因为这是一个生死存亡的案例)。

来源:http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

票数 3
EN

Stack Overflow用户

发布于 2013-10-21 18:08:11

试试这个:

代码语言:javascript
复制
<?php
$type = 'cpreviews';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
票数 1
EN

Stack Overflow用户

发布于 2013-10-21 21:36:58

好的,然后试试这个,它会自动提取你的词条,并排除你不想要的词条,我没有检查它是否工作,但这是逻辑,请检查sintax错误。

代码语言:javascript
复制
$terms = get_terms("sectors");
$count = count($terms);
$termsAr = array();
if ($count > 0 ){
    foreach ( $terms as $term ) {
        if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
            array_push($termsAr, $term->name);
        }
    }
}

$terms = get_terms("types");
$count = count($terms);
$termsAr2 = array();
if ($count > 0 ){
    foreach ( $terms as $term ) {
        if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
            array_push($termsAr2, $term->name);
        }
    }
}
 $args = array(
    'post_type' => 'case-studies',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'sectors',
            'field' => 'slug',
            'terms' => $termsAr //excluding the term you dont want.
        ),
        array(
            'taxonomy' => 'types',
            'field' => 'slug',
            'terms' => $termsAr2 //excluding the term you dont want.
        )
    )
);
$query = new WP_Query( $args );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19490762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档