【问题标题】:Wordpress apply date after last post of each day in list列表中每一天的最后一篇文章之后的 Wordpress 应用日期
【发布时间】:2015-02-18 13:22:46
【问题描述】:

你能帮我申请每天的最后一个帖子吗,比如'<span> this is last post of <date> </span>'。这是一个包含对象的数组:

    Array
    (
        [0] => WP_Post Object
            (
                [ID] => 845
                [post_author] => 3
                [post_date] => 2015-02-18 15:01:37
                [post_date_gmt] => 2015-02-18 13:01:37
                [post_status] => publish
                [post_content_filtered] =>
                [post_type] => post
                [post_mime_type] => 
                [comment_count] => 0
                [filter] => raw
...
            )...

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress sorting loops foreach


    【解决方案1】:

    这是一个奇怪的,但我会做的是有点诡计你不关闭当前的帖子容器,直到下一个帖子的循环,然后你可以在移动之前将当前的帖子日期与下一个日期进行比较在。试试这样的...

    <?php 
    
    if ( have_posts() )  :
    
        //Set a variable before the loop so we can test if its the first post
        $first = true;
    
        //Define 2 variables, one to store current date, one for next date
        $current_day = $next_day = null;
    
        //Start the loop
        while ( have_posts() ) : the_post(); 
    
            //Set next date
            $next_day = get_the_date( 'd-m-Y' );
    
            //End the container of the last post on every post except the first
            if( !$first ) {
    
                //If the current post has a different day to the last include a message
                if( $current_day != $next_day ) {
    
                    echo '<span>This is the last post of ' . get_the_date( 'd-m-Y' ) . '</span>';
    
                }
    
                //Close the container of the last post
                echo '<div>';
    
            }
    
            //From now on the post is not the first
            $first = false;
    
            //Now we can set current date
            $current_day = $next_day;
    
        ?>
    
            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
                <!-- include the contents of your post here but don't close the container! -->
    
        <?php endwhile; ?>
    
        <!-- close the container of the last post -->
        </div>
    
    <?php endif; ?>
    

    希望有帮助

    问候

    【讨论】:

    • 非常感谢!它适用于while looper,但不幸的是它不想使用我的对象:(我使用两个文件来显示帖子列表。首先是:`function inner($posts, $td_column_number = '') { //global $post; $buffy = ''; $td_post_count = 0; // 渲染的帖子数量 if (!empty($posts)) { foreach ($posts as $post) { $td_module_10 = new td_module_10($post); $buffy . = $td_module_10->render($post); $td_post_count++; } } return $buffy; }`
    • 第二个:class td_module_10 extends td_module { function __construct($post) { //运行父构造函数 parent::__construct($post); } 函数渲染() { ob_start(); ?> get_title(td_util::get_option('title'));?>
    • 我尝试在第一个文件中设置全局变量,但它不起作用...你能帮我解决这个问题吗?
    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2013-04-11
    相关资源
    最近更新 更多