0

我很难理解为什么.=操作员将我的代码输出到我想要的地方。它应该在列表元素之间。

这是PHP

<?php 
    function tcc_display_carousel() {

        $reval  = '<div id="tcc_carousel">';
        $reval .= '<ul class="bjqs">';

        $args = array('post__not_in' => array(133), 
                      'post_type' => 'tcc_carousel', 
                      'posts_per_page' => ''. $tcc_ppp .'', 
                      'order' => 'ASC');

        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) : 
            while (  $loop->have_posts() ) :  
                $loop->the_post(); 

                $reval .= '<li>';
                $reval .= the_post_thumbnail('tcc-thumbnail');
                $reval .= '</li>';
                //No post displays
            endwhile; 
        else:
            $reval .= '<h2>No posts to display</h2>';
        endif;

        $reval .=  '</ul>';
        $reval .=  '</div>';
        return $reval;  
    }
?>

&这里是它输出的html:

<img class="attachment-tcc-thumbnail wp-post-image" width="882" height="292" alt="01"  src="http://dcs.dev/wp-content/uploads/2013/08/01.png">
<div id="tcc_carousel">
    <ul class="bjqs">
      <li></li>
      </ul>
</div>
</div>

我已经尝试过了,我猜这与它之间的查询有关,但我不知道如何将 $reval 添加到查询中。

我正在为 wordpress 构建一个插件,这就是我在函数中使用它的原因。

4

2 回答 2

2

the_post_thumbnail does not return but echos it out itself

you could use output buffering to capture it if you need to manipulate it

$reval .= '<li>';
ob_start()
   the_post_thumbnail('tcc-thumbnail');
   $thumb = ob_get_contents();
ob_end_clean;
$reval .= $thumb;
$reval .= '</li>';

or as dev-null-dweller mentions you can use get_the_post_thumbnail(null, 'tcc-thumbnail');

$reval .= '<li>';
$reval .= get_the_post_thumbnail(null, 'tcc-thumbnail');
$reval .= '</li>';
于 2013-08-25T17:49:17.357 回答
0

It's probably got something to do with one of the WordPress functions trying to echo a value from a function.

Try doing something like this

function tcc_display_carousel_obj() {
    ob_start();
    tcc_display_carousel();
    $output=ob_get_contents();
    ob_end_clean();
    return $output;
}

Then call your carousel using tcc_display_carousel_obj instead

于 2013-08-25T17:48:16.043 回答