【问题标题】:wp_footer() doesn't work without wp_head()?wp_footer() 没有 wp_head() 就不能工作?
【发布时间】: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


【解决方案1】:

如果您查看line 200 of wp-includes/default-filters.php,您会注意到脚本与wp_head 一起排队。

这就是您的脚本无法正常工作的原因。 wp_head() 是 Contact Form 7 正常运行的关键功能。您仍然需要包含wp_head(),但不需要包含wp_header() 以实现这一点。例如,以下内容应保持“裸露”,但仍允许加载脚本:

if ( !isset($_GET['naked']) ) {
    get_header(); // if parameter exist do not load header
} else {
    wp_head(); // still include wp_head(), but without the rest of the "header"
}

确保wp_head() 仍在您的文档&lt;head&gt;&lt;/head&gt; 部分中运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    相关资源
    最近更新 更多