php - display text based on category in wordpress -


how can solve following problem. want show different sub headings based on wordpress category being used.

this following code:

 $categ = the_category(' ');  if($categ == 'news'){             $second_header = " secondry header displayed here";     }else{             $second_header = "error";     }     ?>     <h2> <?php the_category(' '); ?></h2>     <p> <?php echo $second_header; ?></p> 

right not working, instead of checking against text 'news' there way check against category id?

thanks in advance.

you can use following store current category id:

<?php $catid = the_category_id($echo=false);?> 

the echo false stops echo on page , stores variable. can following:

<?php   $categ = the_category_id($echo=false);  if($categ == 1) {     $second_header = " secondry header displayed here"; } else {     $second_header = "error"; }  ?>  <h2> <?php the_category(' '); ?></h2> <p> <?php echo $second_header; ?></p> 

alternatively use following (as believe the_category_id deprecated http://codex.wordpress.org/function_reference/the_category_id):

$categories = get_the_category(); $categ = $categories[0]->cat_id;  if($categ == 1) {     $second_header = " secondry header displayed here"; } else {     $second_header = "error"; }  ?>  <h2> <?php the_category(' '); ?></h2> <p> <?php echo $second_header; ?></p> 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -