按分类列出自定义帖子类型中的所有帖子

时间:2012-09-25 作者:Dean Elliott

是否有一种方法可以列出特定自定义帖子类型中的所有帖子,并按其附带的自定义分类术语进行排列?

例如

Taxonmy Term #1
岗位类型
岗位类型
岗位类型

Taxonomy Term #2
岗位类型
岗位类型

任何帮助都将不胜感激。

谢谢

5 个回复
最合适的回答,由SO网友:GhostToast 整理而成

试试这个

$custom_terms = get_terms(\'custom_taxonomy\');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array(\'post_type\' => \'custom_post_type\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'custom_taxonomy\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo \'<h2>\'.$custom_term->name.\'</h2>\';

        while($loop->have_posts()) : $loop->the_post();
            echo \'<a href="\'.get_permalink().\'">\'.get_the_title().\'</a><br>\';
        endwhile;
     }
}
我们获取分类法的所有术语,循环浏览它们,并向属于该术语的每篇文章发送标题链接。如果需要对分类术语进行重新排序,可以使用插件轻松完成。Reorder Taxonomy, 我相信。But 请注意,此插件添加了(!)激活时,表格中的另一列does not remove it upon deactivation!

SO网友:theMojoWill

这不是一个特别优雅的解决方案,但您可以为每个特定术语创建多个查询,然后输出它们。希望有人能想出一种更好的方法来自动提取术语来修改输出/排序。但这会让你走的。

<?php

//First Query for Posts matching term1
$args = array(
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'taxonomy_1\',
            \'field\' => \'slug\',
            \'terms\' => array( \'term1\' )
        ),
    ),
    \'post_type\' => \'my-post-type\'
);
$query = new WP_Query( $args );

if ( have_posts() ) {

    $term = $query->queried_object;

    echo \'All posts found in \' . $term->name;

    while ( have_posts() ) : the_post();
        //Output what you want
        the_title();
        the_content();
    endwhile;
}

//RESET YOUR QUERY VARS
wp_reset_query();

//Second Query for term2
$args = array(
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'taxonomy_1\',
            \'field\' => \'slug\',
            \'terms\' => array( \'term2\' )
        ),
    ),
    \'post_type\' => \'my-post-type\'
);
$query = new WP_Query( $args );

if ( have_posts() ) {

    $term = $query->queried_object;

    echo \'All posts found in \' . $term->name;

    while ( have_posts() ) : the_post();
        //Output what you want
        the_title();
        the_content();
    endwhile;
}

SO网友:Niraj Kumar

很好的一个!戈斯通的解决方案正是我一直在寻找的。在我的情况下,自定义帖子类型是“minining\\u contractions”,与此相关的自定义分类是“contraction types”,下面有多个术语。我的想法是创建一个自定义小部件,以显示此自定义分类法中术语下的帖子列表。在我的试运行中,它得到了我想要的。其余的都打扮得漂漂亮亮的。这是我的代码:

function fn_get_list_of_mining_accident_types()
{
    $custom_taxonomy=\'accident-types\';  
    $custom_terms = get_terms($custom_taxonomy);    
    $str_return=\'<ul>\';
    foreach($custom_terms as $custom_term) 
    {
        wp_reset_query();
        $args = array(
            \'post_type\' => \'minining_accidents\',
            \'tax_query\' => array(               
                array(
                    \'taxonomy\' => $custom_taxonomy,
                    \'field\' => \'slug\',
                    \'terms\' => $custom_term->slug,
                ),
            ),
        );  

        $loop = new WP_Query($args);

        $term_name=$custom_term->name;
        $term_slug=$custom_term->slug;
        $term_link=get_term_link($term_slug, $custom_taxonomy);

        $str_return.=\'<li><a href="\'.$term_link.\'">\'.$term_name.\'</a>\';

        if($loop->have_posts()) 
        {
            $str_return.=\'<ol>\';

            while($loop->have_posts()) : $loop->the_post();
                $str_return.=\'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li> \';
            endwhile;

            $str_return.=\'</ol>\';           
         }
         $str_return.=\'</li>\';
    }
    $str_return.=\'</ul>\';
    return $str_return;
}
是的!始终存在进一步改进代码的选项。

SO网友:Akashxolotl

这是我在开始这篇文章之前尝试的一个很长的解决方案。希望这能帮助别人更好地理解。

        <?php
    // Get list of all taxonomy terms  -- In simple categories title
    $args = array(
                \'taxonomy\' => \'project_category\',
                \'orderby\' => \'name\',
                \'order\'   => \'ASC\'
            );
    $cats = get_categories($args);

    // For every Terms of custom taxonomy get their posts by term_id
    foreach($cats as $cat) {
        ?>
        <a href="<?php echo get_category_link( $cat->term_id ) ?>">
            <?php echo $cat->name; ?> <br>
            <?php // echo $cat->term_id; ?> <br>
        </a>


            <?php
                // Query Arguments
                $args = array(
                    \'post_type\' => \'portfolio\', // the post type
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'project_category\', // the custom vocabulary
                            \'field\'    => \'term_id\',          // term_id, slug or name  (Define by what you want to search the below term)    
                            \'terms\'    => $cat->term_id,      // provide the term slugs
                        ),
                    ),
                );

                // The query
                $the_query = new WP_Query( $args );

                // The Loop
                if ( $the_query->have_posts() ) {
                    echo \'<h2> List of posts tagged with this tag </h2>\';

                    echo \'<ul>\';
                    $html_list_items = \'\';
                    while ( $the_query->have_posts() ) {
                        $the_query->the_post();
                        $html_list_items .= \'<li>\';
                        $html_list_items .= \'<a href="\' . get_permalink() . \'">\';
                        $html_list_items .= get_the_title();
                        $html_list_items .= \'</a>\';
                        $html_list_items .= \'</li>\';
                    }
                    echo $html_list_items;
                    echo \'</ul>\';

                } else {
                    // no posts found
                }

                wp_reset_postdata(); // reset global $post;

                ?>

    <?php } ?>

SO网友:wpdevramki

显示自定义分类法中的自定义帖子列表

$args = array(
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'your-custom-taxonomy\',
            \'field\' => \'slug\',
            \'terms\' => array( \'your-term\' )
        ),
    ),
    \'post_type\' => \'your-post-type\'
);
$loop = new WP_Query($args);
     if($loop->have_posts()) {
    $term = $wp_query->queried_object;
     while($loop->have_posts()) : $loop->the_post();
        //Output what you want      
   echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
      endwhile;
}
标题

列表项列表项

结束

相关推荐