【发布时间】:2015-02-09 14:55:55
【问题描述】:
我正在使用简单的 ajax 方法在 wordpress 网站中加载内容:我捕获导航链接的点击并将 GET 参数添加到 url:
jQuery('#topnav a').click(function(e){
e.preventDefault();
url = jQuery(this).attr('href');
jQuery.ajax({
type: 'GET',
url: url,
data: 'naked=1', // my parameter
dataType: 'html',
success: function(data){
jQuery('#content').html(data); // load new content
});
}
});
});
在我在 wordpress 模板中检查此参数后,如果此参数存在,我将不包含页眉和页脚并加载裸内容:
<?php
/**
* page.php
* @package WordPress
* @subpackage clean
*/
if (!isset($_GET['naked'])) get_header(); // if parameter exist do not load header ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); // cycle wp start ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; // cycle wp end
if (!isset($_GET['naked'])) get_footer(); // if parameter exist do not load footer ?>
这个方法效果很好,但是如果页面包含contactform7短代码,ajax提交表单不起作用,因为footer.php不包含表单的js人员。
我尝试将 wp_footer() 函数放入 page.php,但 函数没有为表单添加 js 脚本! 如果我也将 wp_head() 放入 page.php - wp_footer() 工作正常。
请有任何想法。
【问题讨论】:
-
这个问题似乎跑题了,因为它是关于 Wordpress 的,属于 wordpress.stackexchange.com
-
@Raptor 然而,它有 3 个赞成票。我完全赞成将问题保持在“主题”上。但这并不比 SO 上其他 63,000 篇 WordPress 帖子中的大多数差。
-
他们应该全部迁移到提到的站点。只是时间问题。
标签: php jquery ajax wordpress contact-form-7