WPNovice WPNovice - 2 months ago 10
PHP Question

if is home, check post type, echo if certain result? (wordpress)

Hi i'm wondering if someone could help me with the correct syntax for checking for a post type when on the index feed, currently I'm trying this -

<div class="<?php if(is_home() )
{<?php if ( 'movies' == get_post_type() ) { echo 'textbox';}?> ;}?>">
</div>

Answer

No need for multiple PHP open tags.

<div class="<?php if (is_home() && (get_post_type() == 'movies')) { echo 'textbox';} ?>">    </div>

What I would probably do in this situation is clear it up a bit:

<?php
$class = (is_home() && get_post_type()==='movies') ? 'textbox' : '';
echo '<div class="', $class, '"></div>';