如何在循环中返回自定义帖子?

时间:2016-08-07 作者:Ibraheem

如何在循环中返回自定义帖子?我读了几篇关于同一主题的文章,但没有直接的内容。我有一个显示常规帖子的博客,我有自定义帖子“广告”。我喜欢开始在循环中显示最新的自定义帖子。最终结果可以是只返回自定义帖子的循环。

 <?php if ( ! have_posts() ) : ?>
<div id="post-0" class="post not-found item-list">
    <h2 class="entry-title"><?php _e( \'Not Found\', \'tie\' ); ?></h2>
    <div class="entry">
        <p><?php _e( \'Apologies, but no results were found for the  requested archive. Perhaps searching will help find a related post.\', \'tie\' ); ?></p>
        <?php get_search_form(); ?>
    </div>
</div>

  <?php else : ?>
   <?php
  $count = 0;
  $post_width = tie_post_width();
  $exc_home_cats = tie_get_option( \'exc_home_cats\' );
if( is_home() && $exc_home_cats ) query_posts( array(  \'advert\',\'category__not_in\' => $exc_home_cats , \'paged\' => $paged) ); ?>
<?php while ( have_posts() ) : the_post(); $count++;
$color = \'\';
$get_meta = get_post_custom($post->ID);
$tie_post_color = $get_meta["tie_post_color"][0];
if( !empty($tie_post_color)) $color =\'custom-color \'.$tie_post_color;
?>  

<article <?php post_class(\'item-list post rtl-item \'.$color.\' \'.$post_width) ?>>
    <div class="post-inner">            
        <?php 
            $format = get_post_format();
            if( false === $format ) { $format = \'standard\'; }
        ?>
        <?php get_template_part( \'content\', $format ); ?>


</article><!-- .item-list -->


<?php endif; ?>
谢谢你的帮助!

1 个回复
SO网友:Ibraheem

以下代码与上述代码一起使用:

<?php $loop = new WP_Query( array( \'post_type\' => \'yourposttypehere\', \'posts_per_page\' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
The stuff you want to loop goes in here
<?php endwhile; wp_reset_query(); ?>
资料来源:link

相关推荐

当in_the_loop()为假时,何时以及为什么is_Single(‘my_cpt’)为真?

我正在使用模板系统的示例代码。此地址的页码:http://project.test/my_cpt/hello-post/.无法理解原因is_singular( \'my_cpt\' ) 是true 虽然in_the_loop() 是false.在页面模板中The Loop "E;“工程”:if ( have_posts() ) { while ( have_posts() ) { the_post(); ?>