【问题标题】:Count the number of times a WordPress shortcode is used in a post计算帖子中使用 WordPress 短代码的次数
【发布时间】:2014-12-16 14:49:39
【问题描述】:

我有以下 WordPress 短代码功能:

function wp_shortcode() {
 static $i=1;
 $return = '['.$i.']';
 $i++;
 return $return;
}
add_shortcode('shortcode', 'wp_shortcode');

这很好用。每次在文章中调用函数时,$i 变量都会递增。所以对于

[shortcode][shortcode][shortcode][shortcode]

输出符合预期

[1][2][3][4]

但是

如果文章有多个页面,则计数器会在下一个文章页面上重置。所以对于:

[shortcode][shortcode][shortcode][shortcode]
<!--nextpage-->
[shortcode][shortcode][shortcode][shortcode]

输出是

[1][2][3][4]
<!--nextpage-->
[1][2][3][4]

而不是想要的输出:

[1][2][3][4]
<!--nextpage-->
[5][6][7][8]

无论如何要改变这个?

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案1】:

    因此,在通过预处理 the_content 并在渲染短代码之前将 $atts 添加到 shorcode 中研究了更多 @diggy 的想法之后,我想出了这个:

    function my_shortcode_content( $content ) {
     //check to see if the post has your shortcode, skip do nothing if not found
     if( has_shortcode( $content, 'shortcode' ) ):
    
      //replace any shortcodes to the basic form ([shortcode 1] back to [shortcode])
      //usefull when the post is edited and more shortcodes are added
      $content = preg_replace('/shortcode .]/', 'shortcode]', $content);
    
      //loop the content and replace the basic shortcodes with the incremented value
      $content = preg_replace_callback('/shortocode]/', 'rep_count', $content);
    
     endif;
    
    return $content;
    }
    add_filter( 'content_save_pre' , 'my_shortcode_content' , 10, 1);
    
    function rep_count($matches) {
     static $i = 1;
    return 'shortcode ' . $i++ .']';
    }
    

    【讨论】:

      【解决方案2】:

      好问题。下面是一个可能的解决方案,但并不完全优雅,因为它要求您在每个页面上都有固定数量的短代码,例如4:

      add_shortcode( 'shortcode', 'wp_shortcode' );
      function wp_shortcode() {
          global $page;
          $spp = 4;
          static $i = 1;
          $ii = $i + ( ( $page - 1 ) * $spp );
          $return = '<a href="#f'.$ii.'" name="r'.$ii.'">['.$ii.']</a>';
          $i++;
          return $return;
      }
      

      【讨论】:

      • 不幸的是,每篇文章/页面的短代码数量不同
      • 可以用另一个变量初始化一个静态变量吗?所以,如果我像这样 [shortcode 5] 向短代码添加一个属性,在函数中可以静态 $i = $atts[0] 基本上是静态 i = 5?我试过了,但我得到了一个错误,所以也许我需要调整一些东西。
      • PHP 文档说:“像任何其他 PHP 静态变量一样,静态属性只能使用文字或常量进行初始化;不允许使用表达式。”像这样的东西可以工作,但只查看并传递页面上第一个短代码的 attr:if( ! defined( 'SH_C' ) ) define( 'SH_C', $atts[0] ); static $i = SH_C; 但是如果你开始将数字添加为 atts,那么以编程方式递增就没有意义了,不是吗?预处理 the_content 并动态添加 atts 可能是一个更好的主意,即在处理和呈现短代码之前?
      【解决方案3】:

      如果您只需要计算页面/帖子中的总标签,您可以使用:

      function count_shortcodes( $content, $tag ) {
          if ( false === strpos( $content, '[' ) ) {
              return 0;
          }
      
          if ( shortcode_exists( $tag ) ) {
              preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
              if ( empty( $matches ) )
                  return 0;
      
              $count = 0;
              foreach ( $matches as $shortcode ) {
                  if ( $tag === $shortcode[2] ) {
                      $count++;
                  } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
                      $count++;
                  }
              }
      
              return $count;
          }
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        这是发生的,因为在每次页面加载时您都重新定义了您的函数,所以static $i=1; 将始终执行。

        也许您想使用$_SESSION 变量。但不要忘记session_start() 回调中某处的init

        function wp_shortcode() {
            if (empty($_SESSION["myCounter"])) {
                $_SESSION["myCounter"] = 1;
            }
            $return = '<a href="#f' . $_SESSION["myCounter"] . '" name="r' 
                . $_SESSION["myCounter"] . '">[' 
                . $_SESSION["myCounter"] . ']</a>';
            $_SESSION["myCounter"]++;
            return $return;
        }
        add_shortcode('shortcode', 'wp_shortcode');
        

        设置会话,放入functions.php

        add_action('init', 'my_init');
        function my_init() {
            if (!session_id()) {
                session_start();
            }
            //Other init here
        }
        

        【讨论】:

        • 这行得通,因为计数器一直在增加,但是页面的每次刷新都会使计数器增加,因此当用户离开文章时没有会话结束,这是行不通的。此解决方案起作用的唯一方法是发布特定会话。因此,当您开始阅读一篇文章时,会话开始,当您阅读另一篇文章时,会话结束。
        猜你喜欢
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-12
        • 1970-01-01
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        相关资源
        最近更新 更多