【发布时间】:2021-10-11 13:48:56
【问题描述】:
我需要在首页输出某个产品页面。 add_rewrite_rule 出于任何原因不适用于主页(
数据库中的首页实际上并没有重写规则,WordPress似乎使用了一些其他功能
查询首页):
//works fine
add_rewrite_rule( 'certainproductpage/?$',
'index.php?post_type=product&name=certainproduct',
'top'
);
//does not work
add_rewrite_rule( '', //tried everything like "/", "/?$" etc
'index.php?post_type=product&name=certainproduct',
'top'
);
在花太多时间查看 wp / wc 核心代码和 stackoverflow 之后,我遇到了另一种选择。我可以
只需在页面内容中添加一个简码我需要同时成为主页和产品页面
时间:[product_page id=815]。确实它很好用,但只有在管理编辑器中添加短代码或
存储在数据库中(post_content)。如果我尝试在页面模板上手动调用简码(
page-certainproductpage.php) 然后它输出产品页面而不需要一些必要的东西(PayPal、PhotoSwipe 和
画廊 js)。奇怪的是,如果我将简码保留在内容中(通过 Gutenberg / 代码编辑器)但不要
致电the_content 并仅回显短代码,然后一切正常:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
get_header( 'shop' );
//works fine only if the same shortcode is within the certainproductpage's content
echo do_shortcode("[product_page id='815']");
//the_content();
get_footer( 'shop' );
另外,当我尝试通过the_content 过滤钩子添加短代码时,在核心的 do_shortcode 函数应用之前
default-filters.php ($priority
NOTICE: PHP message: PHP Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/html/wp-includes/functions.php on line 5106
很遗憾,没有记录堆栈跟踪。第 5107 行周围的函数是 wp_ob_end_flush_all,它在 shutdown 上从 default-filters.php 调用
echo do_shortcode(apply_filters('the_content', "[product_page id=815]")); 也没有帮助(与不完整的输出相同
echo do_shortcode("[product_page id=815]");)
也很奇怪: 当我比较编辑器中的内容字符串和以编程方式添加的短代码字符串时,它是 相等!:
add_filter( "the_content", function ( $content ){
$wtf = "<!-- wp:paragraph -->
<p>[product_page id=815]</p>
<!-- /wp:paragraph -->";
$result = $wtf === $content;
?><pre><?php var_dump($result)?></pre><?php
return $content;
}, 1 );
但是,如果我将 return $content 替换为 return $wtf - 我会得到超出最大执行时间的错误。
那么我如何才能在主页(“/”)上正确输出产品页面,或者如何使用简码获得相同的结果
当在 the_content 过滤器中应用时,就像在 (Gutenberg) 编辑器中添加短代码时一样?
更新
使用仅输出标题标签的简单自定义短代码对其进行了测试,并且可以与the_content 过滤器一起正常工作。还在一个仅安装了 WooCommerce 和 PayPal 的绝对干净的网站上进行了尝试——结果相同。似乎是 WooCommerce 方面的一个错误。本周某天将通过 xDebug 运行它。
【问题讨论】:
标签: wordpress woocommerce