I'm trying to make a change to my index.php page where each post displayed by the loop shows a different image depending on category. I can't seem to find it anywhere, or maybe it is too complicated for me to grasp.

I've named my images the same way as category slugs (example: video.png, music.png, text.png for respectively video, music and text categories).

Here is my code so far (I miss the php segment that will return the category slug):

<img src="<?php echo get_template_directory_uri(); ?>/img/<?php ... ?>.png" />

So, I tried to read the codex and found solutions for single posts, but I can't seem to make if work on my index.php file. Any suggestions please?

Thanks :)

1 Answer 1


You probably don't actually want to edit index.php. It is the most generic fallback template when WP doesn't have anything else to go on. Most themes have a front-page.php or blog.php which is where you'll find the loop you are trying to modify. Those files may in turn point to a template part so it really depends on your theme as to which file you need to adjust.

You can identify the "parent template" (the main file serving the current page, such as front-page.php) using code here - How do you find out which template page is serving the current page? - but again that only tells you the overall template file being used. If a template part is responsible for the part of the code you're trying to modify, you'll have to follow the breadcrumb trail to figure out which file is the one you're looking for.


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.