单击链接时,我正在尝试将php文件加载到div #main
中。会加载html内容,但文件中包含的实际php代码不会加载。这是我得到的:
链接触发一切:
<a id="nav" href="http://domain.com/inc/file.php">
jQuery的:
jQuery(document).ready(function($) {
$("#nav").on('click', function (e) {
e.preventDefault();
$("#main").empty();
$.ajax({
url: $("#nav").attr('href')
}).done(function(data) {
$('#main').html(data); // display data
});
});
});
来自
file.php
的代码(主要是WordPress功能):<div id="hostels" class="section cf">
<?php get_a_post(31); ?>
<article id="swiss" <?php post_class('swiss bg col span-1-3'); ?> role="article">
<h3>
<a href="<?php the_permalink(); ?>">
<span class="logo-pl-small"><?php include(TEMPLATEPATH . '/library/svg/logo.svg'); ?></span>
<span class="">Swiss Cottage – London UK</span>
</a>
</h3>
<?php if ( has_post_thumbnail()) { the_post_thumbnail(); } ?>
<div class="entry-content">
<?php the_excerpt(); ?>
<?php edit_post_link( __( 'Edit', 'bonestheme' ) ); ?>
<?php if ( get_post_meta($post->ID, 'vidlink', true) ) {
echo apply_filters('the_content', get_post_meta($post->ID, 'vidlink', true));
} ?>
</div>
</article>
</div>
输出:
<div id="main">
<div id="hostels" class="section cf">
</div>
</div>
该文件开始加载,但是一旦到达
<?php get_a_post(31); ?>
,该文件就不会继续加载。我可能是错的,但我认为通过使用
$.ajax
php代码将在服务器上执行,并且其输出的代码将加载到我的#main
div中。 最佳答案
因此,我对此有所帮助。
我发现this tutorial for Ajax Powered Loops with jQuery and WordPress从中我发现(正如我猜想的那样)我创建的文件需要了解WordPress功能。为此,我需要包括:
require_once('../../../../wp-load.php');
就我而言,这是来自
file.php
的相关路径。仍然存在一些问题,例如从插件生成的短代码无法完全正常工作,但这超出了我最初提出的问题的范围。
但是,在上面的教程中,有很多关于ajax和Wordpress的有用信息,如果有人遇到类似的问题,我建议您阅读一下。