WordPress.org

Support

Support » Themes and Templates » [Resolved] If conditional statement – if category is a parent category

[Resolved] If conditional statement – if category is a parent category

  • 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

Viewing 9 replies - 1 through 9 (of 9 total)
  • try this:

    $category = get_the_category();
    if ($category->category_parent !== '') {
    # code here
    }

    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 !== '')
    { }
    ?>
    esmi

    @esmi

    Forum Moderator

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

    oops thanks esmi 😛

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

    paste your exact code please…

    <?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!

    Tried and tested on my own site here you go:

    <?php
        $category = get_the_category();
        if ($category[0]->category_parent != '') {echo 'hello world!' ;} ?>

    Thanks so much, it’s working!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] If conditional statement – if category is a parent category’ is closed to new replies.
Skip to toolbar