首页 > 解决方案 > Wordpress - 如何在 1 次短代码执行后读取 Post HTML 输出

问题描述

我有自定义 WP 插件,它注册一些短代码并通常在 Post 中运行它们。一篇文章通常有 2 个或更多这些短代码。我需要确定第一个短代码是否已执行并以某种方式更改了 Post HTML - 所以第二次和下一次运行的短代码不会替换第一次运行的短代码生成的 HTML。

但我不知道怎么做。

我知道,应该通过向短代码添加一些参数来完成,例如 [shortcode first_run="true"],但这是最后一个可接受的解决方案,因为它需要更改以前在 Posts 中生成的短代码。我知道我不能使用 get_the_content() - 它在短代码运行之前显示来自 DB 的 Post 内容。我知道我不能使用 the_content() - 因为它会循环 WP 运行

谢谢你的帮助。

我的代码示例(我猜它并没有太大帮助,但是..只是想象):

function joreviewstable_func( $atts ) {
    
    
    if (is_singular() || is_tag() ) {
            $a = shortcode_atts( array(
            'type' => "",
            'id' => NULL,
            'params' => NULL,
            'limit' => NULL,
            'tablesorter_disable' => NULL,
            'disallow_images' => NULL,
            'product_ids' => NULL,
            'manufacturer' => NULL,
            'manufacturer_serie' => NULL,
            'no_sum' => NULL,
            'custom_order' => NULL,
            'vertical_design' => NULL
        ), $atts );
    
    
        require_once( plugin_dir_path( __FILE__ ) . 'inc/shortcode.php');
    
        $return = jo_reviews_shortcode_generate_table( $a['type'], $a['id'], $a['params'],
                                                        $a['limit'], $a['tablesorter_disable'], $a['disallow_images'],
                                                        $a['product_ids'], $a['manufacturer'], $a['manufacturer_serie'],
                                                        $a['no_sum'], $a['custom_order'], $a['vertical_design'] );
        //$return = "<table><tr><td>data</td></tr></table>";
    
        return $return;
    } else {
      return NULL;
    }
add_shortcode( 'joreviewstable', 'joreviewstable_func' );

jo_reviews_shortcode_generate_table 函数做了一些魔术并将 html 返回给 joreviewstable_func。

但我不知道,如果编辑后的 ​​HTML 包含某些内容,如何将其转换为 strpos。

标签: phpwordpressshortcode

解决方案


您可以通过使用全局变量来做到这一点。

function joreviewstable_func( $atts ) {
  global $first_run;
  if(!empty($first_run)) return;
  $first_run=1;
  //REST LINES HERE
}

推荐阅读