Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a Wordpress post's category without any markup, just plain text?

I am creating a wordpress theme and I each category in theme has a designated colour. I want the wordpress code of simply printing out the name of the category into the id of the post so it can be set to the corresponding colour however every code I try gives html markup with it that messes up my code. Below is how I want this to work.

<article> <!-- POST -->

<h1>Post title</h1>

<h2 id="***Here is where I want to insert the code to print the post's category id***>Category Name (Same code as in the ID)</h2>

<img *featured image*>

<p>blah blah article summary</p>

</article>

Any help will be really appreciated!

like image 621
user3479267 Avatar asked Dec 15 '22 00:12

user3479267


2 Answers

I think you want this in your code...

 <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>

Assuming you only have one category per post, should work just fine.

like image 118
Charlie74 Avatar answered Dec 29 '22 00:12

Charlie74


you can use

<?php single_cat_title(); //prints category in plain text ?>

Codex

or

<?php the_category(' '); //displays category with link ?>

Codex

like image 23
Moishy Avatar answered Dec 29 '22 00:12

Moishy