php - 为 AMP 页面交换 HTML
问题描述
有没有办法更改 AMP 页面中的 HTML?例子:
使每个<span class="example1">...</span>
to<a href="example1">...</a>
甚至更好,将 Wordpress 中的特定短代码更改为<a href="example1">...</a>
?
解决方案
我找到了切换简码的解决方案:
// AMP change thrive-shortcode to landingpage link
function add_shortcode_amp($content) {
if (function_exists( 'is_amp_endpoint' ) && is_amp_endpoint()) {
/// Define shortcode-name
$mb_shortc = 'thrive_2step';
/// Define Mappings (thrive_2step id => Target URL for links in AMP)
$ampMappings = array(
"17503" => 'https://www.test.de/schreibtisch-workout-2',
"17505" => 'https://www.test.de/merkmale-arbeitsplatz-kostenlos',
"17506" => 'https://www.test.de/hoehenverstellbarer-schreibtisch-rentenverischerung-antrag');
/// Init Regex arrays
$mb_rexp = array();
$subst = array();
foreach ($ampMappings as $key => $value) {
$mb_rexp[] = '/\[('.$mb_shortc.').*?.id=[\'"]'.$key.'[\'"].*?\](.*?)\[\/\1\]?/';
$subst[] = '<a href="'.$value.'">${2}</a>';
}
/// Process Content
return preg_replace($mb_rexp, $subst, $content);
}
return $content;
}
add_filter( 'the_content', 'add_shortcode_amp', 6);
function mbtest_hello_world() {
return '<a>Hello World</a>';
}
add_shortcode('AMPTEST', 'mbtest_hello_world');
function shortcode_switch4amp( $atts) {
extract( shortcode_atts( array(
'regular' => 'regular',
'amp' => 'amp'
), $atts ) );
if (function_exists( 'is_amp_endpoint' ) && is_amp_endpoint()) {
return do_shortcode(str_replace(array("{","}"), array("[","]"),$amp));
} else {
return do_shortcode(str_replace(array("{","}"), array("[","]"), $regular));
}
}
add_shortcode('switch4amp', 'shortcode_switch4amp');
推荐阅读
- pandas - 删除非英文数据
- javascript - 如何在静态页面和生成的页面之间共享查询?
- c++ - 在控制台中“垂直”输出向量的向量内容
- php - 通过模型关系删除laravel中的图像
- php - 对象数组的数组交集
- dll - 在一台 PC 上无法使用 PyInstaller 找到 DLL,但可以在另一台 PC 上找到
- python - 如何根据另一个数据框中的组在熊猫数据框中创建指标列?
- reactjs - 两个不同父母的孩子可以在 React 中拥有相同的键吗
- excel - UserForm ComboBox 从动态工作表列表填充,从第一个 ComboBox 的选择中添加第二个 ComboBox
- javascript - React Redux:我的 React 组件没有收到更新的数组作为道具