有沒有一種方法獲取類別的名稱和鏈接到類別頁分開在wordpress循環內。我沒有該類別的id,我想顯示圖像而不是類別名稱,因此the_category()不適用於我。獲取類別名稱和類別鏈接裏面的循環在WordPress
感謝
感謝所有的答案..
有沒有一種方法獲取類別的名稱和鏈接到類別頁分開在wordpress循環內。我沒有該類別的id,我想顯示圖像而不是類別名稱,因此the_category()不適用於我。獲取類別名稱和類別鏈接裏面的循環在WordPress
感謝
感謝所有的答案..
get_the_category()
作品。使用這個,你將得到循環當前正在處理的每個帖子的類別對象數組。例如:
//the loop
$categories = get_the_category();
//the loop cont....
var_dump($categories);
array
0 =>
object(stdClass)[191]
public 'term_id' => &string '1' (length=1)
public 'name' => &string 'Uncategorized' (length=13)
public 'slug' => &string 'uncategorized' (length=13)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '1' (length=1)
public 'taxonomy' => string 'category' (length=8)
public 'description' => &string '' (length=0)
public 'parent' => &string '0' (length=1)
public 'count' => &string '1' (length=1)
public 'object_id' => string '66' (length=2)
public 'cat_ID' => &string '1' (length=1)
public 'category_count' => &string '1' (length=1)
public 'category_description' => &string '' (length=0)
public 'cat_name' => &string 'Uncategorized' (length=13)
public 'category_nicename' => &string 'uncategorized' (length=13)
public 'category_parent' => &string '0' (length=1)
1 =>
object(stdClass)[190]
public 'term_id' => &string '3' (length=1)
public 'name' => &string 'asd' (length=3)
public 'slug' => &string 'asd' (length=3)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '3' (length=1)
public 'taxonomy' => string 'category' (length=8)
public 'description' => &string '' (length=0)
public 'parent' => &string '0' (length=1)
public 'count' => &string '1' (length=1)
public 'object_id' => string '66' (length=2)
public 'cat_ID' => &string '3' (length=1)
public 'category_count' => &string '1' (length=1)
public 'category_description' => &string '' (length=0)
public 'cat_name' => &string 'asd' (length=3)
public 'category_nicename' => &string 'asd' (length=3)
public 'category_parent' => &string '0' (length=1)
現在你可以通過每個類別重複,像這樣
foreach($categories as $category){
echo $category->name; //category name
$cat_link = get_category_link($category->cat_ID);
echo '<a href="'.$cat_link.'">'.$category->name.'</a>'; // category link
}
您可以使用:
$category = get_the_category();
echo '<a href="'.get_category_link($category[0]->cat_ID).'"><img src="'.$category[0]->cat_name.'" alt="'.$category[0]->cat_name.'" /></a>';
我認爲這會幫助你
或者:
foreach(get_the_category() as $category)
{
echo '<a href="'.get_category_link($category->cat_ID).'"><img src="'.$category->cat_name.'" alt="'.$category->cat_name.'" /></a>';
}
用get_the_category()即可獲得該類別,並使用get_category_link()即可獲得該類別的鏈接。
我希望這可以幫助你:)在循環
感謝您的回答。 但我不認爲這會對我有用。 我需要知道循環當前正在處理的每個帖子的類別。當存在the_category()時,這兩件事情沒有任何標籤是很奇怪的。 – Aayush 2010-03-08 17:33:26
在循環
<?php
global $post;
$categories = get_the_category($post->ID);
$cat_link = get_category_link($category[0]->cat_ID);
echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>'
?>
我認爲戰略中的代碼應該改變像這樣:
<?php
global $post;
$categories = get_the_category($post->ID);
$cat_link = get_category_link($categories[0]->cat_ID);
echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>'
?>
$ category s應該是$類別,那麼它適用於我
此代碼是好的,除非你忘了把另一個;在鏈路的末端
echo <a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>
應該
echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>' ;
歡迎來到堆棧溢出!這應該是一個評論,而不是一個答案。有了更多的代表,[你將能夠發表評論](http://stackoverflow.com/privileges/comment)。 – 2015-03-12 03:16:46
假設我有一個50類別,我想在一個頁面上只顯示10個類別,在下一個頁面上顯示下一個10,在下一個頁面上顯示下一個10,然後輸入代碼。 ?? – 2013-07-24 07:08:54
這非常有幫助。謝謝 – 2017-10-03 13:35:56