A shortcode to display the name of a category in a post in Wordpress

954 views Asked by At

What I'm trying to do is find a way to display the name of a post category in WordPress, preperably as a shortcode. For example:

[category-name] Blah, blah, blah

on the post would be become

Blog: Blah, blah, blah

Is this at all possible? And what would be required to set it up?

Thanks

1

There are 1 answers

1
Kasmetski On

If I understand you right, you want to display some category with a shortcode? See this one for an example

    add_shortcode('catlist', function($atts, $content) {
    $atts += array('category' => 1);
    $posts = get_posts("category={$atts['category']}");

    foreach ($posts as $post) {
        echo $post->post_name . '<br />';
    }
});

echo do_shortcode('[catlist category=5]');