简体   繁体   English

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

[英]jQuery load() internal server error with wordpress

I have this simple wordpress menu that I want to load in div element. 我有这个简单的wordpress菜单,我想在div元素中加载。

<?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. 当检测到某些媒体查询时,我正在使用modernizr和jQuery load()将php文件加载到div元素中。

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. 如果仅使用一些html或仅回显一些字符串,则此方法有效,但是当我使用上述的wordpress函数或the_title()之类的东西时,我得到500内部服务器错误。

Any ideas? 有任何想法吗?

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 在您的主题中的funcitons.php中注册您的函数,然后按此处引用的方式调用那个function_name在此处输入链接描述

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 并在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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM