乾淨的解決方案是使用WordPress的AJAX功能。
現在的主題通常會分成多個部分以在各個地方重複使用塊。例如,主題twentysixteen在single.php
中使用get_template_part('template-parts/content', 'single');
來包含顯示文件實際內容的模板文件。您可以輕鬆地使用它來獲取您的文章的內容,而無需標題,頁腳等。
首先,設置PHP部分,您可以將其添加到主題的functions.php中,或者直接添加到您的插件中你正在開發什麼。
<?php
// for the admin area
add_action('wp_ajax_my_action', 'my_action_callback');
// for the public area
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
function my_action_callback() {
$postid = intval($_POST['postid']);
$post = get_post($postid);
setup_postdata($post);
get_template_part('template-parts/content', 'single');
wp_die(); // this is required to terminate immediately and return a proper response
}
相應JavaScript部分:
var data = {
'action': 'my_action',
'postid': 1234
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
// on the frontend you have to set it yourself
var ajaxurl = '<?=admin_url('admin-ajax.php')?>';
jQuery.post(ajaxurl, data, function(response) {
// alert('Got this from the server: ' + response);
// response will now contain your post, without header, footer, sidebars etc.
});
my_action
是整個過程的identifyer並且必須在兩個部件之間是一致的。
文檔WordPress的AJAX支持:https://codex.wordpress.org/AJAX_in_Plugins
彈出窗口不夠真實,iframe或新的dom元素?我想如果你只是加載整個頁面來獲取內容很慢,你能不能運行你自己定製的ajax來提取內容? – David
不確定如何描述彈出窗口;這是一個[Magnific Popup](http://dimsemenov.com/plugins/magnific-popup/),內容用ajax加載。整個頁面確實正在加載,這正是問題:如何加載循環? – user3782713