Support » Fixing WordPress » How to get the current category name, inside the loop

  • rykios

    (@rykios)


    Hello guys,

    I would like (mainly for seo reasons) to use the category name, inside the loop, when viewing a spesific category page.

    Lets say in example, we are looking at category “rock-music” page, and we see some existing posts there, titles, thumbs and readmore. Now, i would like to add after each and every title, a line like “Another rock-music post, added (date goes here) by (author goes here)”
    Have done all that, the only problem is geting the actual category name to use inside the loop

    PS – I am using a conditional statement
    elseif ( is_gategory() ) : // Print a second line here in case we are browsing a category page
    The conditional statement and ALL are working fine, my only problem is pass that damn current category name in there….

    ANY help/ideas are most welcomed 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • Michael

    (@alchymyth)

    anywhere within the category archive (could be used before the loop to save the category name to the $cat_name variable):

    $cat_name = get_category(get_query_var('cat'))->name;

    http://codex.wordpress.org/Function_Reference/get_category

    within the loop, if your posts only have one category:

    $cats = get_the_category();
    $cat_name = $cats[0]->name;

    http://codex.wordpress.org/Function_Reference/get_the_category

    Thread Starter rykios

    (@rykios)

    Hmmm since all my posts have several categories ( and i only need one when viewing a category page) i will try what you say, save the category name to a variable and see if can pass to the loop
    Thank you VERY much for reply !

    PS variable Needs be global?

    Michael

    (@alchymyth)

    PS variable Needs be global?

    not, if it is within the same template.

    Michael

    (@alchymyth)

    ps:
    actually, within a category archive, you can use the WordPress function:

    http://codex.wordpress.org/Function_Reference/single_cat_title

    example:

    $cat_name = single_cat_title( '', false );
    Thread Starter rykios

    (@rykios)

    Sadly not working…

    in category.php i have added both for a test

    <?php
    $cat_name = get_category(get_query_var('cat'))->name;
    $cat_name2 = single_cat_title( '', false );
    ....

    then loop.php calls content.php with the new get_template_part thingy, like this:

    <?php while ( have_posts() ) : the_post();
    get_template_part( 'content', get_post_format() );
    ....

    So, in content.php i have added to test both:

    <span>
    <?php echo " test1 $cat_name test2 $cat_name2 endtest" ?>.
    </span>

    Prints the test1 word, the test2 word, endtest – not the variable value – they are empty 🙁

    Live-example page http://bit.ly/xYalrl

    PS If i echo both variables from inside category php, they are both fine

    Michael

    (@alchymyth)

    try to use global $cat_name; global $cat_name2; in loop.php and content.php (possibly also in category.php ?) …

    Thread Starter rykios

    (@rykios)

    Right 🙂 will try that! Lets see …

    Thread Starter rykios

    (@rykios)

    Works FINE! 🙂

    Thank you very very much for this help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to get the current category name, inside the loop’ is closed to new replies.