dousi8237 2013-06-18 16:44
浏览 56

PHP wordpress在短代码中执行一个函数

I have created a function that gathers all the images from a set of posts. I'm trying to add those images between an opening and closing shortcode that creates a slider.

Here is what I have so far - my php knowledge isn't great. Can anyone point out why this might not be working. Thanks

<?php 
function pplSlider()
{
$my_query = new WP_Query( "category_name=editorial&posts_per_page=-1" ); 

if ( $my_query->have_posts() ) : 

while ( $my_query->have_posts() ) : 
    $my_query->the_post();
    the_post_thumbnail(array(300,300));

    if (($my_query->current_post +1 )< $my_query->post_count)
        echo '/!';

endwhile; 
endif;
}
}
echo do_shortcode('[wpic]' . pplSlider() . '[/wpic]');

?>
  • 写回答

1条回答 默认 最新

  • dpizd08264 2013-06-18 19:38
    关注

    Your function should return the value, instead of using echo.

    One way around it is to return the output buffering, like this:

    function pplSlider(){
        ob_start();
        $my_query = new WP_Query( "category_name=editorial&posts_per_page=-1" ); 
        if ( $my_query->have_posts() ) : 
            while ( $my_query->have_posts() ) : $my_query->the_post();
                the_post_thumbnail(array(300,300));
                if (($my_query->current_post +1 )< $my_query->post_count)
                    echo '/!';
    
            endwhile; 
        endif;
        return ob_get_contents();
    }
    
    echo do_shortcode( sprintf( '[wpic]%s[/wpic]', pplSlider() ) );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?