简体   繁体   中英

Wordpress do_shortcode with jquery not working

Here is the code

jQuery('#home_page_banner').html('<?php echo do_shortcode("[banner id="6135"]"); ?>');

result:

输入图像描述

try this below code for shortcode

$('#home_page_banner').html(<?php echo do_shortcode('[banner id="6135"]'); ?>);

OR

jQuery('#home_page_banner').html(<?php echo do_shortcode('[banner id="6135"]'); ?>);

No really sure why you would want to do this as it is quite bad practice to use other code to create other code. You could include the shortcode directly to the page template and then just use conditional tags, if you don't want it to be shown on all pages.

Based on the div id you have, is_front_page() would do the trick for you. If for some reason you want to create with jQuery, you could checkout the WP Rest API.

WP Conditional Tags

This full code. `function jsUpdateSize(){ var width = window.innerWidth ||document.documentElement.clientWidth ||document.body.clientWidth; if(width <= 450){ jQuery('#home_page_banner').html(''); jQuery('#banner_a1').html(''); }else if(width <= 850){ jQuery('#home_page_banner').html(''); jQuery('#banner_a1').html(''); }else{
jQuery('#home_page_banner').html(''); jQuery('#banner_a1').html(''); }
}

window.onload = jsUpdateSize;
window.onresize = jsUpdateSize;`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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