我试图在我的帐户仪表板页面显示相关产品。我遵循这个WooCommerce短代码文档。
我尝试使用以下代码:
add_action( 'woocommerce_account_content', 'output_shortcode_on_child_pages');
function output_shortcode_on_child_pages() {
$post = get_post();
if ( is_page() AND $post->post_parent ) {
echo do_shortcode( '[related_products limit=”3″]' );
}
}
不显示相关产品。但是当我尝试在特定类别中显示产品时,就像下面这样,它工作了:
add_action( 'woocommerce_account_content', 'output_shortcode_on_child_pages');
function output_shortcode_on_child_pages() {
$post = get_post();
if ( is_page() AND $post->post_parent ) {
echo do_shortcode( '[products limit="3" columns="3" orderby="rand" category=”presets-and-actions” ]' );
}
}
请帮忙解决这个问题?
注意相关产品短码需要在单个产品页面上使用,因为相关产品总是链接到一个产品,这就是为什么
[related_products limit="3"]
短码在我的帐户仪表板上不起作用。
现在要瞄准我的帐户仪表板,在函数中替换以下代码行:
$post = get_post();
if ( is_page() AND $post->post_parent ) {
的唯一代码行:
if ( is_account_page() && ! is_wc_endpoint_url() ) {
好多了。参见WooCommerce条件标签