在 Woocommerce 产品类别档案中加载特定元素
Load specific element on Woocommerce product category archives
他们是通过 functions.php 或其他 php 方法不在 woocommerce 的商店/主页上加载特定 div 元素的方法吗?
<div id="nm-shop" class="nm-shop nm-shop-sidebar-header images-lazyload images-show" style="min-height: 288px;">
...
</div>
此代码包括一个过滤器和我的商店页面的产品档案,但我只想在类别页面上显示它,它已经可以工作,但不能在主页上显示。我怎样才能停止在商店/主页上加载这个 div 元素?我知道可以通过 css display:none;
但这不是最干净的解决方案。
我在存档中找到了 div class-product.php 但我不确定它是否被定义为也可以在商店页面上加载它。
<div id="nm-shop" class="nm-shop <?php echo esc_attr( $shop_class ); ?>">
<?php
/**
* Hook: woocommerce_before_main_content.
*
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* NM: Removed - @hooked woocommerce_breadcrumb - 20
* @hooked WC_Structured_Data::generate_website_data() - 30
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
// Shop header
if ( $nm_theme_options['shop_header'] ) {
wc_get_template_part( 'content', 'product_nm_header' );
}
?>
<?php nm_print_shop_notices(); // Note: Don't remove (WooCommerce will output multiple messages otherwise) ?>
<div id="nm-shop-products" class="nm-shop-products">
<div class="nm-row">
<?php
if ( $show_filters_sidebar ) {
/**
* woocommerce_sidebar hook.
*
* @hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommerce_sidebar' );
}
?>
<div class="nm-shop-products-col <?php echo esc_attr( $shop_column_size ); ?>">
<div id="nm-shop-products-overlay" class="nm-loader"></div>
<div id="nm-shop-browse-wrap" class="nm-shop-description-<?php echo esc_attr( $nm_theme_options['shop_description_layout'] ); ?>">
<?php
// Results bar/button
wc_get_template_part( 'content', 'product_nm_results_bar' );
?>
<?php
// Taxonomy description
if ( $show_taxonomy_description ) {
if ( $is_product_taxonomy ) {
/**
* Hook: woocommerce_archive_description.
*
* @hooked woocommerce_taxonomy_archive_description - 10
* @hooked woocommerce_product_archive_description - 10
*/
do_action( 'woocommerce_archive_description' );
} else if ( strlen( $nm_theme_options['shop_default_description'] ) > 0 && ! isset( $_REQUEST['s'] ) ) { // Don't display on search
// Default description
nm_shop_description( $nm_theme_options['shop_default_description'] );
}
}
?>
<?php
if ( woocommerce_product_loop() ) {
/**
* Hook: woocommerce_before_shop_loop.
*
* @hooked wc_print_notices - 10
* NM: Removed - @hooked woocommerce_result_count - 20
* NM: Removed - @hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
// Set column sizes
global $woocommerce_loop;
$woocommerce_loop['columns'] = $nm_theme_options['shop_columns'];
$woocommerce_loop['columns_small'] = '2';
$woocommerce_loop['columns_medium'] = '3';
woocommerce_product_loop_start();
$nm_globals['is_categories_shortcode'] = false;
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
// Note: Don't place in another template (image lazy-loading is only used in the Shop and WooCommerce shortcodes can use the other product templates)
$nm_globals['shop_image_lazy_loading'] = ( $nm_theme_options['product_image_lazy_loading'] ) ? true : false;
/**
* Hook: woocommerce_shop_loop.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
woocommerce_product_loop_end();
/**
* Hook: woocommerce_after_shop_loop.
*
* @hooked woocommerce_pagination - 10
*/
do_action( 'woocommerce_after_shop_loop' );
} else {
/**
* Hook: woocommerce_no_products_found.
*
* @hooked wc_no_products_found - 10
*/
do_action( 'woocommerce_no_products_found' );
}
?>
</div>
</div>
</div>
<?php
/**
* Hook: woocommerce_after_main_content.
*
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
</div>
<?php
// Sidebar/filters popup
if ( $show_filters_popup ) {
wc_get_template_part( 'content', 'product_nm_filters_popup' );
}
?>
</div>
<?php
/**
* Hook: nm_after_shop.
*/
do_action( 'nm_after_shop' );
get_footer( 'shop' );
?>
这很简单。只需创建一个单独的文件 .php
并在下方添加您的赞
<div id="nm-shop" class="nm-shop nm-shop-sidebar-header images-lazyload images-show" style="min-height: 288px;">
...
</div>
然后您可以将其包含在页面中您想要的位置,只需执行以下操作:
include 'name_of_your_file.php';
您可以使用 woocommerce conditional tag is_product_category()
仅定位产品类别档案页面,这样:
<?php if( is_product_category() ) : ?>
<div id="nm-shop" class="nm-shop nm-shop-sidebar-header images-lazyload images-show" style="min-height: 288px;">
...
</div>
<?php endif; ?>
它应该有效
他们是通过 functions.php 或其他 php 方法不在 woocommerce 的商店/主页上加载特定 div 元素的方法吗?
<div id="nm-shop" class="nm-shop nm-shop-sidebar-header images-lazyload images-show" style="min-height: 288px;">
...
</div>
此代码包括一个过滤器和我的商店页面的产品档案,但我只想在类别页面上显示它,它已经可以工作,但不能在主页上显示。我怎样才能停止在商店/主页上加载这个 div 元素?我知道可以通过 css display:none;
但这不是最干净的解决方案。
我在存档中找到了 div class-product.php 但我不确定它是否被定义为也可以在商店页面上加载它。
<div id="nm-shop" class="nm-shop <?php echo esc_attr( $shop_class ); ?>">
<?php
/**
* Hook: woocommerce_before_main_content.
*
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* NM: Removed - @hooked woocommerce_breadcrumb - 20
* @hooked WC_Structured_Data::generate_website_data() - 30
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
// Shop header
if ( $nm_theme_options['shop_header'] ) {
wc_get_template_part( 'content', 'product_nm_header' );
}
?>
<?php nm_print_shop_notices(); // Note: Don't remove (WooCommerce will output multiple messages otherwise) ?>
<div id="nm-shop-products" class="nm-shop-products">
<div class="nm-row">
<?php
if ( $show_filters_sidebar ) {
/**
* woocommerce_sidebar hook.
*
* @hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommerce_sidebar' );
}
?>
<div class="nm-shop-products-col <?php echo esc_attr( $shop_column_size ); ?>">
<div id="nm-shop-products-overlay" class="nm-loader"></div>
<div id="nm-shop-browse-wrap" class="nm-shop-description-<?php echo esc_attr( $nm_theme_options['shop_description_layout'] ); ?>">
<?php
// Results bar/button
wc_get_template_part( 'content', 'product_nm_results_bar' );
?>
<?php
// Taxonomy description
if ( $show_taxonomy_description ) {
if ( $is_product_taxonomy ) {
/**
* Hook: woocommerce_archive_description.
*
* @hooked woocommerce_taxonomy_archive_description - 10
* @hooked woocommerce_product_archive_description - 10
*/
do_action( 'woocommerce_archive_description' );
} else if ( strlen( $nm_theme_options['shop_default_description'] ) > 0 && ! isset( $_REQUEST['s'] ) ) { // Don't display on search
// Default description
nm_shop_description( $nm_theme_options['shop_default_description'] );
}
}
?>
<?php
if ( woocommerce_product_loop() ) {
/**
* Hook: woocommerce_before_shop_loop.
*
* @hooked wc_print_notices - 10
* NM: Removed - @hooked woocommerce_result_count - 20
* NM: Removed - @hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
// Set column sizes
global $woocommerce_loop;
$woocommerce_loop['columns'] = $nm_theme_options['shop_columns'];
$woocommerce_loop['columns_small'] = '2';
$woocommerce_loop['columns_medium'] = '3';
woocommerce_product_loop_start();
$nm_globals['is_categories_shortcode'] = false;
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
// Note: Don't place in another template (image lazy-loading is only used in the Shop and WooCommerce shortcodes can use the other product templates)
$nm_globals['shop_image_lazy_loading'] = ( $nm_theme_options['product_image_lazy_loading'] ) ? true : false;
/**
* Hook: woocommerce_shop_loop.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
woocommerce_product_loop_end();
/**
* Hook: woocommerce_after_shop_loop.
*
* @hooked woocommerce_pagination - 10
*/
do_action( 'woocommerce_after_shop_loop' );
} else {
/**
* Hook: woocommerce_no_products_found.
*
* @hooked wc_no_products_found - 10
*/
do_action( 'woocommerce_no_products_found' );
}
?>
</div>
</div>
</div>
<?php
/**
* Hook: woocommerce_after_main_content.
*
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
</div>
<?php
// Sidebar/filters popup
if ( $show_filters_popup ) {
wc_get_template_part( 'content', 'product_nm_filters_popup' );
}
?>
</div>
<?php
/**
* Hook: nm_after_shop.
*/
do_action( 'nm_after_shop' );
get_footer( 'shop' );
?>
这很简单。只需创建一个单独的文件 .php
并在下方添加您的赞
<div id="nm-shop" class="nm-shop nm-shop-sidebar-header images-lazyload images-show" style="min-height: 288px;">
...
</div>
然后您可以将其包含在页面中您想要的位置,只需执行以下操作:
include 'name_of_your_file.php';
您可以使用 woocommerce conditional tag is_product_category()
仅定位产品类别档案页面,这样:
<?php if( is_product_category() ) : ?>
<div id="nm-shop" class="nm-shop nm-shop-sidebar-header images-lazyload images-show" style="min-height: 288px;">
...
</div>
<?php endif; ?>
它应该有效