Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to design a button in the sidebar which is related to the post displayed. The button is basically a "other news in CAT1". i have this piece of code:

<div id="pagine">
<?php 
$category = get_the_category(); 
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'"><div id="pagine"><ul><li>News su '.$category[0]->cat_name.'</li></ul></div></a>';
}
?>
</div>

which unfortunately displays the wrong category. Can please anybody tell me how to do it. It shouldn't be that difficult, i just started with wordpress.

share|improve this question
add comment

2 Answers

One these three should do the job for you...

1. Function: the_category();

News su <?php the_category(', '); ?>

Displays as:

News su WordPress, Computers, Blogging

And if only a single category is assigned to a post, it shows up like this:

News su WordPress

2. Function: get_the_category_list();

<div id="pagine"><?php echo get_the_category_list(); ?></div>

Displays as:

<div id="pagine">
    <ul class="post-categories">
        <li>
            <a href="http://example.com/category/wordpress/" title="View all posts in Business" rel="category tag">WordPress</a>
        </li>
        <li>
            <a href="http://example.com/category/computers/" title="View all posts in Business" rel="category tag">Computers</a>
        </li>
    </ul>
</div>

And if only a single category is assigned to a post, the output would be like this:

<div id="pagine">
    <ul class="post-categories">
        <li>
            <a href="http://example.com/category/wordpress/" title="View all posts in Business" rel="category tag">WordPress</a>
        </li>
    </ul>
</div>

3. Function: single_cat_title();

If you want to show just one category (category with the lowest ID will be shown) no matter how many categories are assigned to a post, use something like this:

<div id="pagine">
    <ul>
        <li>
            <?php
                $category = get_the_category();
                echo '<a href="'.get_category_link($category[0]->cat_ID).'">News su ' . $category[0]->cat_name . '</a>';
            ?>
        </li>
    </ul>
</div>

The above code always shows one category, like this:

News su WordPress

So, given the codes (and what each does), suit them to your needs.

share|improve this answer
add comment

get_the_category returns an array of the stdClass object of all categories associated with the current post_id.

$category[0] will give you the first category in the array. To see the mapping do.

<?php print_r( $category ); ?>
share|improve this answer
    
Thanks, Chris_O. The reason why it was displaying the wrong category is that for some reason, most of the posts have the wrong category! The print_r, showed that most of them are in the wrong category. But the disturbing thing is that in the WP CMS the post is in the right category! EG. In the CMS postA is in categoryA, while with the print_r it show categoryB! Why do you think it happens? Thanks for your time –  Filippo Bonino Jan 8 '12 at 17:46
    
your code will only work if a post is just in one single category. also, be aware that your code is generating invalid html code, having a div and ul within a link tag. –  Michael Jan 8 '12 at 22:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.