Wordpress 根据类别在Woocommerce中的单个产品页面上显示内容
我已经很接近了,但我不能得到最后一点 我将把作者的名字添加到我们在吴宇森出售的书中。样式需要与书名不同,因此我使用一个自定义元字段将其放在标题下,并按照我想要的方式设置样式。我有一切工作,我想要它,现在我只需要它显示在一个特定的类别,而不是所有的产品 在我看来,有两种方法可以做到这一点。 1-仅显示特定类别的产品 2-仅当有内容时才显示 我目前正在尝试只显示特定类别的内容,但在编写本文时,它似乎是一个更优雅的解决方案,只显示存在内容的内容 这是我的Wordpress 根据类别在Woocommerce中的单个产品页面上显示内容,wordpress,woocommerce,Wordpress,Woocommerce,我已经很接近了,但我不能得到最后一点 我将把作者的名字添加到我们在吴宇森出售的书中。样式需要与书名不同,因此我使用一个自定义元字段将其放在标题下,并按照我想要的方式设置样式。我有一切工作,我想要它,现在我只需要它显示在一个特定的类别,而不是所有的产品 在我看来,有两种方法可以做到这一点。 1-仅显示特定类别的产品 2-仅当有内容时才显示 我目前正在尝试只显示特定类别的内容,但在编写本文时,它似乎是一个更优雅的解决方案,只显示存在内容的内容 这是我的 function cn_add_add_sub
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
if ( is_product() && has_term( 'books-media', 'product_cat' ) ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
函数cn\u add\u add\u subhead\u to\u title(){
全球$woocmerce,$post;
if(is_product()&&has_术语('books media','product_cat')){
?>
通过
如果您只是测试meta是否存在,会怎么样?如果您不将meta保存在其他类别中,它将永远不会显示:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
函数cn\u add\u add\u subhead\u to\u title(){
全球$员额;
如果($meta=get\u post\u meta($post->ID,'.\u text\u field',true)){
?>
如果您只是测试meta是否存在,会怎么样?如果您不将meta保存在其他类别中,它将永远不会显示:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
函数cn\u add\u add\u subhead\u to\u title(){
全球$员额;
如果($meta=get\u post\u meta($post->ID,'.\u text\u field',true)){
?>
Helga-谢谢你的关注和帮助。我有一点时间,所以我决定尝试另一种方法,只在有内容的情况下显示元数据,而不是基于类别。无论如何,这可能是一种更灵活的解决方案,让该领域向他们想要的任何类型的副标题开放,而不是仅限于作者
这是有效的代码:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
/*在单个产品的标题下添加作者*/
功能cn添加添加子标题添加到标题(){
全球$woocmerce,$post;
$meta=get_post_meta($post->ID,''u text_field',true);
如果($meta!=''){
?>
通过
Helga-谢谢你的关注和帮助。我有一点时间,所以我决定尝试另一种方法,只在有内容的情况下显示元数据,而不是基于类别。无论如何,这可能是一种更灵活的解决方案,让该领域向他们想要的任何类型的副标题开放,而不是仅限于作者
这是有效的代码:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
/*在单个产品的标题下添加作者*/
功能cn添加添加子标题添加到标题(){
全球$woocmerce,$post;
$meta=get_post_meta($post->ID,''u text_field',true);
如果($meta!=''){
?>
通过
以下是上下文Helga中的答案-此代码段将向woocommerce产品编辑仪表板添加一个自定义元字段,保存内容并将其显示在单一产品视图的产品标题下
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
/*将产品子标题自定义字段添加到woocommerce产品仪表板*/
添加操作(“woocommerce\u product\u options\u general\u product\u data”、“woo\u add\u custom\u general\u fields”);
函数woo_添加_自定义_常规_字段(){
全球$woocmerce,$post;
回声';
woocommerce_wp_text_input(
数组(
'id'=>'\u text\u field',
'label'=>\('MY-label','woocommerce'),
“占位符”=>“作者”,
'desc_tip'=>'true',
“description'=>\(“此文本将显示在产品标题下方。”,“woocommerce”)
)
);
回声';
}
/*保存woo自定义字段*/
添加操作('woo商业\流程\产品\元','woo \添加\自定义\常规\字段\保存');
函数woo\u添加\自定义\常规\字段\保存($post\u id){
$woocommerce_text_field=$_POST[''文本_field'];
如果(!空($woocmerce\u text\u field))
更新发布元数据($post\u id,''u text\u field',esc\u attr($woocommerce\u text\u field));
}
/*在单个产品的标题下方添加作者*/
功能cn添加子标题至标题(){
全球$woocmerce,$post;
$meta=get_post_meta($post->ID,''u text_field',true);
如果($meta!=''){
?>
通过
以下是上下文Helga中的答案-此代码段将向woocommerce产品编辑仪表板添加一个自定义元字段,保存内容并将其显示在单一产品视图的产品标题下
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
/*将产品子标题自定义字段添加到woocommerce产品仪表板*/
添加操作(“woocommerce\u product\u options\u general\u product\u data”、“woo\u add\u custom\u general\u fields”);
函数woo_添加_自定义_常规_字段(){
全球$woocmerce,$post;
回声';
woocommerce_wp_text_input(
数组(
'id'=>'\u text\u field',
'label'=>\('MY-label','woocommerce'),
“占位符”=>“作者”,
'desc_tip'=>'true',
“description'=>\(“此文本将显示在产品标题下方。”,“woocommerce”)
)
);
回声';
}
/*保存woo自定义字段*/
添加操作('woo商业\流程\产品\元','woo \添加\自定义\常规\字段\保存');
函数woo\u添加\自定义\常规\字段\保存($post\u id){
$woocommerce_text_field=$_POST[''文本_field'];
如果(!空($woocmerce\u text\u field))
更新发布元数据($post\u id,''u text\u field',esc\u attr($woocommerce\u text\u field));
}
/*在单个产品的标题下方添加作者*/
功能cn添加子标题至标题(){
全球$woocmerce,$post;
$meta=get_post_meta($post->ID,''u text_field',true);
如果($meta!=''){
?>
通过
您是否通过add\u action()
将cn\u add\u subhead\u添加到了一个动作挂钩中?
还是您直接从模板调用它?它添加到了挂钩中—“add\u action”(“woocommerce\u single\u product\u summary”,“cn\u add\u subhead\u\u\u to\u title”,6)“您确定图书媒体
是您类别中的slug吗?您确定\u text\u field
是您自定义元的名称吗?您确定保存正确吗?否则,代码是正确的,并且一旦为我自己的pr修改过,代码就是正确的。”