dozxos6346 2015-07-21 19:51
浏览 24
已采纳

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

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).

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??

  • 写回答

1条回答 默认 最新

  • duannei1477 2015-07-21 21:12
    关注

    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

    <!-- 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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示