简体   繁体   English

如果在wordpress中选中,则显示来自特定类别的1条帖子

[英]Displaying 1 post from a particular catergory if selected in wordpress

I'm trying to make a loop in wordpress that pulls in 1 post from 1 category when selected, But if not it should display the latest post from the custom post type I'm calling. 我正在尝试在wordpress中创建一个循环,以使其在被选中时从1个类别中提取1个帖子,但是如果没有,它应该显示我正在调用的自定义帖子类型的最新帖子。 eg Testimonials, Latest News, Case Studies. 例如推荐书,最新消息,案例研究。

Here is what I have, do I need an ifelse; 这就是我所需要的,我需要吗? statement to query if the cat doesn't exist then display the latest post in that CPT. 查询猫是否不存在的语句,然后在该CPT中显示最新帖子。

Here is the code I have. 这是我的代码。

  <?php
                $query = new WP_Query('post_type=testimonial&catergory_name=home&showposts=1&paged=' . $paged);
                $postcount = 0;
            ?>
            <?php if ($query->have_posts()) : ?>
                <?php while ($query->have_posts()) : $query->the_post(); ?>
                    <?php $postcount++; ?>
                    <li> 
                     <A HREF="<?php the_permalink(); ?>"></A>
 <a href="<?php the_permalink(); ?>">
                        <?php
                            if (has_post_thumbnail()) {
                                // check if the post has a Post Thumbnail assigned to it.
                                the_post_thumbnail('thumb-casestudy');
                            }else {
                            ?>
                            <img src="<?php bloginfo('template_url'); ?>/assets/images/default.jpg" alt="<?php the_title(); ?>"/>
                            <?php } ?>
 </a>
                        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                        <p class="hm_text"><?php 
                                //the_excerpt(); 
                                echo get_post_meta($query->post->ID, 'wpld_cf_home_page_text', true) 
                        ?></p>




                    </li>
                    <?php endwhile; ?>
                <?php else : ?>
                <?php endif; ?>

If I understand You correctly: 如果我正确理解您:

$query->post_count

should allow You to check if there are any post returned by query, and if not, than You can call another query. 应该允许您检查查询是否返回了任何帖子,如果没有,则可以调用另一个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM