Support » Fixing WordPress » Get Category Name Not As A Link

  • Resolved stoi2m1

    (@stoi2m1)


    I am trying to find a way to display the current category name not as a link. I eventually want to use this as a variable, but getting it to just display as a header for now will be fine.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Here you go:
    http://codex.wordpress.org/Template_Tags/get_the_category

    Display all categories:
    <?php
    foreach((get_the_category()) as $cat) {
    echo $cat->cat_name . ' ';
    } ?>

    Display only the first category:
    <?php
    $cat = get_the_category();
    $cat = $cat[0];
    echo $cat->cat_name;
    ?>

    Instead of echoing those, you can use $cat->cat_name as your variable.

    Thanks a bunch Im using this code to get the category name only and make it the variable $postname.

    <?php
    $cat = get_the_category();
    $cat = $cat[0];
    $postname = $cat->cat_name;
    ?>

    Perfect. I’m happy that works for you.

    Can you mark this “Resolved” so other people know? Thanks.

    I’m giving this a try too, but it shows only the first category, not the one currently being viewed.

    Wouldn’t it be easier to pull the information from the url ? http://www.myurl.com/category/currentcategory

    Im using this function on my homepage. No way to pass anything to there.

    OK, I was trying to do the same thing, took me ages to work this out but got it now …

    Say, I have a post which has 2 assigned categories ‘journal’ and ‘family’

    I want a special page header if user goes to archive pages for ‘family’

    in archive.php go to this line:

    <?php /* If this is a category archive */ if (is_category()) { ?>

    now I added:

    <?php
    $theCat = ”;
    foreach((get_the_category()) as $cat) {
    $aCat = $cat->cat_name;
    if ($aCat == ‘family’) $theCat = $cat->cat_name;
    }

    if ($theCat == ‘sylvia’) {
    echo “my special code here”;
    }
    ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get Category Name Not As A Link’ is closed to new replies.