Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在图像上执行短代码单击?_Javascript_Wordpress_Shortcode - Fatal编程技术网

Javascript 在图像上执行短代码单击?

Javascript 在图像上执行短代码单击?,javascript,wordpress,shortcode,Javascript,Wordpress,Shortcode,不确定在这里张贴或站长,我道歉,如果我错了 有人知道在图像点击上执行短代码的方法吗 我找不到任何关于如何做到这一点的信息 编辑:我的道歉 Yes Shortcode表示wordpress Shortcode通过图像元素的id获取图像元素,并在单击事件时附加处理程序,如 html <img id="myImg" src="" /> JS <script> document.getElementById('myImg').onclick = function (){

不确定在这里张贴或站长,我道歉,如果我错了

有人知道在图像点击上执行短代码的方法吗

我找不到任何关于如何做到这一点的信息

编辑:我的道歉


Yes Shortcode表示wordpress Shortcode

通过图像元素的id获取图像元素,并在单击事件时附加处理程序,如

html

<img id="myImg" src="" />

JS

<script>
 document.getElementById('myImg').onclick = function (){
     //execute your short code here.
  }
</script>

document.getElementById('myImg')。onclick=function(){
//在这里执行您的短代码。
}
==模板页====


函数executeShortCode(){
var url=“/yourAjaxPage.php”;
post(url、函数(数据){
控制台日志(数据);
});
}
==Ajax页面====

//yourAjaxPage.php
<?php echo do_shortcode('[yourshortcode]'); ?>
//yourAjaxPage.php

因为我在网上找不到关于这个问题的答案,所以我想我应该发布我的解决方案。使用wp_localize_脚本加载我的短代码,下面是我的示例:

// PHP (in functions.php)
<?php

add_action( 'wp_enqueue_scripts', 'custom_add_scripts' );
function custom_add_scripts() {
wp_register_script( 'my_new_js_callname', plugins_url( '/js/filename.js' , __FILE__ ), array(), '1.0.0', true );
wp_enqueue_script( 'my_new_js_callname' );
}


add_action ('template_hook', 'custom_function_name');
function custom_function_name() {

// Because I used if/else statements in my JS, I loaded individual IDs into the array.

$examplea = do_shortcode('[add_to_cart id="118"]');
$exampleb = do_shortcode('[add_to_cart id="119"]');
$examplec = do_shortcode('[add_to_cart id="120"]');

$array = array( 
                'examplea' => $examplea,
                'exampleb' => $exampleb,
                'examplec' => $examplec,
        );


wp_localize_script( 'my_new_js_callname', 'callword', $array );
//PHP(在functions.PHP中)

你试过了吗?这是你要找的wordpress短码吗?如果是这样,你应该用
wordpress
shortcode
来标记你的问题,正如你所知道的,wordpress的shortcode是
php
函数,在页面离开服务器之前被替换掉,所以尝试用
javascript
执行一个似乎是不可能的……我不会说这是不可能的。我想说,您可以通过AJAX请求以某种方式实现这一点,不幸的是,我不知道AJAX。短代码到底应该做什么?我不认为OP在字面意义上是“短代码”的意思。。。这似乎是一种有趣的想法。必须进行测试在这种情况下,短代码已经显示在脚本中…因此,如果javascript调用的是不弹出的或其他东西你好Arkaaprava,我添加了
函数executeShortCode(){
到customfunctions.php以及
标题片段
,并将图像添加到
并放置在页面上,但当我单击图像时,什么都没有happens@Tim尝试使用ajax执行短代码。我已经更新了答案。
// PHP (in functions.php)
<?php

add_action( 'wp_enqueue_scripts', 'custom_add_scripts' );
function custom_add_scripts() {
wp_register_script( 'my_new_js_callname', plugins_url( '/js/filename.js' , __FILE__ ), array(), '1.0.0', true );
wp_enqueue_script( 'my_new_js_callname' );
}


add_action ('template_hook', 'custom_function_name');
function custom_function_name() {

// Because I used if/else statements in my JS, I loaded individual IDs into the array.

$examplea = do_shortcode('[add_to_cart id="118"]');
$exampleb = do_shortcode('[add_to_cart id="119"]');
$examplec = do_shortcode('[add_to_cart id="120"]');

$array = array( 
                'examplea' => $examplea,
                'exampleb' => $exampleb,
                'examplec' => $examplec,
        );


wp_localize_script( 'my_new_js_callname', 'callword', $array );
// JS (in filename.js)
<script>
document.getElementById('myImg').onclick = function (){
    $("#show").html( callword.examplea );
  }
</script>
<div id="show"></div>