获取自定义分类下的帖子

时间:2014-10-16 作者:Parth Kumar

我没有在自定义分类下获取帖子(fabric_building_types). 我正在cat_idcat->name 但也无法获得帖子。

$args = array(
    \'type\'                     => \'post\',
    \'child_of\'                 => 0,
    \'parent\'                   => \'\',
    \'orderby\'                  => \'name\',
    \'order\'                    => \'ASC\',
    \'hide_empty\'               => 1,
    \'hierarchical\'             => 1,
    \'exclude\'                  => \'\',
    \'include\'                  => \'\',
    \'number\'                   => \'\',
    \'taxonomy\'                 => \'fabric_building_types\',
    \'pad_counts\'               => false 
); 

$categories = get_categories( $args );

foreach ( $categories as $cat ) {

    // here\'s my code for getting the posts for custom post type

    $posts_array = get_posts(
        array(
            \'showposts\' => -1,
            \'post_type\' => \'fabric_building\',
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'fabric_building_types\',
                    \'field\' => $cat->cat_ID,
                    \'terms\' => $cat->name,
                )
            )
        )
    );
    print_r( $posts_array ); 
}
谁能帮帮我吗。。。提前感谢

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

您的税务查询不正确,field 应该是要查询的字段:term_id, name, 或slug -

$posts_array = get_posts(
    array(
        \'posts_per_page\' => -1,
        \'post_type\' => \'fabric_building\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'fabric_building_types\',
                \'field\' => \'term_id\',
                \'terms\' => $cat->term_id,
            )
        )
    )
);

SO网友:Courtney Ivey

您应该使用get_terms() 在可能的情况下进行分类。

<?php 
/* Add your taxonomy. */
$taxonomies = array( 
    \'fabric_building_types\',
);

$args = array(
    \'orderby\'           => \'name\', 
    \'order\'             => \'ASC\',
    \'hide_empty\'        => true, 
    \'exclude\'           => array(), 
    \'exclude_tree\'      => array(), 
    \'include\'           => array(),
    \'number\'            => \'\', 
    \'fields\'            => \'all\', 
    \'slug\'              => \'\', 
    \'parent\'            => \'\',
    \'hierarchical\'      => true, 
    \'child_of\'          => 0, 
    \'get\'               => \'\', 
    \'name__like\'        => \'\',
    \'description__like\' => \'\',
    \'pad_counts\'        => false, 
    \'offset\'            => \'\', 
    \'search\'            => \'\', 
    \'cache_domain\'      => \'core\'
); 

$terms = get_terms( $taxonomies, $args );
foreach ( $terms as $term ) {

// here\'s my code for getting the posts for custom post type

$posts_array = get_posts(
                        array( \'showposts\' => -1,
                            \'post_type\' => \'fabric_building\',
                            \'tax_query\' => array(
                                array(
                                \'taxonomy\' => \'fabric_building_types\',
                                \'field\' => \'term_id\',
                                \'terms\' => $term->term_id,
                                )
                            )
                        )
                    );
    print_r( $posts_array ); 
}
?>
Link to Codex: http://codex.wordpress.org/Function_Reference/get_terms

SO网友:kdgilang

global $post; $id = $post->ID;

$cat = get_the_category($id);
$loc = get_the_terms($id, \'taxonomy\');
$posts = get_posts(
array(\'post_type\' => \'post\',
    \'post_status\' => \'publish\', 
    \'posts_per_page\' => -1, 
    \'category\' => $cat[0]->term_id, 
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'location\',
            \'field\' => \'id\',
            \'terms\' => $loc[0]->term_id,
        )
    )
)
);
print_r($posts);
这应该是可行的。

SO网友:Varsha Dhadge

Getting posts assigned to current taxonomy

您可以在taxonomy-your\\u tax中添加以下代码。php

<div class="a-article-wrapper">

                <?php 

                    $terms = wp_get_post_terms( $post->ID, \'your-taxonomy\'); 
                    $terms_ids = [];

                    foreach ( $terms as $term ) {
                        $terms_ids[] = $term->term_id;
                    }

                    $args = array(
                        \'post_type\' => \'your-post-type\',
                        \'tax_query\' => array(
                            \'relation\' => \'AND\',
                            array(
                                \'taxonomy\' => \'your-taxonomy\',
                                \'field\'    => \'term_id\',
                                \'terms\'    => $terms_ids
                            )
                        ),
                    );

                    $query = new WP_Query($args);
                    if ( $query->have_posts() ) {
                        while ( $query->have_posts() ) {
                    ?>

                     <div class="row">
                        <div class="col-md-8 a-article-row">
                            <div class="row">
                                <?php $query->the_post();?>
                                <div class="a-post-time">
                                    <span class="a-current-date"><?php the_time(\'j F, D\') ?></span>
                                    <span class="a-current-time"><?php the_time(\'g:i a\') ?></span>
                                </div>
                                <div class="a-article-title">
                                    <?php the_title(); ?> 
                                </div>
                                <div class="a-article-content">
                                    <div id="excerpt"><?php the_excerpt(); ?></div>

                                    <?php the_content(); ?>
                                </div>
                                <div class="a-article-tags">
                                    <?php echo get_the_term_list( get_the_ID(), \'your-taxonomy\', \'\', \',\' ); ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php } } ?>
            </div> 

结束

相关推荐

Wacky taxonomy in wordpress

我有我的主要网站,www.whatsthatbug。com和一个测试站点test。什么是臭虫。com。据我所知,它们是相同的——相同版本的wordpress,相同版本的相同插件,相同主题,相同修改。然而。www上的category小部件显示父类别的计数,但不包括计数中的子类别,而在测试中(以及它直到最近在www上的工作方式),类别计数包括子类别的小计。和在wp管理/编辑标签中。php?分类法=www上的类别,我没有看到任何类别的子类别。这让一些事情变得困难。这意味着有些东西坏了。在测试中,子类别按其应有的