Php根据帖子类型显示不同的结果

时间:2014-11-21 作者:James Middleditch

我正在努力搜索。php模板。我想根据他们的帖子类型显示不同的结果。我有页面、帖子和机器(自定义帖子类型)

下面是当前结果页面的一个示例:http://dev.digitalgroupmedia.com/fortron/?s=fortron&search-submit=Submit+Query第一个结果是一个帖子,其他的都是机器

页面和帖子可以用一个简单的超链接标题来显示,但我希望机器具有当前的格式。

我的php并不令人惊奇,通常是通过随机尝试和错误拼凑起来的。我知道需要某种类型的if/else语句模板来检查帖子类型,然后在此基础上显示它们,但我很挣扎。任何帮助都将不胜感激。以下是我目前的代码:

<?php get_header(); ?>
“”的搜索结果

        <?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 个回复
SO网友:Laxmana

你可以在你的循环中这样做

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
}

结束

相关推荐

有没有办法阻止WP编辑删除<gcse:earchbox-only></gcse:search box>

我想将我的谷歌自定义网站搜索代码粘贴到编辑器中,以显示在我的一个页面上。当粘贴到“文本”侧时,它会起作用,但如果我切换到“视觉”,get会变成一个NBSP有没有办法让我<gcse:searchbox-only></gcse:searchbox-only> 编辑是否得体?