Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

Categories in WordPress

In this post, we’ll see how to create categories in WordPress, subcategories, and what the code is that defines one or more categories in a template.

Putting our posts into categories is a good way to group our posts and then have them displayed.

Categories apply to posts, which is why creating a category is found in the Posts tab.

On the image before we can see how to create a new category and how to put or not this one as subcategory, you may put a category as subcategory of other specifing its parent(Parent Category).

Observe that in this post i was created a category called “php” with slug “php-slug” ¿What will use wordpress the slug or the name in a URL?

WordPress will use the slug instead of the name in the url

Category Template

We can create a page for categories, in an older post we see the loop and how to create a simple index.php now we’re going to create a category.php file, remember if wordpress do not have a category.php it will load the index file instead, this is explained in WordPress template hirarchy.

I going to take what we do on “the loop” post and i will change it slightly, this code as is actually works is the code im using it to show you the slug example.

this icon will refeer to the theme folder in this post
/**this is a basic wordpress loop */


    if ( have_posts() ) {

        // Load posts loop.
        while ( have_posts() ) {
            <a href="<?php the_permalink();?>">
            <h1><?php the_title();?></h1>
            <?php the_content();
        echo "no post to be showed";

In this example I added “is category” function that checks if is a category instead of do the loop in vain.

Customize your Category

As I write in WordPress template hierarchy, categories can be customized using a slug or an id being the files in the following way:

  • category-$slug.php
  • category-$ID.php

Let’s create a file with slug for example think that you are creating a site of recipes, the category that I want would it be desserts, thus I will put that name as slug.

Now we have to create that file “category-desserts.php”, and now I’m going to copy and change a little what I have in category.php.

I’m going to put this image when the category is desserts, to notice the difference between a normal category and this one.

Then you have to enter in category/postres in my case my project is called test so my URL will be:


This icon on this case references the template folder

    background:url('') no-repeat;
    background-size: 50px 50px;

/**this is a basic wordpress loop */


    echo "<div class='header_postre'><h1>";
    echo "Postres";
echo "</h1></div>";
    if ( have_posts() ) {

        // Load posts loop.
        while ( have_posts() ) {
            <a href="<?php the_permalink();?>">
            <h1><?php the_title();?>
            <?php the_content();
        echo "no post to be showed";

Functions that you can use in categories


Check if is the category assigned to the variable $category, by default the value is “”, is_category permit us using it without parameter when we are in a category page and check if is a category


Obtain all the categories that you added on WordPress via post > categories


Returns an array of objects WP_Term

You may obtain the category id with the following code:

$category = get_the_category();
echo $category[0]->cat_ID;
get_cat_name( $cat_id )

To obtain the category name we can use get_cat_name, we only need to pass the category id as a parameter

single_cat_title( ”, false);

Also, we can get the category name with this one too, single_cat_title(“”,false) this does not print the name of the category on the screen, only obtain it, so you can obtain it and use it in a variable.

Category: en-wordpress
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category