首页 文章

使用Wordpress过滤自定义分类

提问于
浏览
2

我一直试图解决这个问题三天,甚至在这个网站上使用解决方案 . 我仍然无法让这个工作 .

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

但是我需要从这个循环中隐藏一个特定的分类术语 . 分类法称为“部门”,术语称为“医疗保健” . 我尝试了各种组合,但仍然无法得到这个 . 我需要这个非常紧急 . 任何可以帮助的人都会挽救我的生命 .

这是查询和循环

<?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(); 


?>

3 回答

  • 1
    $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 );
    

    我不尝试它,但你只能做一个只调用你想要的术语的查询,你以前可以填充术语数组列出分类法上的所有术语并排除你想要的那些,我认为这有点hacky它应该是另一种直接的方式,但尝试一下,因为它是一个生命或死亡的情况=) .

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

  • 2

    试试这个:

    <?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().
    ?>
    
  • 0

    好吧然后尝试这一个它将提取你的条款dinamicaly并排除你不想要的术语,我没有检查它是否工作,但这是逻辑,请检查sintax错误 .

    $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 );
    

相关问题