WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
Can't get widget to show up on appropriate pages (6 posts)

  1. megmo
    Member
    Posted 3 years ago #

    I have a widget that I need to show up everywhere EXCEPT for on the blog page, a listing of all posts from the blog category (mysite.com/category/blog) and any individual posts in the category "blog."

    I placed the following code in the WidgetLogic area of the widget (it's just a text widget), but the widget keeps showing up on individual posts:

    !is_category('blog') || (is_single() && !in_category('blog'))

    To me, this should say that the widget should not show up if the category is blog OR if it's a single post AND in the category 'blog'. But it keeps showing up on individual posts that are in the "blog" category (the only category I am using on blog posts).

    What am I doing wrong?

  2. Why not just do !is_category('blog')? That should cover it.

  3. megmo
    Member
    Posted 3 years ago #

    I tried that, too, but it doesn't work. I read on the Widget Logic FAQ page that in order to make a widget appear both on a category page and a single post within that category, you'd put

    is_category('blog') || (is_single() && in_category('blog'))

    I figured if that makes it appear, then adding ! should make it disappear. But it doesn't work on the individual posts.

  4. Try category number and not name?

  5. Thorned Rose
    Member
    Posted 3 years ago #

    There's a slight error in the code is_category('blog') || (is_single() && in_category('blog')

    You need to get rid of the '(' beside is_single so it looks like:

    is_category('blog') || is_single() && in_category('blog')

    Try that - I don't know if that will solve your problem but I copy pasted in that code too and didn't notice the bracket in the wrong place till it spat out an error.

  6. mcdonna
    Member
    Posted 3 years ago #

    megmo, the problem is in your logic.

    if ( a || b ) then do x

    will do x if either a is true, or b is true. In this case, !is_category('blog') is true for the index, all categories but 'blog', and all single posts. So the second condition is never even tested unless you are on the blog category page.

    You have two choices. You can list all the disallowed possibilities (not index, not blog cat, not single in blog cat):
    !is_home() && !is_category('blog) && !(is_single() && in_category('blog'))

    or you can list the two allowed possibilities:
    ( is_category() && !is_category('blog') ) || ( is_single() && !in_category('blog') )

    Two other interesting bits of logic are:
    !(A and B) == !A or !B
    !(A or B) == !A and !B

    Which means that
    !( is_home() || is_category('blog) || (is_single() && in_category('blog')) )
    is equivalent to the first solution I suggested.

    [Aside to Thorned Rose: the syntax was correct; megmo had a closing paren which got lost when you copied the code.]

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic