Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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