首页 > 解决方案 > Wordpress 中的短代码和 Ajax 问题

问题描述

我正在尝试在 WordPress 中使用短代码,但它们在 ajax 调用后不起作用(例如,当我单击站点的另一个页面时,ajax 会获取另一个文件file.php并用它替换登录页面的中心部分)。问题是当页面加载时,短代码在插入代码时会被打印出来。例如,我[instafeed]应该显示我的 Instagram 提要,但唯一打印的是"[instafeed]". 短代码也会发生这种情况[bloginfo key='template_url'](我从 css-tricks 获取代码)。

我已经尝试了此处显示的一些解决方案,但它们不起作用。例如,我使用了“bloginfo”的代码,它采用了主题文件夹的路径

//StackOverflow solution from https://stackoverflow.com/questions/26069355/call-wordpress-shortcode-with-ajax
add_action( 'init', function() {
  ps_register_shortcode_ajax( 'digwp_bloginfo_shortcode', 
'digwp_bloginfo_shortcode' );
} );

function ps_register_shortcode_ajax( $callable, $action ) {

  if ( empty( $_POST['action'] ) || $_POST['action'] != $action )
    return;

  call_user_func( $callable );
}
//End of StackOverflow solution

//Code from https://css-tricks.com/snippets/wordpress/bloginfo-shortcode/
function digwp_bloginfo_shortcode( $atts ) {
  extract(shortcode_atts(array(
       'key' => '',
   ), $atts));
   return get_bloginfo($key);
}

add_shortcode('bloginfo', 'digwp_bloginfo_shortcode');

我希望输出path_of_directory_of_theme/[bloginfo key='template_url']

标签: phphtmlajaxwordpressshortcode

解决方案


推荐阅读