web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

wordpress实现页面不跳转设置的步骤和代码示例

393 ℃

1、在主题文件夹中创建一个js文件ajax-script.js

jQuery(document).ready(function($){
  $('#myButton').click(function(){
    $.ajax({
      url: ajax_object.ajax_url,
      type: 'post',
      data: {
        action: 'custom_ajax_request'
      },
      success: function(response){
        alert(response.message);
      }
    });
  });
});

2、在主题的functions.php文件中添加以下代码:

add_action('wp_enqueue_scripts', 'enqueue_ajax_script');
function enqueue_ajax_script(){
  wp_enqueue_script('custom-ajax-script', get_template_directory_uri().'/js/ajax-script.js', array('jquery'), '1.0', true);
  wp_localize_script('custom-ajax-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}
 
add_action('wp_ajax_nopriv_custom_ajax_request', 'custom_ajax_request');
add_action('wp_ajax_custom_ajax_request', 'custom_ajax_request');
 
function custom_ajax_request(){
  // 在这里处理Ajax请求
  $response = array('message' => '这是通过Ajax请求返回的数据');
  wp_send_json($response);
}

3、在WordPress页面或文章中添加一个按钮

<button id="myButton">点击我发送ajax请求</button>

php语法对index.php首页进行判断(根据需要显示不同页面)

wordpress获取内容页最新文章代码

wordpress如何从指定数据库读取文章静态生成

wordpress做时间列表,比如2024年1月文章列表

wordpress新建页面中页面属性里没有“模板”选项解决方法

标签: wordpress代码, wordpress教程

上面是“wordpress实现页面不跳转设置的步骤和代码示例”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_15217.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > wordpress
本文共计888个字,预计阅读时长6分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一个免费、无版权的古典音乐下载的网站——Musopen
下一篇: 推荐一款免费在线图片处理工具——佐糖图片平台
x 打工人ai神器