Support » Fixing WordPress » is_page_template() is not working

  • Why following code doesn’t work?

    if ( is_page_template( 'frontpage1.php' ) ) {
        function enqueue_xxxx() {
        ?>
        <script type="text/javascript">
        .....
        </script>
        <?php
        }
        add_action('wp_footer', 'enqueue_xxxx');
        }
    • This topic was modified 2 years, 11 months ago by  m1000.
    • This topic was modified 2 years, 11 months ago by  Steve Stern. Reason: put code in code tags
Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    In what way is it not working?

    It doesn’t display script in footer, when I remove
    if ( is_page_template( 'frontpage1.php' ) ) { }
    condition then it works but I want to didplay it on frontpage1.php temmplate.

    • This reply was modified 2 years, 11 months ago by  m1000.
    • This reply was modified 2 years, 11 months ago by  m1000.
    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    Use the plugin “show current template” to double check on what is the “master” template for rendering a page.

    It’s showing Template: frontpage1.php on toolbal

    BTW I use this code in functions.php

    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    OK, just checking for low hanging fruit. And that template is in the top level of the theme directory?

    OH, just re-looked at your code. Remove the function def from the conditional — it should be like this:

    function enqueue_xxxx() {
       if ( is_page_template( 'frontpage1.php' ) ) {
          ?>
            <script type="text/javascript">
            .....
            </script>
            <?php
        }
     }   
        
    add_action('wp_footer', 'enqueue_xxxx');

    Thanks but I used this code and it’s not working

    function enqueue_xxxx() {
       if ( is_page_template( 'frontpage1.php' ) ) { ?>
        CODE
      <?php
        }
     }
    
    add_action('wp_footer', 'enqueue_xxxx');
    • This reply was modified 2 years, 11 months ago by  m1000.
    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    Let’s try an experiment:

    function enqueue_xxxx() {
      echo '<!-- Page template is ' . get_template_slug() . ' -->';
    
     }
    
    add_action('wp_footer', 'enqueue_xxxx');

    run that and see what appears in the page source

    It’s breaking the page
    This code also breaking the page after Page template is

    function enqueue_xxxx() { ?>
      Page template is <?php echo get_template_slug();
    
    }
    add_action('wp_footer', 'enqueue_xxxx');
    • This reply was modified 2 years, 11 months ago by  m1000.
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘is_page_template() is not working’ is closed to new replies.