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?
PS variable Needs be global?
not, if it is within the same template.
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
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!