WP Query - Post Thumbnail

时间:2014-01-23 作者:user45531

我正在使用插件:Series 默认情况下,它只获取链接的帖子标题。我想添加后缩略图以及。我能够显示缩略图,但它们显示在小部件的外部。我添加了:\\u post\\u缩略图(“缩略图”)部分。完整模板如下所示。

谢谢你的帮助!

/**
 * Displays a list of posts by series ID.
 *
 * @since  0.1.0
 * @param  array   $args
 * @return string
 */
function series_list_posts( $args = array() ) {

    if ( empty( $args[\'series\'] ) )
        return;

    $out     = \'\';
    $post_id = 0;

    if ( in_the_loop() )
        $post_id = get_the_ID();

    else if ( is_singular() )
        $post_id = get_queried_object_id();

    $defaults = array(
        \'series\'         => \'\', // term slug
        \'order\'          => \'ASC\',
        \'orderby\'        => \'date\',
        \'posts_per_page\' => -1,
        \'echo\'           => true,
    );

    $args = wp_parse_args( $args, $defaults );

    $loop = new WP_Query( $args );

    if ( $loop->have_posts() ) {

        $out .= \'<ul class="series-list">\';

        while ( $loop->have_posts() ) {

            $loop->the_post();

            $out .= $post_id === get_the_ID() ? the_title( \'<li>\' . the_post_thumbnail(\'thumbnail\') .  \'</li>\', false ) : the_title( \'<li>\' . the_post_thumbnail(\'thumbnail\') . \'<a href="\' . get_permalink() . \'">\', \'</a></li>\', false );
        }

        $out .= \'</ul>\';
    }

    wp_reset_postdata();

    if ( false === $args[\'echo\'] )
        return $out;

    echo $out;
}

/**
 * Displays a list of posts related to the post by the first series.
 *
 * @since  0.1.0
 * @param  array  $args
 * @return string
 */
function series_list_related( $args = array() ) {

    $post_id = 0;

    if ( in_the_loop() )
        $post_id = get_the_ID();

    else if ( is_singular() )
        $post_id = get_queried_object_id();

    if ( !empty( $post_id ) )
        $series = get_the_terms( $post_id, \'series\' );

    if ( empty( $series ) )
        return;

    $series = reset( $series );

    $args[\'series\'] = $series->slug;

    return series_list_posts( $args );
}

/* === DEPRECATED === */

/**
 * @since      0.1.0
 * @deprecated 0.2.0
 */
function get_series_feed_link( $cat_id, $feed = \'\' ) {
    _deprecated_function( __FUNCTION__, \'0.2.0\', \'get_term_feed_link\' );
    return get_term_feed_link( $term_id, \'series\', $feed );
}

/**
 * @since      0.1.0
 * @deprecated 0.2.0
 */
function is_series( $slug = false ) {
    _deprecated_function( __FUNCTION__, \'0.2.0\', \'is_tax\' );
    return is_tax( \'series\', $slug );
}

/**
 * @since      0.1.0
 * @deprecated 0.2.0
 */
function in_series( $series, $_post = null ) {
    _deprecated_function( __FUNCTION__, \'0.2.0\', \'has_term\' );
    return has_term( $series, \'series\', $_post );
}

/**
 * @since      0.1.0
 * @deprecated 0.2.0
 */
function create_series_taxonomy() {
    _deprecated_function( __FUNCTION__, \'0.2.0\', \'\' );
}

/**
 * @since      0.1.0
 * @deprecated 0.2.0
 */
function series_register_widgets() {
    _deprecated_function( __FUNCTION__, \'0.2.0\', \'\' );
}

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

the_post_thumbnail 立即将数据打印到屏幕上。它不会返回可以连接到$out 变量

使用get_the_post_thumbnail 相反

确保你的论点正确。它们与the_post_thumbnail. 应该是这样的:

get_the_post_thumbnail(get_the_ID(),\'thumbnail\');

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post