0

I'm trying to work on my search.php template. I'd like to display results differently based on their post type. I have Pages, Posts and Machines (a custom post type)

Here's an example of the results page at the moment: http://dev.digitalgroupmedia.com/fortron/?s=fortron&search-submit=Submit+Query The first result is a post, the others are Machines

Pages and Posts can be displayed with a simple hyperlinked title, but I'd like the machines to have the format they currently have.

My php isn't amazing and is usually cobbled together through random trial and error. I know there needs to be some sort if/else statement template to check the post type and then display them based on that, but I'm struggling. Any help would be really appreciated. Here's my code thus far:

<?php get_header(); ?>

Search Results for ''

        <?php $i = 1;?>
        <?php if (have_posts()) : ?>
            <div class="products row-fluid clearfix">

            <?php while (have_posts()) : the_post(); ?>

                <div class="span3 machine">
                    <a href="<?php the_permalink(); ?>">
                    <?php
                        $machineImage = types_render_field( "machine-image", array( "alt" => "Fortron Machine Tools", "title" => "Fortron Machine Tools", "width" => "184", "height" => "162", "align" => "center", "resize" => "pad", "padding_color" => "transparent", 'index' => 0 ) );
                            if ($machineImage){
                                echo $machineImage;
                            };
                    ?>
                    </a>
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                </div>    

            <?php if($i % 4 == 0) {echo '</div><!-- end machines row fluid --><div class="row-fluid clearfix">';} ?>
            <?php $i++; endwhile; ?>
            </div><!-- end products row fluid -->

            <?php else : ?>

            <strong>Sorry, nothing found.</strong>

        <?php endif; ?>

        </div><!-- span8 end -->
        <?php get_sidebar(); ?>
    </div>
</div><!-- container end -->

1 Answer 1

2

You can do something like this inside your loop

if ( 'machine' == get_post_type() ){
           //code for machine post types
}elseif ( 'post' == get_post_type()){
           //code for posts
}elseif ( 'page' == get_post_type()){
           // code for pages
}
else{
           //code for anything else
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.