【问题标题】:WPBakery Page Builder not rendering shortcodes on WooCommerce Product Category pagesWPBakery Page Builder 不在 WooCommerce 产品类别页面上呈现简码
【发布时间】:2019-04-10 11:55:20
【问题描述】:

我正在开发一个相当复杂的主题,其中我为“页眉”和“页脚”创建了自定义帖子类型,为此我启用了 WPBakery Page Builder(目前在 Wordpress 4.9.8 中使用 v5.5.5)。

在 header.php 中,将 Header 帖子拉入并呈现在

标记内,然后在 footer.php 中将 Footer 帖子拉入并呈现在
标记内。

这一切都完美无缺,除了在 WooCommerce 中转到产品类别页面时,页眉和页脚仅显示为页面构建器的原始短代码并且不解析它们。 WooCommerce 的其他一切都很好;商店页面、单个产品页面、购物车、结帐,一切都很好。出于某种原因,这只是类别。

这里是 header.php 中的代码:

<?php

$this_page_id = get_queried_object_id();

$gd_page_header = get_post_meta( $this_page_id, 'gd_page_header' );
if(empty($gd_page_header) || count($gd_page_header) == 0 || $gd_page_header[0] == 'default'){
    $header_id = intval(get_option('default_header'));
}else{
    $header_id = $gd_page_header[0];
}
if(!empty($header_id) && $header_id != '' && $header_id != 'none'){
    $post   = get_post( $header_id );
    $transparent_header = get_post_meta( $post->ID, 'transparent_header', true );
    $sticky_header = get_post_meta( $post->ID, 'sticky_header', true );
    $sticky_desktop_only = get_post_meta( $post->ID, 'sticky_desktop_only', true );
    if($sticky_header == '1'){
        $header_class = 'sticky';
        if($sticky_desktop_only == '1'){
            $header_class .= ' dt_only';
        }
    }elseif($transparent_header == '1'){
        $header_class = 'transparent';
    }else{
        $header_class = '';
    }
    $content = $post->post_content;
    $content_css = visual_composer()->parseShortcodesCustomCss( $content );
    if ( ! empty( $content_css ) ) { ?>
         <style type="text/css" data-type="vc_shortcodes-custom-css">
                     <?php echo strip_tags( $content_css ); ?>
         </style>
    <?php } ?>

    <header class="<?php echo($header_class); ?>">
        <div id="container" class="container clearfix">
            <?php echo apply_filters( 'the_content', $content ); ?>
        </div>
    </header>
<?php } ?>

然后是 WooCommerce 将其内容提取到的 page.php 中的代码:

<?php get_header(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <main id="post-<?php the_ID(); ?>">
            <div id="container" class="container clearfix">
                <?php the_content(); ?>
            </div>
        </main>
    <?php endwhile; endif; ?>
<?php get_footer(); ?>

这里是我的测试网站主页:http://sandbox.graphicdetail.co.nz/

如您所见,一切都很好地显示在那里。 一切也都很好地显示在商店页面上:http://sandbox.graphicdetail.co.nz/shop/

但是当我们进入一个类别页面时,一切都崩溃了:http://sandbox.graphicdetail.co.nz/product-category/toys/

我知道 WPBakery Page Builder 在此页面上仍然处于活动状态,因为您可以在 header.php 代码中看到它所在的位置:

$content_css = visual_composer()->parseShortcodesCustomCss( $content );
if ( ! empty( $content_css ) ) { ?>
     <style type="text/css" data-type="vc_shortcodes-custom-css">
                 <?php echo strip_tags( $content_css ); ?>
     </style>
<?php } ?>

...这似乎工作正常,因为当我使用检查器查看页面时,我可以看到 css 填充正确。

希望这里有人可能知道我在哪里犯了可怕的错误?

【问题讨论】:

  • 您的类别页面上似乎缺少可视化作曲家 js 文件 js_composer_front.min.js
  • 知道为什么它会选择不在类别页面上排队,以及如何让它加载而无需在所有其他页面上重复加载?在这样做之前,还需要检查 WPBakery Page Builder 是否已实际安装和激活。
  • 好的,我已经设法强制将 js_composer_front.min.js 加载到类别页面上,但页面的外观仍然没有变化。

标签: php wordpress woocommerce themes


【解决方案1】:

为了其他遇到此问题的人的利益,我终于解决了这个难题,这一切都归功于这篇文章(特别是“模板文件”部分):

http://stephanieleary.com/2010/02/using-shortcodes-everywhere/

所以我的 header.php 文件中有这段代码:

<?php $content = $post->post_content; ?>

<header class="<?php echo($header_class); ?>">
    <div id="container" class="container clearfix">
        <?php echo apply_filters( 'the_content', $content ); ?>
    </div>
</header>

我可以说内容都被正确地拉入并且 CSS 被正确解析等等,但这只是 WPBakery Page Builder 的短代码由于某种原因没有被解析。

那么,在阅读了 Stephanie Leary 的文章后,我决定试试这个:

<?php
    $content = $post->post_content;
    $content = apply_filters( 'the_content', $content );
?>

<header class="<?php echo($header_class); ?>">
    <div id="container" class="container clearfix">
        <?php echo do_shortcode($content); ?>
    </div>
</header>

因此,我不仅尝试回显 apply_filters,还使用 ​​apply_filters 填充了 $content 变量,然后将 $content 解析为简码,然后哇!成功了!

【讨论】:

    【解决方案2】:

    在回显内容之前使用它:

    WPBMap::addAllMappedShortcodes();
    echo apply_filters('the_content', the_content());
    

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多