在主页中显示最新使用的分类

时间:2017-06-20 作者:Hank Scorpio

我不确定我的标题是否正确,我需要在主页中做一些东西来显示我最新帖子中使用的自定义分类法。。。例如:

我发表了一篇新文章,并使用了一种称为Actors:Juan的自定义分类法

我想在主页上显示“Juan”分类法,标题下面写着“最近的演员”之类的东西

我正在考虑一个标记云,但我不知道是否可以通过自定义分类法更改标记。

2 个回复
SO网友:Isu

您可以这样做:

$args = array(
    \'post_type\'        => \'post_type_name\', // or use default if you use standard
    \'numberposts\'      => 3,  // or you can use 1 if you need only from last post.
    \'orderby\'          => \'date\', //by dates to get lastest
    \'order\'            => \'DESC\',
);

$posts = get_posts($args);
$recent_taxonomies = [];
foreach ($posts as $key => $post_data) {
    $recent_taxonomies[] = get_the_terms( $post_data->ID, \'custom_taxonomy_name\'); 
}
更多关于get_posts https://codex.wordpress.org/Template_Tags/get_posts更多关于get_the_terms https://developer.wordpress.org/reference/functions/get_the_terms/

如果您想从此创建云标记,可以基于返回的对象和count 对于每个对象,然后基于它创建云。

为了简单地标记您可以使用的云(其中$name 是一个分类名称(它从整个分类中创建云)

function tagsCloudByTerm($name){
    $tags_out = null;
    $tax_terms = get_terms($name);
    $tags_out = \'<span class="tag-meta ">\';
    $tmp = [];
    $max = 0;
    if(!empty($tax_terms)):
        foreach ($tax_terms as $term) :
            if($max < $term->count){
                $max = $term->count;
            }
        endforeach;
        foreach ($tax_terms as $term) :
             $percent = floor(($term->count / $max) * 100);
             if ($percent < 20):
               $class = \'smallest\';
             elseif ($percent >= 20 and $percent < 40):
               $class = \'small\';
             elseif ($percent >= 40 and $percent < 60):
               $class = \'medium\';
             elseif ($percent >= 60 and $percent < 80):
               $class = \'large\';
             else:
             $class = \'largest\';
             endif;
            $term_link_url = get_term_link($term, $post_taxonomies[$tag_key]);
            $tmp[] = \'<a class="\'.$class.\'" href="\'.$term_link_url.\'" title="\'.$term->name.\'">\'.$term->name.\'</a>\';
        endforeach;
    endif;
    $tags_out .= implode(\', \', $tmp);
    unset($tmp);
    $tags_out = $tags_out.\'</span>\';
    return $tags_out ;
}

SO网友:Sonali

您可以使用以下代码:

 add_shortcode(\'get_latest_taxonomy\', \'get_latest_taxonomy\');
        function get_latest_taxonomy()
        {
            global $wpdb;
            $args = array(\'post_type\'=>\'POST_TYPE_NAME\',\'posts_per_page\'=>-1);
            $result =  get_posts($args);
            $cat_name = array();
            foreach ($result as $key => $value) {
                $ids = $value->ID;
                $cate = get_the_terms($ids, \'YOUR_TAXONOMY_NAME\');
                foreach ($cate as $k => $v) {
                    $cat_name[] = $v->name;
                }
            }
            $latest_cate = array_unique($cat_name);
            echo "<ul>";
            foreach ($latest_cate as $latest_cate_key => $latest_cate_value) {
                echo "<li>";
                print_r($latest_cate_value);
                echo "</li>";
            }
            echo "</ul>";
        }

结束

相关推荐