如何为一个特定的职位获得最高级别的职位?

时间:2016-08-07 作者:bestprogrammerintheworld

我理解Wordpress的“主要类别”概念,但I do not understand how to fetch the name of the highest category in the hierarchy 针对特定职位?

具有此代码:

$category_obj = get_the_category( $post_id );
$category_name = $category_obj[0]->cat_name;                   
将获取该帖子的类别名称,但该类别只是该帖子列出的第一个类别,并且该类别并不总是主要类别。

我在“循环”之外使用这个。

3 个回复
SO网友:LWS-Mo

另一个想法(这在很大程度上取决于您正在查看的[单个或归档]以及您的分类结构)是寻找术语父级。

也许这对你也有帮助。。。

您是在询问;特定职位层次结构中的最高类别;。

示例:
帖子与3 terms 分类法的hierachical:

Parent Term
-First-Child Term
--Second-Child Term
您可以获取此帖子的所有相关条款:

$all_terms = get_the_terms( $post_id, $taxonomy );
然后找到parent 属于0:

foreach($all_terms as $term) {

   if($term->parent == 0){
      //here you have the data of the topmost parent, e.g. "Parent Term"

      // get parent term ID
      $parent_id = $term->term_id; 

      // get term obj from ID to get term name
      $parent_obj = get_term_by( \'id\', $parent_id, $taxonomy );
      
      // show parent term name
      echo $parent_obj->name; // "Parent Term"

      // show parent term permalink URL
      echo = get_term_link( $parent_id );
   }

}
如您所见,只有当该帖子只有一个父项和多个子项关联时,这才有效。对于其他设置,这将不起作用!

SO网友:bestprogrammerintheworld

我想出来了。我在Wordpress中找不到这方面的任何核心功能,但要检索主要类别,可以使用get permalink函数:

$site_url = get_site_url();
$post_slug = get_permalink( $post_id );
$post_slug_remove_siteurl = str_replace( $site_url, \'\', $post_slug);                           
$category_from_slug = explode (\'/\', $post_slug_remove_siteurl);
$category_name = $category_from_slug[1];
这要求您在永久链接中包含类别。之所以可能这样做,是因为在更改主要类别时,永久链接也会更改:

<site-url> / <primary category> / <post name>
请告诉我是否有更好的方法来实现这一目标!

SO网友:Ravi Patel

Display Primary Category (Yoast\'s WordPress SEO)

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