DESCRIPTION
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:
Example
Europe
Italy
Rome
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
wp-content/uploads/cities/
GOAL
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