如何在wordpress中将类别显示为我的页面上的标题?

时间:2016-03-16 09:22:52

标签: php wordpress

我创建了一个名为" State Leagues"的自定义帖子类型。使用类型插件。我的帖子类型有一些类别和输出。当我显示帖子时#39;它正确显示的标题。我显示帖子标题的代码是:

 <?php
            $args = array(
                'post_type' => 'stateleague-pos-type',
                'posts_per_page' => 4,
                'order_by' => 'post_date',
                'order' => 'DESC'
            );
             $loop = new WP_Query($args);
//            $counter = 0;
//            $big = 1;
            while ($loop->have_posts()) : $loop->the_post();
          ?>

                <a href="<?php the_permalink(); ?>">

               <h1> <?php the_title(); ?> </h1>
                </a>  
?>

现在我要显示类别&#39;将标题命名为标题而不是帖子&#39;标题。我已经搜索过了,但没有发现任何可行的东西。是否可以将类别名称显示为标题?

1 个答案:

答案 0 :(得分:0)

    <?php
    $taxonomy = 'filters';
    $terms = get_terms($taxonomy);

    if ( $terms && !is_wp_error( $terms ) ) :
    ?>

        <ul>
            <?php foreach ( $terms as $term ) { ?>
                <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
            <?php } ?>
        </ul>
    <?php endif;?>



Or in fuctions.php Put  this:



  function get_the_category_custompost( $id = false, $tcat = 'category' ) {
    $categories = get_the_terms( $id, $tcat );
    if ( ! $categories )
        $categories = array();

    $categories = array_values( $categories );

    foreach ( array_keys( $categories ) as $key ) {
        _make_cat_compat( $categories[$key] );
    }

    return apply_filters( 'get_the_categories', $categories );
}


and call the function as:



<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?>

试试这个我希望它会有所帮助