2014-04-01 57 views
2

我正在創建一個WordPress主題,並且我在主題中的每個類別都有指定的顏色。我想要的wordpress代碼只是打印出類別的名稱到帖子的ID,所以它可以設置爲相應的顏色,但是我嘗試的每個代碼都會給它添加html標記,這會弄亂我的代碼。以下是我希望如何工作。如何在沒有任何標記的情況下打印Wordpress帖子的分類,只是純文本?

<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> 

任何幫助將非常感激!

+0

是這個類別中的頁面?或在列出多個帖子的頁面上? – Mark

回答

5

我覺得你在你的代碼要這個......

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

假設你只有每個崗位一類,應該只是罰款。

+1

完美!正是我在看什麼。謝謝。 – user3479267

+0

如何使用此代碼獲取永久鏈接的類別? @ Charlie74 – user3479267

+0

我相信這是$ category-> term_id。 – Charlie74

0

可以使用

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

Codex

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

Codex

相關問題