正在尝试加载WordPress页面上的类别缩略图

时间:2012-05-05 作者:McGuyverr

我试图在我的主页上显示一些Wordpress类别缩略图,但我的代码似乎破坏了我网站的其余部分。它会杀死页面,我的代码下的任何内容都不会加载。

如果您能提供帮助,我们将不胜感激。

谢谢

<?php

    global $post;
    $temp_post = $post;
    $args = array(\'numberposts\' => 1, \'category\' => 40, \'orderby\' => \'date\', \'order\' => \'DESC\');
    $posts = get_posts($args);
    foreach ($posts as $post) : setup_postdata($post);
    if (has_post_thumbnail()) {
        $thumbnail = get_post_thumbnail($post->ID, \'thumbnail\');
        $title = get_the_title();
        $output = $title . "<br />" . $thumbnail;
    }
    endforeach;
    $post = $temp_post;
    wp_reset_postdata();
    return($output);

?>

EDIT

<?php

global $post;
$temp_post = $post;
$args = array(\'numberposts\' => 6, \'category\' => 227, \'orderby\' => \'date\', \'order\' => \'DESC\');
echo \'<div class="categoryThumbnailList">\';
$posts = get_posts($args);
foreach ($posts as $post) : setup_postdata($post);
if (has_post_thumbnail()) {
    $thumbnail = get_the_post_thumbnail($post->ID, \'thumbnail\');
    $link = get_permalink($post->ID);
    $title = get_the_title();
    echo \'<div class="categoryThumbnailList_item">\';
    echo \'<a href="\' .$link . \'" title="\' .$title . \'">\' .$thumbnail . \'</a><br/>\';
    echo \'<a href="\' .$link . \'" title="\' .$title . \'">\' .$title . \'</a>\';
    echo \'</div>\';
}
endforeach;
echo \'</div>\';
echo \'<div class="categoryThumbnailList_clearer"></div>\';
$post = $temp_post;
wp_reset_postdata();
?>

1 个回复
SO网友:Milo

问题是你使用return 此处不适用,它正在停止模板的执行。你应该echo 您的输出。

结束