Display image or post in sidebar depending on category

Posted in: Newspaper
Post count: 33

Hi,
i want to add on the top of the sidebar a picture with link or a specific post thumbnail depending on the post category. For each category to have a different thumbnail/image.

On my old theme i added this code in the sidebar.php file:

<?php if ( in_category( ‘xxxxxx’ ) ) {?>
<div id=”label”></div>
<?php }?>

Where can i add this code in order to show the div label or what can i add in the widget sidebar for this?

Creating a separate sidebar for each category is an option but i have about 20 cat and i have to create 20 separate sidebars for this and i prefer to keep this as backup solution.

Thank you

Post count: 22421

Hi,
It will still work in sidebar.php but the one from the wp-booster folder in our case: http://screencast.com/t/HpXCl3pXzb
It will not work in sidebars created in visual composer though(2/3 + 1/3 column layout) but on pages you can add it with a single image element.
Thank you!

Post count: 33

Hi,
not sure i understand where i have to add the php code in sidebar.php file, i’m using only the default sidebar.

The solution 2 with the widgets, on sidebar in a new widget i added the following code for one post
[vc_row][vc_column][vc_posts_slider count="1" interval="3" thumb_size="large" posts_in="3429"][/vc_column][/vc_row]
but i want to make it somehow change depending on the category.
On the home page i want to hide this.

  • This reply was modified 10 years by dragos_2p.
Post count: 22421

Hi,

You can use the in_category function and use the do_shortcode function as well if you plan to use visual composer shortcodes.
You wanted it to show at the top of every sidebar so you would place the code at the top of the sidebar file. like so:
http://screencast.com/t/AsflY1dxoby8
if (in_category('category-slug')){
echo do_shortcode('[vc_posts_slider count="1" interval="3" thumb_size="large" posts_in="3429"]');
}
elseif (in_category('category-slug2')){
echo do_shortcode('[vc_posts_slider count="1" interval="3" thumb_size="large" posts_in="3429"]');
}

Hope this helps.

Post count: 33

Works great except one thing,
if i’m on the post 3429 in the sidebar i have the text Nothing found.

Any idea why is this happening?

Post count: 22421

I’m not sure why this happens, try to add another element there to see if it works, like a custom ad for example, then if that shows up, you need to check the slider settings. Try the slider on the default settings.
Thanks.

Viewing 6 posts - 1 through 6 (of 6 total)
The forum ‘Newspaper’ is closed to new topics and replies.