Home > Backend Development > PHP Tutorial > 请问wordpress中如何获取自定义post_type的分类

请问wordpress中如何获取自定义post_type的分类

WBOY
Release: 2016-06-06 20:39:38
Original
1077 people have browsed it

我再wordpress 自定义的post type中想要获取当前文章的分类。分类也是自己用register_taxonomy来自定义的。

<code><div class="cell-home">
<?php query_posts(array( 'post_type' => 'wcp_employees' ));?>
  <?php if ( have_posts() ) : ?>
   <?php query_posts(array( 'post_type' => 'wcp_employees' )); ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <dl>
            <dt style="background-image:url(<?php bloginfo('stylesheet_directory'); ?>/images/employees/01.jpg);">
                <div class="title"><?php the_category(', ');  ?></div>
            </dt>
            <dd style="display:block;">
                <div class="txt-content">
                    <h3><?php the_title(); ?></h3>
                    <p><img  class="thumb" src="<?php%20bloginfo('stylesheet_directory');%20?>/images/employees/04.jpg" alt="请问wordpress中如何获取自定义post_type的分类" ></p>
                    <p>=get_post_excerpt()?></p>
                    <p class="tr"><a href="<?php%20the_permalink();%20?>" class="btn-more">More</a></p>
                </div>
            </dd>
        </dl>
    <?php ;endwhile; twentyfourteen_paging_nav();?>
    <?php else : ?>
    <?php endif; ?>
</div>
</code>
Copy after login
Copy after login

这里我也用了query_posts 来指定自己的分类,但是分类还是现实不出来。

回复内容:

我再wordpress 自定义的post type中想要获取当前文章的分类。分类也是自己用register_taxonomy来自定义的。

<code><div class="cell-home">
<?php query_posts(array( 'post_type' => 'wcp_employees' ));?>
  <?php if ( have_posts() ) : ?>
   <?php query_posts(array( 'post_type' => 'wcp_employees' )); ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <dl>
            <dt style="background-image:url(<?php bloginfo('stylesheet_directory'); ?>/images/employees/01.jpg);">
                <div class="title"><?php the_category(', ');  ?></div>
            </dt>
            <dd style="display:block;">
                <div class="txt-content">
                    <h3><?php the_title(); ?></h3>
                    <p><img  class="thumb" src="<?php%20bloginfo('stylesheet_directory');%20?>/images/employees/04.jpg" alt="请问wordpress中如何获取自定义post_type的分类" ></p>
                    <p>=get_post_excerpt()?></p>
                    <p class="tr"><a href="<?php%20the_permalink();%20?>" class="btn-more">More</a></p>
                </div>
            </dd>
        </dl>
    <?php ;endwhile; twentyfourteen_paging_nav();?>
    <?php else : ?>
    <?php endif; ?>
</div>
</code>
Copy after login
Copy after login

这里我也用了query_posts 来指定自己的分类,但是分类还是现实不出来。

找到方法了,用这个function

<code>function custom_taxonomies_terms_links(){
    //根据当前文章ID获取文章信息
    $post = get_post( $post->ID );

    //获取当前文章的文章类型
    $post_type = $post->post_type;

    //获取文章所在的自定义分类法
    $taxonomies = get_object_taxonomies( $post_type, 'objects' );

    $out = array();
    foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
        $term_list = wp_get_post_terms($post->ID, $taxonomy_slug, array("fields" => "all"));
        echo $term_list[0]->name; //显示文章所处的分类中的第一个
    }

    return implode('', $out );
}
</code>
Copy after login

<code>/**
 *获取分类
 **/
function deel_category(){
    $tag_arr = get_the_category();
    foreach($tag_arr as $value){
        if(!empty($value)){
            echo '<a data-bear="main" href=".get_term_link(%24value->slug,%24value->taxonomy).">'.$value->name.'</a><a>';
        }
    }
}
</a></code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template