Question

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! enter image description here

I want not show if category not description

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top