在Category y.php中使用get_the_excerpt()不会显示任何内容

时间:2016-11-16 作者:Mike

使用类别。php模板文件和我可以获得关于帖子的所有其他信息,除了摘录或内容。下面是我使用的代码:

$cat = get_queried_object();
$posts = get_posts(array(\'posts_per_page\' => 15, \'offset\' => 0, \'category\' => $cat->term_id));

                foreach($posts as $post)
                {
                    $postID = $post->ID;
                    $title = get_the_title($postID);
                    $link = get_permalink($postID);
                    $date = get_the_date(\'M. j, Y\', $postID);
                    $authors = coauthors_posts_links(null, null, null, null, false);
                    $excerpt = get_the_excerpt($postID);
                    $thumb = get_the_post_thumbnail($post, array(\'class\' => \'front-page-tease-sm\'));

                    echo \'<div class="category-post">\';

                    // Left box - date
                    printf(\'<div class="date">%1$s</div>\', esc_attr($date));

                    // Middle box flex - headline, author, and excerpt
                    echo \'<div class="category-post-info">\';
                    printf(\'<a class="category-headline" href="%1$s">%2$s</a>\', esc_attr($link), esc_html($title));
                    printf(\'<div class="category-author">By %1$s</div>\', $authors);
                    printf(\'<div class="category-tease">%1$s</div>\', $excerpt);
                    echo \'</div>\';

                    // Right box - thumbnail
                    printf($thumb);

                    echo \'</div>\';
                }
除了摘录之外,这段代码完全符合我的预期。它不会为摘录打印任何内容,甚至不会打印HTML标记。

2 个回复
SO网友:Syed Fakhar Abbas

我已经检查了代码。您的代码中存在一些问题:

没有任何方法“coauthors\\u posts\\u links”,但WordPress提供了一种方法the_author_posts_link();.$thumb = get_the_post_thumbnail($post, \'thumbnail\',array(\'class\' => \'front-page-tease-sm\')); 我已经更新了代码。对于“coauthors\\u posts\\u links()”不知道您想在这里做什么?


$cat = get_queried_object();
$posts = get_posts(array(\'posts_per_page\' => 15, \'offset\' => 0, \'category\' => 1));

                foreach($posts as $post)
                {
                    $postID = $post->ID;
                    $title = get_the_title($postID);
                    $link = get_permalink($postID);
                    $date = get_the_date(\'M. j, Y\', $postID);
                    $authors = \'\';
                    $excerpt = get_the_excerpt($postID);
                    $thumb = get_the_post_thumbnail($post, \'thumbnail\',array(\'class\' => \'front-page-tease-sm\'));

                    echo \'\';

                    // Left box - date
                    printf(\'%1$s\', esc_attr($date));

                    // Middle box flex - headline, author, and excerpt
                    echo \'\';
                    printf(\'%2$s\', esc_attr($link), esc_html($title));
                    printf(\'By %1$s\', $authors);
                    printf(\'%1$s\', $excerpt);
                    echo \'\';

                    // Right box - thumbnail
                    printf($thumb);

                    echo \'\';
                }

SO网友:rudtek

我要说的是,你对摘录进行了筛选。

你有没有试过用回声代替打印?

<?php echo \'here is my excerpt: \'.$excerpt;?>
此外,如果某处有一个过滤器,但您找不到,您可以尝试:

//$excerpt = get_the_excerpt(); // This doesn\'t work

// Let\'s do it like the get_the_excerpt filter function in the core does
// Get Excerpt directly without filters.
$excerpt = $post->post_excerpt;

// Choose what to do based on whether it\'s empty or not
if ($excerpt != \'\') echo $excerpt;
else echo \'some fallback\';