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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的