2010-03-08 3 views

Répondre

10

get_the_category() travaux dans la boucle. En utilisant ceci, vous obtiendrez un tableau d'objet de catégorie pour chaque message que la boucle traite actuellement. exemple:

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

vous pouvez maintenant parcourir chaque catégorie, comme si

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 
} 
+0

supposons que j'ai une catégorie 50 et je veux montrer seulement 10 catégorie sur une page et 10 prochaines sur la page suivante et 10 prochaines sur la page suivante avec la navigation .. alors ce qui code. ?? –

+0

C'était extrêmement utile. Merci –

5

Vous pouvez utiliser:

$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>'; 

Je pense que cela vous aidera

Ou:

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>'; 
} 

Avec get_the_category() vous obtenez la catégorie, et avec get_category_link(), vous obtiendrez le lien de la catégorie.

J'espère que cela vous aide :)

+0

Merci pour la réponse. mais je ne pense pas que cela fonctionnerait pour moi. J'ai besoin de connaître la catégorie pour chaque message que la boucle est en train de traiter. C'est bizarre qu'il n'y ait pas de balises pour ces deux choses, quand il y a the_category(). – Aayush

0

En boucle

<?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>' 
?> 
0

Je pense que le code de Strateg devrait être modifié comme ceci:

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

la catégorie $ s devrait être $ catégories, alors cela fonctionne pour moi

0

Ce code est bon sauf que vous avez oublié d'en mettre un autre; à la fin du lien

echo <a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a> 

devrait être

echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>' ; 
+0

Bienvenue dans Stack Overflow! Cela aurait dû être un commentaire, pas une réponse. Avec un peu plus de rep, [vous serez en mesure de poster des commentaires] (http://stackoverflow.com/privileges/comment). –

Questions connexes