在同一页中执行两次快捷码

时间:2015-01-15 作者:Daniele Pavinato

我需要一个简短问题的帮助。

我有以下代码:

add_shortcode("archive", "archive_render");
function archive_render($atts) {

   extract(shortcode_atts(array(
      "rientro" => "no",
      "year" => "",
   ), $atts));

   global $wpdb;

   $rientro == "si" ? $rientro = "yes" : "no";

   $query = "SELECT event_name FROM wp_em_events WHERE EXTRACT(YEAR FROM event_end_date) = ".$year." AND event_end_date < CURDATE()";
   $pasts_event = $wpdb->get_col($query);

   function get_pasts_event( $pasts_event ){

      foreach ( $pasts_event as $past_event_slug ) {
         $output .= "<li><a href=\'".get_site_url()."/eventi/".$past_event_slug."\'>$past_event_slug</a></li>";
      }

      return $output;
   } 

   $string = \'[one_third last="\'.$rientro.\'" class="" id=""][accordian class="" id=""][toggle title="\'.$year.\'" open="no"]<ul>\'.get_pasts_event($pasts_event).\'</ul>[/toggle][/accordian][/one_third]\';

   echo do_shortcode( $string );
}
如果在WP编辑器中添加两次或更多,则只会执行第一次,页面停止工作。

有人知道为什么我的短代码不能被多次使用吗?

1 个回复
SO网友:Privateer

我相信你真正的问题是回显你的数据而不是返回它。

A shortcode should return the output to be drawn, 然后将其绘制以代替短代码。

下面是使用HEREDOC语法进行输出和查询的代码。我相信一旦你习惯了,这会让事情变得更加可读。看见http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Changes to get_past_events:

  • 如果未接收到移动的事件,则跳过处理get_site_url() 循环外调用(每个项目都相同)使用了HEREDOC表示法(为了清晰和清晰的输出)
  • 重命名了该函数,以减少与get\\u past\\u events函数冲突的可能性:

    function do_my_get_past_events( $past_events = array() ) {
       $output = \'\';
    
       if ( is_array($past_events) && 0 < count($past_events) ) {
          $site_url = get_site_url();
          foreach ( $past_events as $past_event_slug ) {
             $output .= <<<HTML
    <li><a href="{$site_url}/eventi/{$past_event_slug}">{$past_event_slug}</a></li>
    
    HTML;
    
          }
       }
       return $output;
    }
    

    Changes to archive_render:

    <将短代码属性拉入数组(清晰)
  • 声明event_items (因此,如果需要,我们可以跳过处理)
  • 添加prepare 语句(防止sql注入)使用数字,已将HEREDOC用于查询字符串(清晰度)调整rientro 变量(左侧声明-清晰度)
  • 已更改string 变量至custom_short_code (清晰度)
  • 返回最终输出(实际修复)
  • 添加了可选块,以防需要默认值
  • 重命名该函数,使其不太可能与存档渲染函数发生冲突:

    add_shortcode("archive", "do_my_custom_archive_render");
    function do_my_custom_archive_render($atts) {
    
       $options = shortcode_atts(
          array(
             \'year\' => 0,
             \'rientro\' => \'no\'
          ),
          $atts
       );
    
       $past_event_items = \'\';
    
       if ( !empty( $options[\'year\'] ) ) {
          global $wpdb;
    
          $query = <<<SQL
     SELECT event_name 
     FROM wp_em_events 
     WHERE 
       EXTRACT(YEAR FROM event_end_date) = %d 
       AND event_end_date < CURDATE()
    SQL;
    
          $query = $wpdb->prepare( $query, (int) $options[\'year\'] );
    
          $past_events = $wpdb->get_col($query);
    
          $past_event_items = do_my_get_past_events( $past_events );
       }
    
       $rientro = ( \'si\' == "{$options[\'rientro\']}" )? \'yes\' : \'no\';
    
       # If there are no event items for the year provided the user will get an empty box
       # If that is not what is wanted, uncomment the following 3 lines
       # if ( empty($past_event_items) ) {
       #    $past_event_items = \'<li>No Events Found</li>\';
       # }
    
       $custom_short_code = <<<HTML
    [one_third last="{$rientro}" class="" id=""]
       [accordian class="" id=""]
          [toggle title="{$options[\'year\']}" open="no"]
             <ul>
                {$past_event_items}
             </ul>
          [/toggle]
       [/accordian]
    [/one_third]
    HTML;
    
       return do_shortcode( $custom_short_code );
    }
    
    对于那些不熟悉HEREDOC的人,这里有相同的代码,没有使用HEREDOC。

    function do_my_get_past_events( $past_events = array() ) {
       $output = \'\';
    
       if ( is_array($past_events) && 0 < count($past_events) ) {
          $site_url = get_site_url();
          foreach ( $past_events as $past_event_slug ) {
             $output .= \'<li><a href="\' . $site_url . \'/eventi/\' . $past_event_slug . \'">\' . $past_event_slug . \'</a></li>\' . "\\r\\n";
          }
       }
    
       return $output;
    }
    
    add_shortcode("archive", "do_my_custom_archive_render");
    function do_my_archive_render($atts) {
    
       $options = shortcode_atts(
          array(
             \'year\' => 0,
             \'rientro\' => \'no\'
          ),
          $atts
       );
    
       $past_event_items = \'\';
    
       if ( !empty( $options[\'year\'] ) ) {
          global $wpdb;
    
          $query = "SELECT event_name FROM wp_em_events WHERE EXTRACT(YEAR FROM event_end_date) = %d AND event_end_date < CURDATE()";
    
          $query = $wpdb->prepare( $query, (int)$options[\'year\'] );
    
          $past_events = $wpdb->get_col($query);
    
          $past_event_items = do_my_get_past_events( $past_events );
       }
    
       $rientro = ( \'si\' == "{$options[\'rientro\']}" )? \'yes\' : \'no\';
    
       # Uncomment the following 3 lines if you want.
       # if ( empty($past_event_items) ) {
       #    $past_event_items = \'<li>No Events Found</li>\';
       # }
    
       $custom_short_code = \'[one_third last="\' . $rientro . \'" class="" id=""][accordian class="" id=""][toggle title="\' . $options[\'year\'] . \'" open="no"]<ul>\' . $past_event_items . \'</ul>[/toggle][/accordian][/one_third]\';
    
       return do_shortcode( $custom_short_code );
    }
    

  • 结束

    相关推荐

    Remove echo from shortcode

    我刚刚开始使用短代码,由于缺乏php知识,我无法想出如何在不使用php echo的情况下使用此短代码。有人能帮我修改代码吗?// SPONSORS Shorcode function sponsors_shortcode($atts) { extract(shortcode_atts(array( \"name\" => \"sponsors\", ), $atts)); $args = array( \"