【问题标题】:Get The Category Name & Category Link Inside The Loop In WordPress获取 WordPress 循环内的类别名称和类别链接
【发布时间】:2010-03-08 16:26:20
【问题描述】:

有没有办法在 wordpress 循环内分别获取类别名称和类别页面链接。我也没有类别的 ID,我想显示图像而不是类别名称,因此 the_category() 对我不起作用。

谢谢

感谢所有答案..

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    你可以使用:

    $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()时,这两个东西没有任何标签。
    【解决方案2】:

    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
    }
    

    【讨论】:

    • 假设我有 50 个类别,我想在一个页面上只显示 10 个类别,在下一页上显示下一个 10 个类别,在下一页上显示下一个 10 个导航.. 那么将编码什么。 ??
    • 这非常有帮助。谢谢
    【解决方案3】:

    循环中

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

    【讨论】:

      【解决方案4】:

      我认为 Strateg 的代码应该这样更改:

       <?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 应该是 $categories,那么它对我有用

      【讨论】:

        【解决方案5】:

        这段代码很好,只是你忘了放另一个;在链接的末尾

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

        应该是

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

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-09
        • 2021-09-14
        • 2018-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多