php - 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。
解决方案
您可以通过使用全局变量来做到这一点。
铁
function joreviewstable_func( $atts ) {
global $first_run;
if(!empty($first_run)) return;
$first_run=1;
//REST LINES HERE
}
推荐阅读
- javascript - 如何在电子中获取渲染器控制台的原始数据
- laravel - laravel中的分页没有刷新页面
- minizinc - 为什么 MiniZinc 有时不使用求解器定义的常规约束?
- r - 将 geom_smooth 线添加到图表并出现错误 geom_path:每组仅包含一个观察值。需要调整群体审美吗?
- python - Ignoring tables / charts in text extraction pytesseract python
- javascript - 来自 Mongo 的对象的数组属性未定义
- python-3.x - tkinter not working in python 3(in linux)
- python - Django HTML 无法读取字典的值
- c++ - from vb.net, calling functions in a C++ dll passing WORD parameter and returning result
- nuget - Is there a way to tell NuGet to get the latest version?