0

I am trying to display images if a child category is selected however when a child category is not selected I want to display a message saying its not available. The below code works to display the images however I cannot seem to correctly add the code that displays message if no child category is selected.

foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(210, $childcat)) {

  echo '<img src="/wp-content/themes/Bootstrap/images/flags/';
  echo $childcat->slug;
  echo '.png" alt="';
  echo $childcat->cat_name;
  echo '" title="';
  echo $childcat->cat_name;
  echo '" style="width:28px;height:20px;border-radius:20px;"> ';

}}

Below is a working conditional however it does not display all the child categories like the code above.

if (is_category_or_sub(210)) {

  echo '<img src="/wp-content/themes/Bootstrap/images/flags/';
  echo $childcat->slug;
  echo '.png" alt="';
  echo $childcat->cat_name;
  echo '" title="';
  echo $childcat->cat_name;
  echo '" style="width:28px;height:20px;border-radius:20px;"> ';

} else {
  echo 'Sorry This Movie Is Not Available';
}

Here is it in action: http://netflix.rhgfx.com/life-itself/

its the availability column on the left.

  • What is is_category_or_sub()? – s_ha_dum May 11 '15 at 14:28
  • I found that code using Google. I am not sure what the exact PHP call is. – user3009948 May 11 '15 at 14:30
1

I don't know what is_category_or_sub() and that function is the critical piece of code. Without it, the question of why your code doesn't work isn't answerable but there is code in the Codex that seems to do what you need:

/**
 * Tests if any of a post's assigned categories are descendants of target categories
 *
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, 'category' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        }
        return false;
    }
}

And ...

if ( in_category( 210 ) || post_is_in_descendant_category( 210 ) ) {
    // In the category
} else {
    // not in the category
}
  • Thank you very much but unfortunately that does not work. In a nutshell, I would like the flags (which are child categories of 'Availability') to display if any of them are checked in the back-end and if none are checked it would say "Not Available". I am hope I am being clear, thank you for your time. The first code displays them and the 2nd code does the "Not Available" function, so in essence I am looking for a combination of both codes. – user3009948 May 11 '15 at 14:55
  • I made a couple of edits to make the answer more clear, but I don't see why this wouldn't work. – s_ha_dum May 11 '15 at 15:43
  • I have tried it, it does not list out the child categories so I am able to make the flag images work. It simply allows me to put data there but I need to be able to list out the child categories to replace the text with flag images. Note - how on this post there is a list of flags: netflix.rhgfx.com/life-itself those are each child categories, that I used the echo output to make images. – user3009948 May 11 '15 at 17:11
  • Nevermind, this is correct. It just took some tweaking. Thank you very much for your time and effort. – user3009948 May 11 '15 at 23:07

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.