Support » Fixing WordPress » Can you tell me what is wrong? It doesn’t work with is_page_template

  • Can you tell me please what is wrong? I want to put this on header.php

    <?php if (is_page_template(‘image.php’)) echo(‘<meta name=”robots” content=”noindex,follow” />’); ?>

    If I put for example is_home, works fine… but not with is_page_template, can you tell me how to fix it?

    Thanks in advance.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Can anyone help me, please?

    is_page_template takes the Template Name as the argument, not the template’s filename.

    If image.php contained this:

    Template Name: My Template

    Then you’d use this code:
    if (is_page_template('My Template'))

    Thanks for your answer Otto.

    I did it, but it doesn’t work :(, is only happening me with this template… What is wrong?

    In header.php

    <?php if (is_page_template('Imagenes')) echo('<meta name="robots" content="noindex,follow" />'); ?>

    In image.php

    Template Name: Imagenes

    I hope your help!

    Ps: My hole <head> in header.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head profile="">
    	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    	<title><?php if (is_archive()) { wp_title(' &mdash;',true,'right'); } else { wp_title('&mdash;',true,'right'); } ?><?php bloginfo('name'); ?></title>
    	<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats -->
    	<?php if (is_page_template('Imagenes')) echo('<meta name="robots" content="noindex,follow" />'); ?>
    	<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/custom.css" type="text/css" media="screen" />
    	<!--[if lte IE 7]>
    	<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/ie7.css" media="screen" />
    	<!--[if lte IE 6]>
    	<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/ie6.css" media="screen" />
    	<link rel="icon" href="" />
    	<link rel="shortcut icon" href="" />
    	<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
    	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    	<?php wp_head(); ?>
    	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery.js"></script>
    	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/cycle.js"></script>

    Hmm.. Looking closer at the code, maybe I’m wrong. Your original is_page_template('image.php') should have worked.

    Let me examine it closer.

    Okay, I think that it might be dependent on the Loop having started already. Try putting this into the Loop part of the Page Template itself:
    if (is_page_template('image.php')) echo "Hi, we're in image.php!";

    Put the same code before and after the Loop and see where it works.

    Otto, I’m a really novice in these topics…

    You mean?

    <?php if (is_page_template('image.php')) echo "Hi, we're in image.php!"; if (is_page_template('image.php')) echo('<meta name="robots" content="noindex,follow" />'); if (is_page_template('image.php')) echo "Hi, we're in image.php!"; ?>

    It doesn’t work 🙁 Nothing shows.

    No, no, after The Loop. You know, the bit that actually shows the Page content itself?

    The info in the link is very old… Let me see…

    You mean..?

    <?php if (is_page_template('image.php')) echo "Hi, we're in image.php!"; if (have_posts()) : while (have_posts()) : the_post(); if (is_page_template('image.php')) echo "Hi, we're in image.php!"; ?>

    Or these:

    <?php if (have_posts()) : while (have_posts()) : the_post(); if (is_page_template('image.php')) echo "Hi, we're in image.php!"; ?>
    <?php if (is_page_template('image.php')) echo "Hi, we're in image.php!"; endwhile; else: ?>

    Nothing happen…

    Thanks for your answers Otto.

    Anyone know how to solve these? I don’t want Google indexes my hundreds, thousands of images.


    Simple, use robots.txt to block search robots from your uploads directory.

    I did it something like that, but is not the best… Is not standart, but works for Google.

    Disallow: /blog/*/*/*/*-*

    The other possibily… How can I add rel=nofollow to all thumbmails? Do you know?


    Wait a second.. Is this image.php actually a PAGE TEMPLATE, or are you using the attachment thing instead? Because an Attachment is not a Page is not a Post. is_page_template only works on actual Pages, as in things you make using “Write Page”.

    Have you tried “is_attachment” instead?

    NOW WORKS!!!

    <?php if (is_attachment()) echo('<meta name="robots" content="noindex,follow" />'); ?>

    With this codes… If I put (is_attachment(‘image.php’)). How to limit just to image.php, I did it well?

    Thanks Otto42 🙂

    is_page_template() function first tests to see if the content is a Page (eg. About, Contact…):

    1153  function is_page_template($template = '') {
    1154      if (!is_page()) {
    1155          return false;
    1156      }
    1158      global $wp_query;
    1160      $page = $wp_query->get_queried_object();
    1161      $custom_fields = get_post_custom_values('_wp_page_template',$page->ID);
    1162      $page_template = $custom_fields[0];
    1164      // We have no argument passed so just see if a page_template has been specified
    1165      if ( empty( $template ) ) {
    1166          if (!empty( $page_template ) ) {
    1167              return true;
    1168          }
    1169      } elseif ( $template == $page_template) {
    1170          return true;
    1171      }
    1173      return false;
    1174  }

    There should also be a is_single_template() because Post attachements are considered Posts too.

    Therefore the following code in a Post with image attachements will never output because the condition is always false:

    <?php if ( is_page_template('image.php') ) { ?>
    			<li><h2>Previous</h2><?php previous_image_link() ?></li>
    			<li><h2>Next</h2><?php next_image_link() ?></li>
    		    <?php } ?>
Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Can you tell me what is wrong? It doesn’t work with is_page_template’ is closed to new replies.