0

Im not sure if my title its the correct, i need to make something in homepage to show recent custom-taxonomies used in my latest posts... like, for example:

I make a new post and i use a custom taxonomy called Actors: Juan

i want to show "Juan" taxonomy in the homepage, below a title that say "Recent actors" or something like that

I was thinking about a tag cloud but i dont know if i can change tags by custom taxonomy.

0

You can do it like:

$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'); 
}

More about get_posts https://codex.wordpress.org/Template_Tags/get_posts More about get_the_terms https://developer.wordpress.org/reference/functions/get_the_terms/

If you want create cloud tag from this you can base on returned objects and count for each object, then based on it create cloud.

For simply tag cloud you could use (where $name is a taxonomy name) (it create cloud from whole taxonomy)

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 ;
}
0

You can use following code:

 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>";
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.