最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - add woocommerce archive description if exist

programmeradmin2浏览0评论

i want show category description on my archive product page

i add this code to my template:

<?php if ( category_description() ) ?>
<div class="myproductdescription"><?php echo category_description(); ?></div>

and add this css code to style.css:

.myproductdescription{
 position: relative;
background-color: #fff;
-webkit-transition: all .3s ease;
-moz-transition: all .3s ease;
-o-transition: all .3s ease;
transition: all .3s ease;
border-radius: 7px;
box-shadow: 0 2px 2px 0 rgba(168, 172, 185, .3);
padding: 15px;
margin-bottom: 20px;
line-height: 25px;}

its work! but show box shadow and padding in archive product page is`t description!

I want not show if category not description

i want show category description on my archive product page

i add this code to my template:

<?php if ( category_description() ) ?>
<div class="myproductdescription"><?php echo category_description(); ?></div>

and add this css code to style.css:

.myproductdescription{
 position: relative;
background-color: #fff;
-webkit-transition: all .3s ease;
-moz-transition: all .3s ease;
-o-transition: all .3s ease;
transition: all .3s ease;
border-radius: 7px;
box-shadow: 0 2px 2px 0 rgba(168, 172, 185, .3);
padding: 15px;
margin-bottom: 20px;
line-height: 25px;}

its work! but show box shadow and padding in archive product page is`t description!

I want not show if category not description

Share Improve this question asked Apr 28, 2020 at 23:10 mostafamostafa 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You're not opening and closing the if statement correctly, so the div is always being output. While you can use if statements without the opening and closing parts like this if there's only one line of code, that doesn't work if the PHP tags are closed for HTML.

So you need to have this:

<?php if ( category_description() ) : ?>
    <div class="myproductdescription"><?php echo category_description(); ?></div>
<?php endif; ?>

Or

<?php if ( category_description() ) { ?>
    <div class="myproductdescription"><?php echo category_description(); ?></div>
<?php } ?>

I prefer the more verbose style when mixed in with HTML, but either one is fine.

Generally you should always avoid using the if statement without opening and closing brackets, even if you've only got one line of code in the condition. It makes your code less clear and can cause serious issues if code that is supposed to be in the condition is added on a new line without adding the brackets. A major vulnerability in iOS was caused by this mistake in 2014.

发布评论

评论列表(0)

  1. 暂无评论