Support » Plugin: Head & Footer Code » adding different header codes, based on WP categories

  • Hallo!
    I need to add a dozen of different javascript codes, based on the catogories/tags of the posts.

    For example:

    – posts tagged with “red” should be get the “red” version of the code (in the body)
    – posts tagged with “green” should be get the “green” version of the code (in the body)

    Do you know how I could achieve that?

    Thank you

    Michele

Viewing 1 replies (of 1 total)
  • Plugin Author Aleksandar Urošević

    (@urkekg)

    Hi @agrycult,

    I’m not clearly sure what you trying to achieve, but you could look for post tag links, and run JS code which you need for that particular tag.

    For example, here is an code example for default WP theme Twenty Fifteen which will run on single posts only if post have tags green, red or blue, and as a fallback default case if none of these 3 specific tags are present in post:

    if ( jQuery('body').hasClass('single-post') {
      if ( jQuery('.tags-links a[href$="/tag/green/"]').length > 0 ) { 
        // JS code for green tag
      } else if ( jQuery('.tags-links a[href$="/tag/red/"]').length > 0 ) { 
        // JS code for red tag
      } else if ( jQuery('.tags-links a[href$="/tag/blue/"]').length > 0 ) { 
        // JS code for blue tag
      } else { 
        // JS code for all other tags
      }
    }

    If you wish to include some external JS file depending on tag, it’s better to you code that functionality in your child theme.

    Kind regards,
    Aleksandar

Viewing 1 replies (of 1 total)
  • The topic ‘adding different header codes, based on WP categories’ is closed to new replies.