在 if 语句中一起使用多个 WooCommerce 条件标签
Using multiple WooCommerce conditional tags together in an if statement
我在使用 is_cart()
、is_checkout()
、is_account_page()
函数时遇到问题。
我想做什么:
- 如果是购物车页面,则回显此简码。
- 如果不是购物车页面,则什么都不做。
这需要发生在页面顶部和底部,都在页面容器之外。
当我查看购物车、结帐、帐户页面时,一切正常。但是只要我查看任何其他不是 cart/checkout/account 页面的页面,简码仍然显示!
该网站在 WooCommerce 中使用 Divi 主题(由 ElegantThemes 设计)。
这是我正在处理的片段:
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="218"][/et_pb_section]'); ?>
<?php endif; ?>
<?php endif; ?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]'); ?>
<?php endif; ?>
这是完整的 page.php 代码:
<?php
get_header();
$is_page_builder_used = et_pb_is_pagebuilder_used( get_the_ID() );
?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="218"][/et_pb_section]'); ?>
<?php endif; ?>
<div id="main-content">
<?php if ( ! $is_page_builder_used ) : ?>
<div class="container">
<div id="content-area" class="clearfix">
<div id="left-area">
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( ! $is_page_builder_used ) : ?>
<h1 class="entry-title main_title"><?php the_title(); ?></h1>
<?php
$thumb = '';
$width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );
$height = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
$classtext = 'et_featured_image';
$titletext = get_the_title();
$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
$thumb = $thumbnail["thumb"];
if ( 'on' === et_get_option( 'divi_page_thumbnails', 'false' ) && '' !== $thumb )
print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height );
?>
<?php endif; ?>
<div class="entry-content">
<?php
the_content();
if ( ! $is_page_builder_used )
wp_link_pages( array( 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'Divi' ), 'after' => '</div>' ) );
?>
</div> <!-- .entry-content -->
<?php
if ( ! $is_page_builder_used && comments_open() && 'on' === et_get_option( 'divi_show_pagescomments', 'false' ) ) comments_template( '', true );
?>
</article> <!-- .et_pb_post -->
<?php endwhile; ?>
<?php if ( ! $is_page_builder_used ) : ?>
</div> <!-- #left-area -->
<?php get_sidebar(); ?>
</div> <!-- #content-area -->
</div> <!-- .container -->
<?php endif; ?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]'); ?>
<?php endif; ?>
</div> <!-- #main-content -->
<?php get_footer(); ?>
如果我理解得很好,您只想在购物车、结帐和帐户 WooCommerce 页面中输出此简码内容
那你最好试试这个更短的方法:
<?php
if ( class_exists( 'WooCommerce' ) && ( is_cart() || is_account_page() || is_checkout() ) ) :
echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]');
endif;
?>
使用 class_exists( 'WooCommerce' )
将确保加载 woocommerce 插件并且条件标签将正常工作…
我在使用 is_cart()
、is_checkout()
、is_account_page()
函数时遇到问题。
我想做什么:
- 如果是购物车页面,则回显此简码。
- 如果不是购物车页面,则什么都不做。
这需要发生在页面顶部和底部,都在页面容器之外。
当我查看购物车、结帐、帐户页面时,一切正常。但是只要我查看任何其他不是 cart/checkout/account 页面的页面,简码仍然显示!
该网站在 WooCommerce 中使用 Divi 主题(由 ElegantThemes 设计)。 这是我正在处理的片段:
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="218"][/et_pb_section]'); ?>
<?php endif; ?>
<?php endif; ?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]'); ?>
<?php endif; ?>
这是完整的 page.php 代码:
<?php
get_header();
$is_page_builder_used = et_pb_is_pagebuilder_used( get_the_ID() );
?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="218"][/et_pb_section]'); ?>
<?php endif; ?>
<div id="main-content">
<?php if ( ! $is_page_builder_used ) : ?>
<div class="container">
<div id="content-area" class="clearfix">
<div id="left-area">
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( ! $is_page_builder_used ) : ?>
<h1 class="entry-title main_title"><?php the_title(); ?></h1>
<?php
$thumb = '';
$width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );
$height = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
$classtext = 'et_featured_image';
$titletext = get_the_title();
$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
$thumb = $thumbnail["thumb"];
if ( 'on' === et_get_option( 'divi_page_thumbnails', 'false' ) && '' !== $thumb )
print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height );
?>
<?php endif; ?>
<div class="entry-content">
<?php
the_content();
if ( ! $is_page_builder_used )
wp_link_pages( array( 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'Divi' ), 'after' => '</div>' ) );
?>
</div> <!-- .entry-content -->
<?php
if ( ! $is_page_builder_used && comments_open() && 'on' === et_get_option( 'divi_show_pagescomments', 'false' ) ) comments_template( '', true );
?>
</article> <!-- .et_pb_post -->
<?php endwhile; ?>
<?php if ( ! $is_page_builder_used ) : ?>
</div> <!-- #left-area -->
<?php get_sidebar(); ?>
</div> <!-- #content-area -->
</div> <!-- .container -->
<?php endif; ?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]'); ?>
<?php endif; ?>
</div> <!-- #main-content -->
<?php get_footer(); ?>
如果我理解得很好,您只想在购物车、结帐和帐户 WooCommerce 页面中输出此简码内容
那你最好试试这个更短的方法:
<?php
if ( class_exists( 'WooCommerce' ) && ( is_cart() || is_account_page() || is_checkout() ) ) :
echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]');
endif;
?>
使用 class_exists( 'WooCommerce' )
将确保加载 woocommerce 插件并且条件标签将正常工作…