繁体   English   中英

在 Wordpress 中用 HTML 替换短代码

[英]Replace Shortcodes with HTML in Wordpress

我正在一个 WordPress 网站上工作,该网站一直在使用插件来使用短代码获取亚马逊产品图像。

您只需将书的 asin 插入短代码,如下所示:

[amazon template=image&asin=B00FYY53B8]

当帖子加载时,使用来自亚马逊的 URL 将短代码转换为实际的图像 HTML.... 所以上面的例子将返回

http://ecx.images-amazon.com/images/I/71kIifYTccL._SL1500_.jpg

我正在使用另一个自定义插件来构建帖子的内容,到目前为止我只是使用这个短代码作为帖子内容的一部分。 我想做一些其他的事情,比如设置特色图片等,并且需要那个 URL。

我试过了

echo do_shortcode( '[amazon template=image&asin=B00FYY53B8]' );

但它不会返回任何东西。 有没有办法在插件中“执行”短代码并保存输出?

最终,我还想扩展这个功能,以便我可以用它们的 HTML 输出替换其他/旧的短代码,并将其保存回帖子,基本上消除了在帖子中使用一些短代码。

我有一个在 wordpress 中添加短代码的演示。 我希望它有所帮助。

add_action('plugins_loaded','MOD_load');

function MOD_load() {
     short_enable_form();
}

function short_enable_form(){
    if(!function_exists('add_shortcode')) {
                   return;
    }
    add_shortcode('form_mod' , array(&$this, 'out_put_form'));
}

public function out_put_form(){         
    return 'HTML TEXT';
}

您可以在functions.php 中包含此代码。 并在 content-singular.php 或主题中的类似文件中调用此函数。

 function updatesc_database( $post ) {
 if ( empty($post) ) global $post;
 if ( empty($post) || ! isset($post->post_content) ) return false;
 $content = $post->post_content;
 $shortc1="amazon template=image&asin";
 $shortc2="B00FYY53B8]"; //insert the number variable here
 $shortwh=$shortc1.$shortc2;
 $replace = do_shortcode($shortwh);
 $content = str_replace( $shortwh, $replace, $content );   
 return $content;
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM