【问题标题】:Get the_category outside of loop在循环之外获取 the_category
【发布时间】:2013-04-08 05:14:27
【问题描述】:

我正在开发一个具有自定义作者页面的网站,并且在作者页面上它有一个最近的作者帖子小部件,该小部件显示该作者的其他帖子。该站点有多个作者,因此每个帖子都不同,我还没有找到可以做到这一点的插件。我正在尝试显示帖子缩略图、标题和类别名称。

我正在使用一个显示标题和缩略图的函数,但它没有 category 。我尝试添加类别: the_category(' ','multiple',$authors_post->ID) 不幸的是它显示了第一个 li 中的所有类别。而不是每个帖子。

这是我正在使用的:

function get_related_author_posts() {
global $authordata, $post;

$authors_posts = get_posts( array( 'author' => $authordata->ID,
  'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );

$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
    $output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' )
       . '<p>' . the_category(' ','multiple',$authors_post->ID)
       . '</p> <a href="' . get_permalink( $authors_post->ID )
       . '">' . apply_filters( 'the_title', $authors_post->post_title,
       $authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';

return $output;

}

任何帮助将不胜感激, 谢谢!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    请试试这个代码,

    <?php
     $cat=1;
     $yourcat = get_category($cat);
     if ($yourcat) 
     {
        echo '<h2>' . $yourcat->name . '</h2>';
     }
    ?>
    

    要获取类别名称,

    【讨论】:

      【解决方案2】:

      尝试一些类似的东西

      function get_related_author_posts() {
      global $authordata, $post;
      
      $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
      
      $output = '<ul>';
      foreach ( $authors_posts as $authors_post ) {
      $category = get_the_category($authors_post->ID);
      foreach($category as $c){
      $cat=$c->cat_name.' '.$cat;
      }
      
          $output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' ) . '<p>' . $cat . '</p> <a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
      }
      $output .= '</ul>';
      
      return $output;
      }
      

      希望这对你有用..

      【讨论】:

      • 谢谢!这让我更接近我的目标,它显示类别名称,不,我只需要它输出到类别的链接。
      猜你喜欢
      • 2016-11-30
      • 2015-12-26
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多