Ricky
Ricky

Reputation: 87

display text based on category in wordpress

how can i solve the following problem. Basically I want to show different sub headings based on the wordpress category being used.

this is the 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 now this is not working, instead of checking against the text 'news' is there any way to check against the category id?

thanks in advance.

Upvotes: 0

Views: 974

Answers (2)

IdidntKnewIt
IdidntKnewIt

Reputation: 582

Different Text on some Category Pages:

<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php endif; ?>

if you need category id you will need to first get category through <?php get_the_category_by_ID( $cat_ID ); ?> Because the_category_ID is deprecated, then you can do the same with some modification. More here - http://codex.wordpress.org/Category_Templates

Upvotes: 1

The Humble Rat
The Humble Rat

Reputation: 4696

You can use the following to store the current category id:

<?php $catID = the_category_ID($echo=false);?>

The echo false stops any echo on the page and stores the variable. You can then do the 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 to this you could also use the following (as I believe the_category_ID is now 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>

Upvotes: 1

Related Questions