Support » Fixing WordPress » Custom code for posts and another for archives/home

  • marcorroma

    (@marcorroma)


    Hi, I need to enter a code in the script type=”application/ld+json”, in head.
    I need to specify a different code for posts, and one for the homepage and archives.
    What is the code that can help me?
    Thanks in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • Nikhil Bhansi

    (@nikhilbhansi)

    I think you are trying to add Schema markup for these pages.
    Instead of placing that in the header.php file, I would push it through the wp_head hook.
    Please add the following code in your theme’s function file and test it first is it showing alert box for Home, Archive, and Post pages.
    If Yes, then replace the lines with your code.

    /**
     * Script for Home, Archive and Post Pages
     * https://wordpress.org/support/topic/custom-code-for-posts-and-another-for-archives-home/
     */
    add_action( 'wp_head', 'my_header_scripts' );
    
    function my_header_scripts() {
        
        if ( is_front_page() ) {
            ?>
            <script>alert( 'This is Front Page!' ); </script> <!--Replace this line with your code for Home Page-->
            <?php
        } else if ( is_archive() ) {
            ?>
            <script>alert( 'This is Archive Page!' ); </script> <!--Replace this line with your code for Archive Page-->
            <?php 
        } else {
            ?>
            <script>alert( 'This is for other Pages!' ); </script> <!--Replace this line with your code for Post Page-->
            <?php 
        }
        
    }
    marcorroma

    (@marcorroma)

    Thanks a lot!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom code for posts and another for archives/home’ is closed to new replies.