【问题标题】:Get categories for a single post in a custom post type获取自定义帖子类型中单个帖子的类别
【发布时间】:2015-10-11 10:57:34
【问题描述】:

我正在尝试在自定义帖子类型循环中获取单个帖子的类别的无格式列表(最好是 slug 列表)。此列表最终将用作 div 的类 ($CATEGORYSLUGSWILLEVENTUALLYGOHERE)。

我发现了几种不同的方法来获取自定义帖子类型的所有类别的列表,但不是特定于单个类别的方法。到目前为止,这是我所拥有的:

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

这是我迄今为止尝试获取类别列表的方法:

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

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

                                ?>

我让它返回数组 - 但数组显示它将显示所有类别,而不是与该特定帖子相关的类别。

我也试过了:

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

这还会显示所有类别,而不是与帖子相关的类别。

有什么建议吗??

【问题讨论】:

    标签: php wordpress custom-post-type categories slug


    【解决方案1】:

    知道了!找到这篇帮助我的文章:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      相关资源
      最近更新 更多