简体   繁体   English

以自定义帖子类型获取单个帖子的类别

[英]Get categories for a single post in a custom post type

I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. 我试图在自定义帖子类型循环中获取单个帖子的类别的无格式列表(最好是子列表)。 This list will eventually serve as a class for a div ( $CATEGORYSLUGSWILLEVENTUALLYGOHERE ). 此列表最终将作为div( $CATEGORYSLUGSWILLEVENTUALLYGOHERE )的类。

I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. 我发现了几种不同的方法来获取自定义帖子类型的所有类别的列表,但没有特定于单个类别的列表。 Here's what I have so far: 这是我到目前为止的内容:

<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>

                        <?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>


                            <div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">

                                    <div class="port-item-home">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'portfolio-home' ); ?></a>
                                        <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                                    </div>

                                </div>



                    <?php endwhile; ?>

And here's what I've tried so far to get the category list: 到目前为止,这是我尝试获取类别列表的方法:

<?php
                                    $args = array(
                                      'orderby' => 'name',
                                      'parent' => 0,
                                      'taxonomy' => 'project-type'
                                      );
                                    $categories = get_categories( $args );

                                    echo '<p> '.print_r(array_values($categories)).'something</p>'

                                ?>

I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post. 我有返回数组的数组-但数组显示它将显示所有类别,而不是与该特定帖子有关的类别。

I also tried: 我也尝试过:

<?php
    //list terms in a given taxonomy (useful as a widget for twentyten)
        $taxonomy = 'project-type';
        $tax_terms = get_terms($taxonomy);
?>

<?php
    foreach ($tax_terms as $tax_term) {
        echo $tax_term->name;
    }
?>

And that also displays all categories instead of the ones pertaining to the post. 并且还会显示所有类别,而不是与帖子相关的类别。

Any suggestions?? 有什么建议么??

Got it! 得到它了! Found this article that helped me out: 找到这篇对我有帮助的文章:

https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type

<!-- The Query -->
<?php 
    $args = array( 
        'post_type'      => 'my_post_type', 
        'posts_per_page' => -1, 
        'orderby'        => 'menu_order' );

    $custom_query = new WP_Query( $args );
?>

<!-- The Loop -->
<?php 
    while ( $custom_query->have_posts() ) : 
        $custom_query->the_post();
        $terms_slugs_string = '';
        $terms = get_the_terms( $post->ID, 'my_post_type' );
        if ( $terms && ! is_wp_error( $terms ) ) {                
            $term_slugs_array = array();
            foreach ( $terms as $term ) {
                $term_slugs_array[] = $term->slug;
            }
            $terms_slugs_string = join( " ", $term_slugs_array );
        }
?>

    <div class="box<?php echo $terms_slugs_string ?>">          
        <div class="port-item-home">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail( 'portfolio-home' ); ?>
            </a>
            <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
            </a>
        </div>
    </div>  

<?php endwhile; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM