Answer the question
In order to leave comments, you need to log in
How to display the id of the current category in Wordpress?
Hello!
I really need help.
CMS WordPress. In general, there is such an entry in the category.php and single.php files:
<div class="class" style="background:url(адрес с темой/images/papka/X.jpg) no-repeat;"></div>
Answer the question
In order to leave comments, you need to log in
Roman Gruff writes correctly, but if there is only one category, or you need to take the id of only the first of them (after all, only one background needs to be inserted), then it’s better like this:
<?php $category = get_the_category(); ?>
<div class="class" style="background:url(адрес с темой/images/papka/<?php echo $category[0]->term_id; ?>.jpg) no-repeat;"></div>
We create a picture for each category.
Images should have the same names as the category IDs (for example 12.jpg) and be located in the root of the site in the images/papka/ folder.
The code needs to be used inside the Loop:
<?php foreach((get_the_category()) as $category) {
echo '<div class="class" style="background:url(http://example.com/images/papka/' . $category->cat_ID . '.jpg) no-repeat;"></div>';
} ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question