• Resolved curchunflo

    (@curchunflo)


    Hello how are you? I need to insert a different analytics and ads code for each section, each note and my homepage. I have three different ad and analytics scripts, which I want to insert in the head of my homepage, another in the head of the article and another in the head of the category.

    I’ve been reading and could do it using hooks (wp_head) in functions.php.

    The homepage code, I insert it in header.php, so it would not require creating anything, it should for categories and posts.
    This is my code (which I am putting at the end of functions.php), I still don’t know how to do it for posts. Loading it in the functions.php doesn’t give me any error, but it doesn’t work. Could you guide me? I’ve never done this and don’t know where to start:

    function category_head_hook () {
        if (is_category ('category-slug')):
            // Head of categories
    ?>
    <script> my ads code </script>
    <script> my analytics code </script>
    <? php
        endif;
    }
    add_action ('wp_head', 'category_head_hook', 10)

    I would give them the link, but I am working on localhost.
    I greatly appreciate your help! If they could guide me or tell me where to study, they would help me a lot
    Greetings and thank you very much!

    • This topic was modified 3 years, 4 months ago by Jan Dembowski.
    • This topic was modified 3 years, 4 months ago by bcworkz. Reason: code fixed
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    As your code is, the only time you’ll get ads and analytics code is for a request like example.com/category/category-slug/. If even that doesn’t work, it may be due to caching delivering stale content. To add code for any category, do not supply any argument, just
    if ( is_category()):

    But you also want to target home page and posts. I’m unsure exactly what you mean by “posts”, but let’s set that aside for the moment. To target any category request or the home page, do this:
    if ( is_category() || is_front_page()):

    Next, add another OR criteria to target posts. By “posts” do you mean post archives? As in a date archive or similar. Or do you mean single, individual posts? For single posts, use is_singular(). For post archives use is_post_type_archive(). For example:
    if ( is_category() || is_front_page() || is_post_type archive('post')):

    Thread Starter curchunflo

    (@curchunflo)

    Thank you very much! With ‘posts’ I mean a single article. I have wrote this, but it has a parse error

    function cambiar_head_posts(){
    do_action('cambiar_head_posts');
    }
    
    add_action('cambiar_head_posts','imprime_head_posts', 10);
    function imprime_head_posts() {
    global $wp_query;
    $postid = $wp_query->post->ID;
    	//Carga ads y analytics en los posts
    	//
    	?>
    	<script> analytics
    </script>
    	<script> ads
    </script>				
    <?php
    echo $output;
    wp_reset_query();
    
    }

    this is the error I get: syntax error, unexpected 'function' (T_FUNCTION)

    I will try what you propose me.
    Thank you very much!

    • This reply was modified 3 years, 3 months ago by bcworkz. Reason: code format fixed
    Moderator bcworkz

    (@bcworkz)

    Unexpected ‘function’ error usually means the issue is somewhere above where PHP says the error is. The error could be as simple as a missing ; at the end of a line. Or unmatched braces, parenthesis, or quotes. Or missing or extra comma. These can be hard to spot. Using a syntax highlighting editor can help you spot errors.

    Thread Starter curchunflo

    (@curchunflo)

    Thank you very much! Now I have new problems ??

    This is my code now:

    function category_head_hook() {
    	if (is_singular ()):
            // Head of single
    ?>
    <script> my analytics  script for single post </script>
    <script> my ads script for single post </script>
    					<?php
        endif;
       if ( is_category( 'category-slug' )):
    <script>my analytics script for 'categories home page'</script>
    <script> my ads script for 'categories home page' </script>
    					 <?php
        endif;
    }
    add_action( 'wp_head' , 'category_head_hook' , 10)

    If I want to read all the articles that are within the ‘Technology’ category, I click on the main menu and the page loads all the articles that I have written within that section, consequently, the head must contain the code of analytics and the code of the ads, which are specific to be displayed within the technology category, and if I later enter an individual article, the head must load the analytics and ads that are specific to the interior of the articles.

    The category code works perfectly and only shows the analytics and the category homepage ads, when I am inside the category.
    If I am inside an individual article, the code also works and specifically shows analytics and ads of individual articles, but if I am on the home page, it shows me the analytics + ads of the home page, but also the analytics + ads script inside the articles.
    I can’t find why, I appreciate your help enormously. Don’t know why, but I think that is the endif, I wrote that thinking: if you are in 1 do A else, if you are in 2 do B, else if you are in 3 do C, but can’t find the way to say that in PHP,

    thank you very much for your help, really

    • This reply was modified 3 years, 3 months ago by bcworkz. Reason: code format fixed
    Thread Starter curchunflo

    (@curchunflo)

    I made it! hehe, now this is my code and it’s working, thank you @bcworkz for your help!

    function category_head_hook() {
    if (is_front_page()):
    // Head homepage
    ?>
    <script> my analytics script for single post </script>
    <script> my ads script for single post </script>
    <?php
    endif;
    if (is_singular ()):
    // Head single
    ?>
    <script> my analytics script for single post </script>
    <script> my ads script for single post </script>
    <?php
    endif;
    if ( is_category( ‘category-slug’ )):
    <script>my analytics script for ‘categories home page'</script>
    <script> my ads script for ‘categories home page’ </script>
    <?php
    endif;
    }
    add_action( ‘wp_head’ , ‘category_head_hook’ , 10)
    • This reply was modified 3 years, 3 months ago by bcworkz. Reason: code format fixed
    Moderator bcworkz

    (@bcworkz)

    You’re welcome. It hadn’t occurred to me that you’d need slightly different script for each situation. I’m glad to see you were able to adapt.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Insert a different code in the head of home, category and post’ is closed to new replies.