显示自定义帖子类型的类别,但不显示所有帖子

时间:2017-12-08 作者:Lucy Brown

我的网站上有此页面-

这是一个自定义分类法模板,用于一个名为Directory Entries的自定义post类型,其分类法名为Directory\\u entry\\u type,这就是这里要引入的内容。

目前,它只是在“社区目录”类别下拉入所有帖子-这个类别有大约6个子类别,我想在这个页面上拉入,但目前它只是在社区目录下拉入所有帖子。

我该怎么做?

这就是我目前要做的-

<div id="content" class="col-md-8">



<?php 
    $queried_object = get_queried_object();
    $term_query = $queried_object->term_id;
    $current_term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );


    query_posts( array(

        \'posts_per_page\' => -1, // you may edit this number
        \'orderby\' => \'title\',
        \'order\' => \'ASC\',
        \'child_of\' => $current_term->term_id,
        \'hierarchical\' => true,
        \'depth\'  => 1,
        \'tax_query\' => array(
                array(
                \'field\' => \'term_id\',
                \'terms\' => $term_query,
                \'taxonomy\' => $current_term->taxonomy,

                    )
                ),
        \'meta_query\' => array(
            array(

                \'key\' => \'is_member\',
                \'value\' => \'1\',
                \'compare\' => \'LIKE\'
            )
        )
        )
    );



?>


<div class="grid-vw">
    <ul>        

    <?php while (have_posts()) : the_post(); ?>

        <li>

        <a href="<?php the_permalink();?>">

                <figure class="effect-goliath">
                     <?php 

                        $image = get_field(\'thumbnail\');
                        $size = \'dir-tile\'; // (thumbnail, medium, large, full or custom size)

                        if( $image ) {

                            echo wp_get_attachment_image( $image, $size );

                        }

                    ?>

                    <figcaption>
                        <h2><?php the_title();?> ></h2>
                    </figcaption>           
                </figure>

        </a>

        </li>


       <?php endwhile; 

            wp_reset_query();
        ?>      </ul>


</div>
提前感谢您的帮助!

编辑

所以我用这个解决了这个问题

<div class="grid-vw">
    <ul>        



  <?php
  // List posts by the terms for a custom taxonomy of any post type

  $post_type = \'directory_entry\';
  $tax = \'directory_entry_type\';
  $tax_args = array(
    \'order\' => \'ASC\',
    \'parent\' => 289
  );
 // get all the first level terms only
  $tax_terms = get_terms( $tax, $tax_args );
  if ($tax_terms) {
  foreach ($tax_terms as $tax_term) { // foreach first level term
  // print the parent heading
    ?>



    <li>

        <a href="<?php echo get_permalink( $tax_term->ID ); ?>">

                <figure class="effect-goliath">
                     <?php 

                        $image = get_field(\'thumbnail\');
                        $size = \'dir-tile\'; // (thumbnail, medium, large, full or custom size)

                        if( $image ) {

                            echo wp_get_attachment_image( $image, $size );

                        }

                    ?>

                    <figcaption>
                        <h2><?php echo $tax_term->name; ?> ></h2>
                    </figcaption>           
                </figure>

        </a>

           </li>



  <?php wp_reset_query();

  }

  }
  ?>


    </ul>

这正是我想要的,但现在permalinks不起作用了,有人能告诉我我做错了什么吗?

2 个回复
SO网友:Adam N

要澄清的是,您的查询是否只将帖子标记为父类别,而不将帖子标记为子类别?你是说你想把一切都写在这页上?

您可以尝试更改child_ofcat 然后摆脱depth.

但TBH您可能希望在查询时尝试不同的方法;这个问题/答案表明query_posts() 不是最好的方法:When should you use WP_Query vs query_posts() vs get_posts()?

另一种方法是查询此父级的子类别的ID,然后使用单独的post查询对其进行循环-如果您希望将其分为六个部分。

SO网友:Lucy Brown

我用这个解决了这个问题

<div class="grid-vw">
    <ul>        



 <?php
 // List posts by the terms for a custom taxonomy of any post type

 $post_type = \'directory_entry\';
 $tax = \'directory_entry_type\';
 $tax_args = array(
 \'order\' => \'ASC\',
 \'parent\' => 289
 );
 // get all the first level terms only
 $tax_terms = get_terms( $tax, $tax_args );
 if ($tax_terms) {
 foreach ($tax_terms as $tax_term) { // foreach first level term
 // print the parent heading
 ?>



 <li>

 <?php           echo \'<a href="\'.get_term_link($tax_term).\'">\';

?>

                <figure class="effect-goliath">
                     <?php 

                        $image = get_field(\'thumbnail\');
                        $size = \'dir-tile\'; // (thumbnail, medium, large, full or custom size)

                        if( $image ) {

                            echo wp_get_attachment_image( $image, $size );

                        }

                    ?>

                    <figcaption>
                        <h2><?php echo $tax_term->name; ?> ></h2>
                    </figcaption>           
                </figure>

        </a>

        </li>



 <?php wp_reset_query();

 }

 }
 ?>


    </ul>

结束

相关推荐

显示自定义帖子类型的类别,但不显示所有帖子 - 小码农CODE - 行之有效找到问题解决它

显示自定义帖子类型的类别,但不显示所有帖子

时间:2017-12-08 作者:Lucy Brown

我的网站上有此页面-

这是一个自定义分类法模板,用于一个名为Directory Entries的自定义post类型,其分类法名为Directory\\u entry\\u type,这就是这里要引入的内容。

目前,它只是在“社区目录”类别下拉入所有帖子-这个类别有大约6个子类别,我想在这个页面上拉入,但目前它只是在社区目录下拉入所有帖子。

我该怎么做?

这就是我目前要做的-

<div id="content" class="col-md-8">



<?php 
    $queried_object = get_queried_object();
    $term_query = $queried_object->term_id;
    $current_term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );


    query_posts( array(

        \'posts_per_page\' => -1, // you may edit this number
        \'orderby\' => \'title\',
        \'order\' => \'ASC\',
        \'child_of\' => $current_term->term_id,
        \'hierarchical\' => true,
        \'depth\'  => 1,
        \'tax_query\' => array(
                array(
                \'field\' => \'term_id\',
                \'terms\' => $term_query,
                \'taxonomy\' => $current_term->taxonomy,

                    )
                ),
        \'meta_query\' => array(
            array(

                \'key\' => \'is_member\',
                \'value\' => \'1\',
                \'compare\' => \'LIKE\'
            )
        )
        )
    );



?>


<div class="grid-vw">
    <ul>        

    <?php while (have_posts()) : the_post(); ?>

        <li>

        <a href="<?php the_permalink();?>">

                <figure class="effect-goliath">
                     <?php 

                        $image = get_field(\'thumbnail\');
                        $size = \'dir-tile\'; // (thumbnail, medium, large, full or custom size)

                        if( $image ) {

                            echo wp_get_attachment_image( $image, $size );

                        }

                    ?>

                    <figcaption>
                        <h2><?php the_title();?> ></h2>
                    </figcaption>           
                </figure>

        </a>

        </li>


       <?php endwhile; 

            wp_reset_query();
        ?>      </ul>


</div>
提前感谢您的帮助!

编辑

所以我用这个解决了这个问题

<div class="grid-vw">
    <ul>        



  <?php
  // List posts by the terms for a custom taxonomy of any post type

  $post_type = \'directory_entry\';
  $tax = \'directory_entry_type\';
  $tax_args = array(
    \'order\' => \'ASC\',
    \'parent\' => 289
  );
 // get all the first level terms only
  $tax_terms = get_terms( $tax, $tax_args );
  if ($tax_terms) {
  foreach ($tax_terms as $tax_term) { // foreach first level term
  // print the parent heading
    ?>



    <li>

        <a href="<?php echo get_permalink( $tax_term->ID ); ?>">

                <figure class="effect-goliath">
                     <?php 

                        $image = get_field(\'thumbnail\');
                        $size = \'dir-tile\'; // (thumbnail, medium, large, full or custom size)

                        if( $image ) {

                            echo wp_get_attachment_image( $image, $size );

                        }

                    ?>

                    <figcaption>
                        <h2><?php echo $tax_term->name; ?> ></h2>
                    </figcaption>           
                </figure>

        </a>

           </li>



  <?php wp_reset_query();

  }

  }
  ?>


    </ul>

这正是我想要的,但现在permalinks不起作用了,有人能告诉我我做错了什么吗?

2 个回复
SO网友:Adam N

要澄清的是,您的查询是否只将帖子标记为父类别,而不将帖子标记为子类别?你是说你想把一切都写在这页上?

您可以尝试更改child_ofcat 然后摆脱depth.

但TBH您可能希望在查询时尝试不同的方法;这个问题/答案表明query_posts() 不是最好的方法:When should you use WP_Query vs query_posts() vs get_posts()?

另一种方法是查询此父级的子类别的ID,然后使用单独的post查询对其进行循环-如果您希望将其分为六个部分。

SO网友:Lucy Brown

我用这个解决了这个问题

<div class="grid-vw">
    <ul>        



 <?php
 // List posts by the terms for a custom taxonomy of any post type

 $post_type = \'directory_entry\';
 $tax = \'directory_entry_type\';
 $tax_args = array(
 \'order\' => \'ASC\',
 \'parent\' => 289
 );
 // get all the first level terms only
 $tax_terms = get_terms( $tax, $tax_args );
 if ($tax_terms) {
 foreach ($tax_terms as $tax_term) { // foreach first level term
 // print the parent heading
 ?>



 <li>

 <?php           echo \'<a href="\'.get_term_link($tax_term).\'">\';

?>

                <figure class="effect-goliath">
                     <?php 

                        $image = get_field(\'thumbnail\');
                        $size = \'dir-tile\'; // (thumbnail, medium, large, full or custom size)

                        if( $image ) {

                            echo wp_get_attachment_image( $image, $size );

                        }

                    ?>

                    <figcaption>
                        <h2><?php echo $tax_term->name; ?> ></h2>
                    </figcaption>           
                </figure>

        </a>

        </li>



 <?php wp_reset_query();

 }

 }
 ?>


    </ul>

相关推荐