0

I wanted to do the following, and I know its possible, but I don't know how. I'm looking at in_category, but it doesn't make to much sense.

Basically what I want to do, for an example on my homepage, category page and similar posts are shown with title and a thumb.

And for this thumbs I want to add function like this

If post/thumb is inside category video -> add video icon above it. I guess with css I would need to position it, for an example at the bottom right corner.

Any ideas how to this

2 Answers 2

1

The implementation can be purely CSS, and depends on your specific HTML markup. However, the class you'll need to target is supplied by the post_class() template tag.

Let's say you have something like the following, basic markup:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div class="post-title">

        <img ... />
        <h1><?php the_title(); ?></h1>

    </div>

    <!-- Etc. -->

</div>

The post_class() template tag will output a category-based class, category-{name}, and also, category-{id}. So, you can target either one in your stylesheet:

.category-{name} .post-title img {}

or

.category-{id} .post-title img {}

It'll be specific to your markup, of course; but hopefully you get the idea.

4
  • Hi Chip, thaks for your guide.As i said I am a newbie with php, working with tutorials and trial and errors. I tried with your way but without any luck.I think i tried every possible solution with it ..your code for calling trough css is .category-{name} and your post template tag link is without curly bracket so .category-name so i m little confused what the right one is, still i tried both of them..and no luck. However, i manage to do it on other way, to place a div element arround it with the class (icon) which i called from css .But the thing is, it is shown only on the category page
    – Max
    Commented Aug 5, 2013 at 20:13
  • What i want is not that.Maybe i explained on the wrong way,i m not sure, as english is not my first languange.What i want is this.For an example on my homepage,archive,search and so on, i want every post which is under category video (add post-check cat video-publish) and have thumb,to show in that thumb play button icon.On this way it is shown as i said only on the category page link. Here it widget i edited for my need ( pastebin.com/kPc57e0z) and the index. php (pastebin.com/vkMQfkU3) so you can see much easier am i doing something wrong here. Kind regards,and thank you
    – Max
    Commented Aug 5, 2013 at 20:18
  • #Update : looking at wp conditional tags replacing if (is_category to if (in_category did the trick. However now another problem is that if the post is inside 2,3 or more categories, and one of them is video icon doesnt appear.It appears only and only is the post have only one category - video.. so now need a fix for this...And I m not sure is there some easier way to do this..as you said only trough css would be awesome..but cant find a way for that
    – Max
    Commented Aug 5, 2013 at 21:13
  • Chip has the right idea here. If you're using post_class() on your containers you most likely achieve what you want with CSS alone, skipping your in_category checks. Can you post the HTML output from the content area of your archive or search page?
    – Will
    Commented Aug 31, 2015 at 17:56
0

You could do something with this, depending upon how many icons you wish to use.

http://codex.wordpress.org/Category_Templates#Different_Text_on_Some_Category_Pages

<?php if (is_category('video')) : ?>
  <img class="icon" src="img/video-icon.png" />
<?php elseif (is_category('music')) : ?>
  <img class="icon" src="img/music-icon.png" />
<?php else :   // Set a general icon used in any other instance ?>
  <img class="icon" src="img/general-icon.png" />  
<?php endif; ?>
6
  • Hi Thanks for your answer. So if i got this right.Here img/video-icon.png i add image link which is logical, and the code i add inside function.php and inside css i stlye the position of the image...or i need to add it somewhere else...it looks easy looking at this..that why i ask
    – Max
    Commented Aug 3, 2013 at 15:50
  • I tried to insert this code with a link to image here "img/video-icon.png" inside category,archive,main,and widget it self but no luck.I left out the last part as i dont need it, i dont know is that wrong..Still the image doesnt show up..I even try to wrap it with div
    – Max
    Commented Aug 3, 2013 at 16:25
  • I dont know why it wont show up the code, and the previous post is deleted, weird but i add it here so pls @user2081869 take it look pastebin.com/Htv7KSXx
    – Max
    Commented Aug 3, 2013 at 18:06
  • anything i post its deleted...wtf? Basically i tried with this code but nothing shows up <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php $cats = wp_get_post_categories( $post->ID );if (in_array('5', $cats)) echo '<img class="vid-icon" src="http://domain.com/wp-content/uploads/2013/08/video-icon.gif">';the_post_thumbnail('large-thumb'); ?></a>
    – Max
    Commented Aug 4, 2013 at 20:53
  • Sorry for the delay. This code doesn't go into the functions.php file, but into the template code. So if you are using this on a blog page, insert it into the page.php template where you want it to go.
    – Baerkins
    Commented Aug 9, 2013 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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