在Foreach循环中显示帖子类别

时间:2014-05-01 作者:Hussain Ansari

我试图在foreach循环中显示帖子类别。我有所有的元素显示正确,除了类别。以下是我的功能:

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

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

$output = \'<ul>\';
foreach ( $authors_posts as $authors_post ) {
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $authors_post->ID ), \'related-author\' );
    $output .= \'<li>
    <a class="title" href="\' . get_permalink( $authors_post->ID ) . \'">
    <strong>\' . apply_filters( \'the_title\', $authors_post->post_title, $authors_post->ID ) . \'</strong>
    <img src="\'.$image[0].\'"> 
    </a>
    <span>\'.get_the_time(\'m.d.y\').\'</span>\'.the_category(\'\', \'\', $authors_post->ID).\'
    </li>\';
}
$output .= \'</ul>\';

return $output;}
按照以下链接查看电流输出。https://www.evernote.com/shard/s250/sh/0e3c6bf9-5676-4d56-acdf-ee6f53285acf/fa43094ca54f018c0e517b4e678d4231

如何在html输出下包装\\u category函数?

2 个回复
最合适的回答,由SO网友:cybmeta 整理而成

foreach 循环来自get_posts(), 你需要跑步setup_postdata() 对于每个帖子,如果你想在循环中使用模板标签the_category(), 如果没有,你可能会有意想不到的行为。此外,您还需要运行wp_reset_postdata()foreach 环请注意the_category 而类似的模板标记实际上会显示/回显输出,因此,如果要将输出串联成字符串,应该使用get_the_category() 相反

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

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

    $output = \'<ul>\';

    foreach ( $authors_posts as $authors_post ) {

        // Build a comma separated categories list
        // You can customize as needed
        // or use get_the_category_list() for quick and delimited list of categories
        // http://codex.wordpress.org/Function_Reference/get_the_category_list
        $categories = get_the_category($authors_post->ID);
        $categories_string = \'\';
        $separator = \', \';
        if($categories) {
            foreach($categories as $category){
                $categories_string .= \'<a href="\'.get_category_link( $category->term_id ).\'" title="\' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . \'">\'.$category->cat_name.\'</a>\'.$separator;
            }
            $categories_string = trim($categories_string, $separator);
        }

        $image = wp_get_attachment_image_src( get_post_thumbnail_id($authors_post->ID), \'related-author\' );
        $output .= \'<li>
        <a class="title" href="\' . get_permalink($authors_post->ID) . \'">
        <strong>\' . apply_filters( \'the_title\', $authors_post->post_title, $authors_post->ID ) . \'</strong>
        <img src="\'.$image[0].\'"> 
        </a>
        <span>\'.get_the_time(\'m.d.y\', $authors_post->ID ).\'</span>\'.$categories_string.\'
        </li>\';
    }

    $output .= \'</ul>\';

    return $output;
}

SO网友:Maidul

the_category() 函数必须在循环内使用。您可以使用get_the_category() 函数instate。

所以新的代码是

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

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

    $output = \'<ul>\';
    foreach ( $authors_posts as $authors_post ) {
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $authors_post->ID ), \'related-author\' );
    }       
        $output .= \'<li>
        <a class="title" href="\' . get_permalink( $authors_post->ID ) . \'">
        <strong>\' . apply_filters( \'the_title\', $authors_post->post_title, $authors_post->ID ) . \'</strong>
        <img src="\'.$image[0].\'"> 
        </a>
        <span>\'.get_the_time(\'m.d.y\').\'</span>\'.get_the_category_list(\', \', \'\', $authors_post->ID).\'
        </li>\';
    $output .= \'</ul>\';

    return $output;
    }  

结束

相关推荐

函数中的函数错误。php?

我在自定义主题中使用以下函数通过AJAX加载帖子。但由于某种原因,当我将主题上传到一个实时服务器时,它打破了WP管理小部件的拖放,并显示了死亡的白色屏幕。我不知道我做错了什么。我的屁股上没有空格。php<?php function mm_get_all_informations() { ?> <table class=\"table results\"> <thead> &