Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Show code only on one page

Hello, I have this code but it does not work in wordpress, it will work only in plain .php site...

I want this code to be shown only on home page, how can I do that?

Thanks

```php 
        <?php 
            if( basename($_SERVER['PHP_SELF'], '.php') == 'index' ) {
                echo '<div class="logo">';
                echo '<img src="', bloginfo('template_directory'), '/images/logo.svg" alt="">';
                echo '</div>';
            }
        ?>
```

2 Answers

Shaker Advertising
Shaker Advertising
5,395 Points

You can use Wordpress conditionals to check if it's the homepage or not like this:

<?php
  if(is_home()){  // this checks if it's the home page
    // echo your logo here
  }
?>

Or if you have front-page.php set up in your theme you can use:

  <?php
    if(is_front_page()){
      // echo you logo here
    }
  ?>

Here is the codex link: http://codex.wordpress.org/Function_Reference/is_front_page

This is awesome :) Thanks.

Do you maybe know if I could do something similar to show same code for every page of custom post type?

Shaker Advertising
Shaker Advertising
5,395 Points

Ivan Tomicic If you're looking to add it to the single post template you can use:

<?php
  if( is_single( array( 'foo', 'bar' ) ) ){  // foo and bar are names of your custom post types
    // echo content
  }
?>

Here is a reference for all the conditional tags within Wordpress: http://codex.wordpress.org/Conditional_Tags