0

I have an if statement that checks for specific category ID's and displays HTML if the condition is true. I have many categories and would like to only apply the HTML to categories that do have a parent.

Is there a way to check if the category has a parent?

The current code is rather heavy, and requires manual intervention when a new child category is added - there must be an easier way.

<?php if(in_array($_category->getId(), array(117,179,121,123))): ?>
        <p>Display this text.</p>
<?php else: ?>
        <p>Display this text instead.</p>
<?php endif; ?>
2

There are several ways to do this:

# not first level
if ($_category->getParentCategory()->getLevel() > 1) {

# parent is not root
if ($_category->getParentId() == Mage::app()->getStore()->getRootCategoryId()) {

# not root and not first level
if (count($_category->getParentIds()) > 2) {

# has parent anchor category
if (count($_category->getAnchorsAbove()) {

Try var_dump(get_class_methods($_category)) ... guess there are more possibilies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.