Support » Plugin: Jetpack by » Customize Widget Visibility

  • Resolved snapalot


    When I choose to display a widget by selecting Show if: Category is News, the widget only shows on the category archive and not on singleposts that has category News.

    It works for tags, but not categories. Can I use code in my functions.php file to modify this function?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Richard Archambault


    Happiness Engineer

    Hi, I can’t reproduce this; the widget shows up properly on single posts in the relevant category. Could you post your site URL here, so I can have a look?

    If you want it to remain private, you can also contact us via this contact form:

    If you contact us through our form, please make sure to include a link to this thread in your message.

    Thank you for your reply Richard 🙂

    I decided to rather use my own conditionals with PHP and put it in a PHP widget. I use it to display the correct Google DFP ad tags on different categories, and the conditionals are being used in my theme’s header.php anyway. Here’s an example of the widget code:

    <?php // News
    if ( is_category( 'news' ) ) { ?>
    <!-- DFP-banner-728x90 -->
    <div id='dfp-news-728x90' style='width:728px; height:90px;'>
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('dfp-news-728x90'); });
    <?php } // Books
    elseif ( is_category( '41' ) || post_is_in_descendant_category( '41' ) && !is_front_page() ) { ?>
    <!-- DFP-books-728x90 -->
    <div id='dfp-books-728x90' style='width:728px; height:90px;'>
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('dfp-books-728x90'); });
    <?php } // Run Of Site
    else { ?>
    <!-- DFP-ros-728x90 -->
    <div id='dfp-ros-728x90' style='width:728px; height:90px;'>
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('dfp-ros-728x90'); });
    <?php } ?>

    The function post_is_in_descendant_category() is defined by

     * 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
    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;

    The problem was probably caused by some conflict with another pugin, but this way I get around it. I’m sorted thanks. If you have any comments about the code above, the feedback would be appreciated.

    Plugin Contributor Richard Archambault


    Happiness Engineer

    It looks good to me, so I’m glad you got it all sorted!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Customize Widget Visibility’ is closed to new replies.