A
A
Aqualtis2015-04-18 18:55:56
Taxonomy
Aqualtis, 2015-04-18 18:55:56

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>

What needs to be substituted so that instead of X there is a number - the rubric ID.
Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Vorotnev, 2015-04-19
@HeadOnFire

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>

R
Roman, 2015-04-18
@llgruff

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 question

Ask a Question

731 491 924 answers to any question