A
A
Alexander Ruzhevich2015-11-06 11:48:25
WordPress
Alexander Ruzhevich, 2015-11-06 11:48:25

How to display an image for a specific post?

Good day. I understand that the question is easy, but for some reason, after several hours of searching, it didn’t “reach”. There is a page that shows all entries from category "2", and as it should be, in order to indicate that a person is viewing this particular category, the desired picture is loaded from above. I do it like this:
<?php if ( in_category('2') ) { ?>
here I display the block with the desired image
<?php } ?>
It works fine, the question is how to make it so that the category image is also displayed when clicking on entry? that is, when you go to the page of the entry itself from category "2", a picture corresponding to it is shown, category "3" is also corresponding to it, etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2015-11-06
@ruzhevich

$category = get_the_category(); 
$cat_id =  $category[0]->cat_ID;
if( 2  ==  $cat_id ) {
// тут вывожу блок нужной картинкой 
}

Or is it better
if( has_term( 2, 'category' ) ){
   // тут вывожу блок нужной картинкой 
}

But it's more correct
$category = get_the_category(); 
// тут функция которой выводит у вас картинку категории 
// для примера сферическо вакуумная get_term_img();
echo get_term_img($category[0]->cat_ID);

A
Alexander Ruzhevich, 2015-11-06
@ruzhevich

<?php $category = get_the_category();
$cat_id = $category[0]->cat_ID;?> <
?php if( 2 == $cat_id ) {?>
//picture here
<?php }?>
Sorry

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question