簡體   English   中英

jQuery load()wordpress內部服務器錯誤

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

我有這個簡單的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>' );
 ?>

當檢測到某些媒體查詢時,我正在使用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' );
    }
}); 

如果僅使用一些html或僅回顯一些字符串,則此方法有效,但是當我使用上述的wordpress函數或the_title()之類的東西時,我得到500內部服務器錯誤。

有任何想法嗎?

我想出於安全考慮,您不應該直接訪問主題中的任何文件。 在您的主題中的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);
});
});

並在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