好吧所以我已经为我的Wordpress主题创建了这个AJAX代码,而我最初是为html创建的。如何点击时使用wordpress php编码从wordpress页面获取内容?不关心获得post循环等....去拥有一个静态页面。但我需要它来点击页面的内容。
继承代码
$( document ).ready(function() {
$.ajax({
method: 'GET',
url: "pages/main.html",
success: function(content)
{
$('#contentarea').html (content);
}
});
});
$('.menu_nav') .click (function () {
var href = $(this) .attr('href');
$('#contentarea').hide() .load(href).slideDown( 'very slow' )
return false;
});
menu_nav的HTML结构
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#homenav">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="homenav">
<ul class="nav navbar-nav">
<li><a href="pages/main.html" class="menu_nav demo-4">
<span>
<span>Home</span>
<span>-Home-</span>
<span></span>
</span>
</a></li>
<li> <a href="pages/artist.html" class="menu_nav demo-4">
<span>
<span>Roster</span>
<span>-Roster-</span>
<span></span>
</span>
</a></li>
<li>
<a href="pages/order.html" class="menu_nav demo-4">
<span>
<span>Beats</span>
<span>-Beats-</span>
<span></span>
</span>
</a>
</li> <li>
<a href="pages/music.html" class="menu_nav demo-4">
<span>
<span>Music</span>
<span>-Music-</span>
<span></span>
</span>
</a>
</li>
<li>
<a href="pages/videos.html" class="menu_nav demo-4">
<span>
<span>Videos</span>
<span>-Videos-</span>
<span></span>
</span>
</a> </li>
<li>
<a href="pages/videos.php" class="menu_nav demo-4">
<span>
<span>Store</span>
<span>-Store-</span>
<span></span>
</span>
</a>
</li>
<li>
<a href="pages/services.html" class="menu_nav demo-4">
<span>
<span>Services</span>
<span>-Services-</span>
<span></span>
</span>
</a>
</li>
<li>
<a href="#" class="menu_nav demo-4">
<span>
<span>Resources</span>
<span>-Resources-</span>
<span></span>
</span>
</a> </li>
<li>
<a href="#" class="demo-4 snipcart-checkout">
<span>
<span>YOUR CART: </span>
<span><div class="snipcart-summary"><span class="snipcart-total-price"></span></div></span>
<span></span>
</span>
</a> </li>
</ul>
</div>
<div class="col-sm-1 col-md-1"><div class="navbar-right">
<a href="#" class=""><font color="black" ></span></font></a></span>
</div></div>
</div>
</nav>
答案 0 :(得分:1)
当您可以添加而不是结束网址时,您可以执行以下操作:
<强> HTML 强>
<a href="#" class="menu_nav demo4" data-page="1">Home</a>
<a href="#" class="menu_nav demo4" data-page="2">About</a>
<a href="#" class="menu_nav demo4" data-page="3">Projects</a>
<a href="#" class="menu_nav demo4" data-page="4">Contact</a>
<强> PHP 强>
<?php
/**
* Init js script and its variables
*/
add_action( 'wp_enqueue_scripts', 'init_js_vars' );
function init_js_vars() {
wp_enqueue_script( 'your_script', [YOUR_PATH_TO_JS_FILE], array( 'jquery') );
wp_localize_script( 'your_script', 'theme', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ajax-get-page-content' ),
'page' => 4
) );
}
/**
* Handle ajax request, returns content of specific page
*/
add_action( 'wp_ajax_get_page_content', 'get_page_content' );
add_action( 'wp_ajax_nopriv_get_page_content', 'get_page_content' );
function get_page_content() {
check_ajax_referer( 'ajax-get-page-content', 'nonce' );
$post_id = absint( $_POST['page_id'] );
$post = get_post( $post_id );
$content = apply_filters('the_content', $post->post_content);
wp_send_json_success( array( 'content' => $content ) );
}
?>
<强> JS 强>
$(document).ready(function() {
function load_page( e ) {
e.preventDefault();
var page = $( this ).data( 'page' );
$.ajax({
url: theme.ajaxurl,
method: 'GET',
data: {
action: 'get_page_content',
nonce: theme.nonce,
page_id: page
},
success: function(response) {
$('#contentarea').html(response.content);
}
});
}
$('a.menu_nav.demo4').on( 'click', load_page );
});