WordPress.org

Ready to get started?Download WordPress

Forums

Display category in post title? (7 posts)

  1. Robpoke
    Member
    Posted 5 years ago #

    I want to display the category of each post in the beginning of the title, example: "Category - Subject", "Movies - Spider Man review!" and so on.

    This is probably very easy, but I have been searching for forever and I just can't find it. I would be forever grateful for any help!

  2. MichaelH
    Member
    Posted 5 years ago #

    Assuming you only have one category per post this example:

    <?php
    foreach((get_the_category()) as $category) {
        echo $category->cat_name . ' ';
    }
    ?>

    taken from Template_Tags/get_the_category

  3. Robpoke
    Member
    Posted 5 years ago #

    thank you! I do not know however where to put this code? I know in Index, home, etc.. but where in the code? because it won't show up :(

  4. Robpoke
    Member
    Posted 5 years ago #

    I got it to work now BUT my titles always breaks into two rows, example:

    Category
    Subject

    Movies
    Batman

    I want it to show up as:

    Category Subject

    is there a way to do this?

  5. MichaelH
    Member
    Posted 5 years ago #

    <?php
    foreach((get_the_category()) as $category) {
        $category_name = $category->cat_name;
    }
    echo 'Category ' . $category_name . ' Movies ' . $post->title;
    ?>

    If that doesn't work, paste your code in a pastebin and report the link back here.

  6. advancedracedotcom
    Member
    Posted 5 years ago #

    hey michael i used the same above code, but the categories as such on below the post title dont appear as a link.
    could you assist me in getting that ...
    my site : http://advancedrace.com NSFW

  7. MichaelH
    Member
    Posted 5 years ago #

    advanceracedotcom - post your code in a pastebin and report the link back here.

Topic Closed

This topic has been closed to new replies.

About this Topic