【问题标题】:php foreach loop echo, print, return dillemaphp foreach 循环回显、打印、返回困境
【发布时间】:2016-05-21 23:35:39
【问题描述】:

这里是新手。

我正在编写这个 wordpress 插件,以便我可以使用 get_posts() 作为简码。

function getposts_func($atts) {
    $atts = shortcode_atts( array('category' => '',), $atts, 'get_posts' );
    $cat=$atts['category'];
    global $post;
    $args = array(
                'category'      => $cat,
                'numberposts'   => -1,
                'order'         => 'ASC',
                );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post);
        $post_permalink = get_permalink();
        $post_title = get_the_title();
        echo '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';
    endforeach;
    wp_reset_postdata();} add_shortcode( 'get_posts', 'getposts_func' );

问题:它在实际内容之前输出。我在某处读到这是因为 ECHO,我需要使用 RETURN。但是,如果我使用 return,它会中断循环并且只输出一篇文章。我也尝试使用 PRINT,但它与 ECHO 基本相同。

我的理论是我需要将值作为数组返回。但我不完全知道如何做到这一点。我尝试使用 $output[] 缓冲区,但失败得很惨。

有帮助吗?

【问题讨论】:

  • 就像我说的我不是 WP 开发人员。在我看来,您返回的内容取决于了解 add_shortcode 的期望 See the Manual
  • @RiggsFolly 您的第一条评论似乎奏效了。抱歉,我认为它没有(实际上我只是忘了刷新)。哈哈。傻我。
  • 哦,对了,我已经取消删除我的答案,所以你可以再看一遍。

标签: php wordpress foreach return echo


【解决方案1】:

如果您将 foreach 部分更改为使用变量作为返回值,您将获得相关信息。

   $result = '';
    foreach( $myposts as $post ) : setup_postdata($post);
        $post_permalink = get_permalink();
        $post_title = get_the_title();
        $result .= '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';
    endforeach;
return $result;

【讨论】:

    【解决方案2】:

    如果 return 实际上是您必须编码的方式,那么只需将所有行保存在一个变量中,以便在流程结束时返回

    function getposts_func($atts) {
    
        $htm = '';
    
        $atts = shortcode_atts( array('category' => '',), $atts, 'get_posts' );
        $cat=$atts['category'];
        global $post;
        $args = array(
                    'category'      => $cat,
                    'numberposts'   => -1,
                    'order'         => 'ASC',
                    );
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) : 
            setup_postdata($post);
            $post_permalink = get_permalink();
            $post_title = get_the_title();
    
            $htm .= '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';
    
        endforeach;
        wp_reset_postdata();
    
        return $htm;
    } 
    
    add_shortcode( 'get_posts', 'getposts_func' );
    

    如果事实上你希望它是一个返回的数组(我不是 WP 专家)

    function getposts_func($atts) {
    
        $htm = array();
    
        $atts = shortcode_atts( array('category' => '',), $atts, 'get_posts' );
        $cat=$atts['category'];
        global $post;
        $args = array(
                    'category'      => $cat,
                    'numberposts'   => -1,
                    'order'         => 'ASC',
                    );
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) : 
            setup_postdata($post);
            $post_permalink = get_permalink();
            $post_title = get_the_title();
    
            $htm[] = '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';
    
        endforeach;
        wp_reset_postdata();
    
        return $htm;
    } 
    
    add_shortcode( 'get_posts', 'getposts_func' );
    

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2017-03-10
      相关资源
      最近更新 更多