WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] If conditional statement - if category is a parent category (10 posts)

  1. rjpinney
    Member
    Posted 1 year ago #

    I need an if statement to include in my category template. I want to show some information, but only if the category that is being viewed is a parent category.

    Can anyone advise on what conditional statement I need to use here?

    <?php if ( ?????? ) : ?>

    Thanks

  2. tiaanswart
    Member
    Posted 1 year ago #

    try this:

    $category = get_the_category();
    if ($category->category_parent !== '') {
    # code here
    }
  3. rjpinney
    Member
    Posted 1 year ago #

    Thanks for your response, I've put that in but it crashes the page if I try and put code (or anything) into it - I may have implemented it wrong.

    This is what I have:

    <?php
    $category = get_the_category();
    if ($category->category_parent !== '')
    { }
    ?>
  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Try if ($category->category_parent != '')

  5. tiaanswart
    Member
    Posted 1 year ago #

    oops thanks esmi :P

  6. rjpinney
    Member
    Posted 1 year ago #

    I'm afraid I've still got the same problem..

  7. tiaanswart
    Member
    Posted 1 year ago #

    paste your exact code please...

  8. rjpinney
    Member
    Posted 1 year ago #

    <?php
    $category = get_the_category();
    if ($category->category_parent != '')
    {  }
    ?>

    If I put anything in the area you entered for code, it crashes the page.

    I want to then list the child categories as links, if that makes any difference.

    Thanks for all your help, appreciated!

  9. tiaanswart
    Member
    Posted 1 year ago #

    Tried and tested on my own site here you go:

    <?php
        $category = get_the_category();
        if ($category[0]->category_parent != '') {echo 'hello world!' ;} ?>
  10. rjpinney
    Member
    Posted 1 year ago #

    Thanks so much, it's working!

Topic Closed

This topic has been closed to new replies.

About this Topic