使用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?

duancuan7057
duancuan7057 您需要包含定义wp_nav_menu的WP文件(或您打算使用的任何其他WP函数)。该函数位于wp-includes/nav-menu-template.php。请参阅codex.wordpress.org/Function_Reference/wp_nav_menu。
大约 7 年之前 回复

1个回答

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();
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐