• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Is this possible?: If a post has the tag X, then add this HTML

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Is this possible?: If a post has the tag X, then add this HTML

This topic is: not resolved

Tagged: php

  • This topic has 1 reply, 2 voices, and was last updated 2 years, 9 months ago by andytc.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • June 24, 2020 at 8:32 am #499511
    koksalcanberk
    Participant

    Hi there,

    Usually, when I want to add a site-wide HTML code (before the closing the body tag) to all of my posts and pages in my WordPress site, I always go to:

    Wordpress Panel > Customize > Theme Settings > Header/Footer Scripts > Footer Scripts

    And paste the code there.

    Works perfectly fine!

    But...

    Now I want to add this code only to some certain posts of mine - to those with certain post tags (let's call them X, Y and Z).

    This is my code:

    <div id="mediavine-settings" data-blacklist-leaderboard="1"></div>

    Here's what I tried out:

    I logged in to my WordPress Panel, navigated to functions.php file and finally added this code at the bottom:

    function function1() {
      if (has_tag ('X' 'Y' 'Z')) { 
        ?>
    <div id="mediavine-settings" data-blacklist-leaderboard="1"></div>
        <?php
      }
    }
    add_action('wp_footer', 'function1');

    But it doesn't work. The file doesn't even get updated, the code gets rejected.

    Any words of wisdom?

    June 25, 2020 at 7:53 am #499548
    andytc
    Participant

    You can try adding this to functions.php in the child theme - change the tag names to your own and play with it to get what you want.

    //Adding custom ID (mediavine-settings) to article class output
    add_filter( 'genesis_attr_entry', 'koksalcanberk_add_tag_id' );
       
    global $post;
    
    function koksalcanberk_add_tag_id( $attributes ) {
    	if( has_tag( array( 'tag-x','tag-y','tag-z', $post  ) ) ) {
    	
    	 $attributes['id'] = 'mediavine-settings';
    	 
    	}
    
     	return $attributes;
    }

    You could also add this into your custom CSS so you can see visually where it's being added , it'll be on archives , singles , anywhere with those tag. just delete the CSS when your happy it's working as intended.

    #mediavine-settings {
        background: #FF9800;
    }
  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble