自定义帖子类型即将发布和过去发布

时间:2013-11-21 作者:agis

我想在我的archive-CPT.php 最近即将发布的帖子(状态为future) 和过去的帖子(最早发布的帖子)。

这就是我存档CPT的方式。php看起来像:

 <?php

get_header(); ?>

<div id="primary" class="row">

    <?php if($layout == \'left_side\'){ ?>
    <aside id="side-bar" class="span4">
            <?php dynamic_sidebar( \'sidebar_left\'); ?>
    </aside>
    <?php } ?>  
    <?php if($layout == \'both_side\'){ ?>
    <aside id="side-bar" class="span3">
            <?php dynamic_sidebar( \'sidebar_left\' ); ?>
    </aside>
    <?php } ?>  

    <div id="content" class="margin span9" role="main">
    <?php 

        if( have_posts() ){ 
            // normal blog layout
                $x = 1;
                while ( have_posts() ){
                    the_post(); 
                    if ( 0 === (int) $post->post_parent ) {
                    get_template_part( \'inc/post-format/content-debate\');
                }

                    $x++;
                }
            }

        else{ ?>
        <article class="type-page box">
            <h1 class="title"><?php _e(\'Post not found\', \'outbox\'); ?></h1>
            <div class="the-content">
            <p class="lead"><?php _e(\'We could not find that post you were looking for.\', \'outbox\'); ?></p>
            <br>
            <h3><?php _e(\'Try searching\', \'outbox\') ?></h3>
            <?php echo get_search_form(); ?>
            <?php get_template_part( \'inc/recent-posts\' ); ?>               
            </div>
        </article>
        <?php } 

        kriesi_pagination(); 

        ?> 
        <div class="debatesinfo">
        <div class="row-fluid">
            <div class="span6">
            <h3>UPCOMING DEBATES</h3>
            <?php
            $query = new WP_Query( array(
             \'post_status\' => \'future\',
             \'orderby\' => \'date\', 
             \'order\' => \'ASC\', 
             \'posts_per_page\' => 1 
           ) );

         if( $query->have_posts() ) {
         while( $query->have_posts() ) {
         $query->the_post();
         echo the_title();
    } 
  }     
  ?>
    </div>
    </div><!-- #content -->
    <?php if($layout == \'right_side\'){ ?>
    <aside id="side-bar" class="span4">
            <?php dynamic_sidebar( \'sidebar_right\' ); ?>
    </aside>
    <?php } ?>      
    <?php if($layout == \'both_side\'){ ?>
    <aside id="side-bar" class="span3">
            <?php dynamic_sidebar( \'sidebar_right\' ); ?>
    </aside>
    <?php } ?>      
</div><!-- #primary -->
此代码来自functions.php 用于检索主循环的最新发布帖子:

function wpse124228_alter_ppp_order_for_mycpt( $query ) {
if ( ! $query->is_main_query() || is_admin() )
    return;
if ( is_post_type_archive( \'debate\' ) ) {
    //Only display 1 post on mycpt archive
    $query->set( \'posts_per_page\', 1 );
    //Most recent/current
    $query->set( \'orderby\', \'date\' );
    $query->set( \'order\', \'DESC\' );
 }
  }
add_action( \'pre_get_posts\', \'wpse124228_alter_ppp_order_for_mycpt\' );
关于如何检索即将发布的文章、最后一篇文章的标题和缩略图,有什么建议吗?

Eric Holmes编辑:修复了代码中的一个拼写错误。

1 个回复
最合适的回答,由SO网友:Eric Holmes 整理而成

For Future Posts, you can just have Published Posts that are scheduled for the future. When editing a post, you can click on Edit beside "Publish Immediately" and select a date/time.

You can then have a subloop get all posts with the post_status of future. That should work just fine for you!

WP_Query documentation

Future:

$query = new WP_Query( array(
    \'post_status\' => \'future\',
    \'orderby\' => \'date\', 
    \'order\' => \'ASC\', 
    \'posts_per_page\' => 1 
) );

if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        echo the_title();
    }
}

Oldest

$query = new WP_Query( array(
    \'orderby\' => \'date\', 
    \'order\' => \'ASC\', 
    \'posts_per_page\' => 1 
) );

if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        echo the_title();
    }
}
结束

相关推荐

只在本地主机上运行的Functions.php代码?

我想在函数中运行add\\u操作。仅当主题是从我的localhost开发站点加载时才使用php。如何使其仅在本地主机上运行?function livereload(){ ?> // mycode <?php } add_action(\'headway_body_close\', \'livereload\');