How do I print a certain wordpress category anywhere I want on any page?

Can someone tell me what the PHP would look like in order to get a category from WordPress and then print it where ever I want?

I’m guessing I have to build a PHP function. Something like:

Read More

function get_a_category() {

$category = get_the_category(); <—–( not sure how to ge ta specific category )

echo $category;

}

I have no idea I know nothing about PHP

can someone help me please ?

Related posts

Leave a Reply

2 comments

  1. You don’t have to write your own function; you do have to work with the The Loop (WordPress Codex) and a new query (Function Reference/query posts « WordPress Codex) to keep the original WP loop in place. Run this new query in a page template to get the first post from “mycategory”. Change showposts to the number of posts you want.

    <?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <?php the_title(); ?></a>
    <?php endwhile; ?>
    
  2. Try this

    function get_a_category() {
        $category = get_the_category();
        foreach ($category AS $key => $value) {
            $category_name[] = $value->cat_name;
        }
        $categories = implode(', ',$category_name);
        echo $categories;
    }