Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取Wordpress单帖子主类别_Php_Wordpress - Fatal编程技术网

Php 获取Wordpress单帖子主类别

Php 获取Wordpress单帖子主类别,php,wordpress,Php,Wordpress,当一篇文章有多个类别时,我只想显示它的主要类别。目前使用此代码: <?php $perma_cat = get_post_meta($post->ID , '_category_permalink', true); if ( $perma_cat != null ) { $cat_id = $perma_cat['category']; $category = get_category($cat_id); } else { $categories

当一篇文章有多个类别时,我只想显示它的主要类别。目前使用此代码:

<?php
  $perma_cat = get_post_meta($post->ID , '_category_permalink', true);
  if ( $perma_cat != null ) {
    $cat_id = $perma_cat['category'];
    $category = get_category($cat_id);
  } else {
    $categories = get_the_category();
    $category = $categories[0];
  }
  $category_link = get_category_link($category);
  $category_name = $category->name;  
?>    

  <a href="<?php echo $category_link ?>"><?php echo $category_name ?></a> <span class="ion-ios-arrow-right"></span></li>



目前找不到太多关于此的文档。有任何改进建议吗?

请注意,只有在您使用Yoast实现“主要”类别时,这才有效——但我相信这就是您正在做的——因此此代码块(由Jawinn在github上编写)应该可以工作:

<?php 
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category();
$useCatLink = true;
// If post has a category assigned.
if ($category){
    $category_display = '';
    $category_link = '';
    if ( class_exists('WPSEO_Primary_Term') ){
    // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
    $wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
    $wpseo_primary_term = $wpseo_primary_term->get_primary_term();
    $term = get_term( $wpseo_primary_term );
    if (is_wp_error($term)) { 
        // Default to first category (not Yoast) if an error is returned
        $category_display = $category[0]->name;
        $category_link = get_category_link( $category[0]->term_id );
    } else { 
        // Yoast Primary category
        $category_display = $term->name;
        $category_link = get_category_link( $term->term_id );
    }
} 
else {
    // Default, display the first category in WP's list of assigned categories
    $category_display = $category[0]->name;
    $category_link = get_category_link( $category[0]->term_id );
}
// Display category
if ( !empty($category_display) ){
    if ( $useCatLink == true && !empty($category_link) ){
        echo '<span class="post-category">';
        echo '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
        echo '</span>';
    } else {
        echo '<span class="post-category">'.htmlspecialchars($category_display).'</span>';
    }
}

}
?>


您是否正在使用yoast设置您的主要类别?非常感谢。Wordpress没有这个功能真是太奇怪了!!!Np-我同意,或者至少Yoast应该为此提供一个函数:)