doushi6864 2013-10-14 14:29
浏览 39
已采纳

使用wordpress jQuery load()内部服务器错误

I have this simple wordpress menu that I want to load in div element.

<?php 
$menuParameters = array(
'theme_location' => 'sidebar-menu',
'container'       => false,
'echo'            => false,
'items_wrap'      => '%3$s',
'depth'           => 0,
);
echo strip_tags(wp_nav_menu( $menuParameters ), '<a>' );
 ?>

I'm using modernizr and jQuery load() to load the php file into the div element when a certain media query is detected.

jQuery(document).ready(function($){
    if(Modernizr.mq('only all and (max-width:600px)')) {
        $('#content-wrapper').removeClass('col-4-5');
        $("#trending-Container").load( 'wordpress/wp-content/themes/test_theme/navbar-mobile.php' );
    }
}); 

This works if just use some html or if just echo some string but when I use a wordpress function like above or something like the_title() I get a 500 internal server error.

Any ideas?

  • 写回答

1条回答

  • dsg24156 2013-10-14 15:23
    关注

    I suppose you should no directly access any file in your theme for security reasons. Register your function in funcitons.php in your theme and then call that function_name as refereed over here enter link description here

    jQuery(document).ready(function($) {
    var data = {
        action: 'my_action',
        whatever: ajax_object.we_value      // We pass php values differently!
    };
    // We can also pass the url value separately from ajaxurl for front end AJAX implementations
    jQuery.post(ajax_object.ajax_url, data, function(response) {
        alert('Got this from the server: ' + response);
    });
    });
    

    and register your action in functions.php

    <?php
    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    function my_enqueue($hook) {
    if( 'index.php' != $hook ) return;  // Only applies to dashboard panel
    
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery'));
    
    // in javascript, object properties are accessed as ajax_object.ajax_url,            ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    }
    
    // Same handler function...
    add_action('wp_ajax_my_action', 'my_action_callback');
    function my_action_callback() {
    global $wpdb;
    $whatever = intval( $_POST['whatever'] );
    $whatever += 10;
        echo $whatever;
    die();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能