WordPress.org

Forums

Jetpack by WordPress.com
[resolved] Customize Widget Visibility (4 posts)

  1. snapalot
    Member
    Posted 1 year ago #

    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?

    https://wordpress.org/plugins/jetpack/

  2. Richard Archambault
    Happiness Engineer
    Plugin Author

    Posted 1 year ago #

    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:
    http://jetpack.me/contact-support/

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

  3. snapalot
    Member
    Posted 1 year ago #

    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'); });
    </script>
    </div>
    
    <?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'); });
    </script>
    </div>
    
    <?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'); });
    </script>
    </div>
    <?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 http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_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;
    }

    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.

  4. Richard Archambault
    Happiness Engineer
    Plugin Author

    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Jetpack by WordPress.com
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic