I want to get the name of the slug of the last child of the post's category. My categories of my posts represents cities and I am assigning them 3-4 level categories as continent, country, region, and finaly most important category city as you can see here:



I am using this working code to display a category image of the posts which speaks about some city on my index page:

<img src="<?php echo bloginfo('url'); ?>/wp-content/uploads/cities/<?php foreach(get_the_category() as $category) {
echo $category->slug . ' ';} ?>-140x140.jpg" alt=" " />

It is fully working and it is giving me a result something like:

..... wp-content/uploads/cities/europe italy rome -140x140.jpg

I am using a folder cities to keep the city images in the "uploads" folder



I am interested only about the cities subcategory, but the name of the slug contains 3 categories which unfortunately are never in the correct parent order and that's why I would like to make it more simple and I want to only get the name of the last child of those 3 assigned categories/subcategories.

How can I achieve that?

Example of my GOAL - name of the image using the third (the last) child category "representing the city":

......wp-content/uploads/cities/rome -140x140.jpg

2 Answers 2


Create a custom taxonomy for cities, and then use get_the_terms() to display the term slug in the image URL.


If you're only wanting the last slug, you can use PHP for that. Place this at the very top of the file just after get_header() :

    /* Grab the link. */
    $link = $_SERVER['PHP_SELF'];
    /* Separate the url. */
    $array = explode( '/', $link );
    /* Count your way backwards. */
    $last_slug = count( $array ) - 2;
    $the_city = $array[$last_slug];


    /* Grab the link. */
    $link = $_SERVER['REQUEST_URI'];
    /* Separate the url. */
    $array = explode( '/', $link );
    /* Grab the second to last element in the array. */
    $the_city = prev( $array );

USAGE: Use the following for your image tag:

<img src="<?php echo bloginfo('url'); ?>/wp-content/uploads/cities/<?php echo $the_city; ?>-140x140.jpg" alt=" " />
  • Courtney, thank you very much for your respond, but more than taxonomy I was interested about the category, because I have already 2000 posts with assigned categories: continent, country, regions, cities and it was a lot of work to assign them, the problem is that current slug is never in correct parent order, so sometimes it is "rome europe italy" and than it doesn't work, if I will manage to get it work only for the last child of category "rome" it will be easy for me to upload city images with their correct names: as rome-140x140.jpg, right now I have to know this 3 cats long correct slug. Oct 16, 2014 at 4:44
  • Alright... updated the original answer, with a modified version of the second answer. Lol Oct 16, 2014 at 5:29
  • Nothing :( <img src="<?php echo bloginfo('url'); ?>/wp-content/uploads/cities/<?php /* Grab the link. */ $link = $_SERVER['PHP_SELF']; /* Separate the url. */ $array = explode( '/', $link ); /* Count your way backwards. */ $last_slug = count( $array ) - 2; $the_city = $array[$last_slug]; ?>-140x140.jpg" alt=" " /> Oct 16, 2014 at 5:35
  • 4ella.com the website where I use this code, the first image is the city category image Oct 16, 2014 at 5:36
  • I updated my answer to include usage. Oct 16, 2014 at 5:47

Try this function:

function misha_get_last_cat_slug() {

    $categories = get_the_category();
        $last_category = $categories[0];

        foreach($categories as $category) {
            if($category->parent == $last_category->cat_ID ) {
                $last_category = $category;
        return $last_category->slug;


And then use it in your HTML:

<img src="<?php echo bloginfo('url'); ?>/wp-content/uploads/cities/<?php echo misha_get_last_cat_slug() ?>-140x140.jpg" alt=" " />

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.