简体   繁体   English

以Wordpress简码返回函数

[英]Return a Function in Wordpress Shortcode

Recently I have been playing around with Wordpress Shortcode. 最近,我一直在玩Wordpress Shortcode。 When following this tutorial (changed a few things, of course), I found that even without returning anything (return $return_string; - part) my code is still working as intended. 遵循教程(当然,做了一些改动)时,我发现即使不返回任何内容(返回$ return_string;-部分),我的代码仍可以按预期工作。

I opened PHP manual and it says: 我打开了PHP手册,上面写着:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. 如果从函数内部调用,则return语句将立即结束当前函数的执行,并将其参数作为函数调用的值返回。 return will also end the execution of an eval() statement or script file. return也将结束eval()语句或脚本文件的执行。

My question: 我的问题:

Since looks like there is nothing broken, should I keep doing what I do, or should I "return" something from my function (there is actually a downside from not "return-ing" anything I just haven't seen, yet) ? 既然看起来没有什么坏处,那么我应该继续做我应该做的事情,还是应该从函数中“退还”某些东西(实际上,不“退还”我从未见过的任何东西是不利的)?

Edit: The code, just in case.. no "return", still processing the shortcode / loop 编辑:代码,以防万一..没有“返回”,仍在处理简码/循环

function looping_cat($atts, $content) {
    extract(shortcode_atts(array(
      "query" => '',
      "category" => ''
    ), $atts));

    $wp_query = new WP_Query();

    if(!empty($category)){
      $query .= '&category_name='.$category;
    }
    $wp_query->query($query);
    ?>

      <ul>
      <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
          <li>
              <div>
                  <?php the_post_thumbnail(); ?>

                   <h2><?php the_title() ?></h2>
              </div>
               <?php the_excerpt(); ?>
              </div>
          </li>
      <?php endwhile; wp_reset_query(); ?>
      </ul>
    <?php 
}

If you don't specify a return statement, null will be returned. 如果不指定return语句,则将返回null See this question . 看到这个问题

If it fits your goals that nothing will be displayed for your shortcode, it is fine to omit the return statement. 如果符合您的目标,即您的短代码将不显示任何内容,则可以省略return语句。

For readability purposes I recommend to use return statements everywhere, even if you are returning null . 出于可读性考虑,即使您返回null ,我也建议在各处使用return语句。

Edit: 编辑:

Now I understand what do you mean by returning nothing . 现在我明白你什么都不回报了 Actually you're inlining the HTML in your shortcut function. 实际上,您是在快捷功能中内联 HTML。

Actually, I still prefer to collect and return all content as a string , because it gives you more control of handling the content. 实际上,我仍然更喜欢以字符串形式 收集和返回所有内容,因为它使您可以更好地控制内容的处理。 If a certain condition happens later, you may still decide to return something else. 如果以后发生某种情况,您仍然可以决定退还其他东西。 It may also "disturb" WP, because inlining will output the content immediately , while returning the content enabled WP to decide when (and where) will it show. 它也可能会“打扰” WP,因为内联会立即输出内容,同时返回启用了内容的WP来决定何时(以及在何处)显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM