按月和年自定义归档页面-嵌套问题

时间:2015-09-26 作者:Gakuranman

我试图在wordpress中添加一个自定义档案页面,按年份和月份输出完整的档案列表。下面的代码或多或少可以工作,但html验证器输出错误,表示它们是未关闭的ul元素。

尽管尝试了许多变体,并花了几个小时来处理代码,但我无法理解为什么ul和li嵌套有问题。

你知道我哪里做错了吗?(该代码用于wordpress的Genesis框架)。

add_action( \'genesis_entry_content\', \'custom_page_archive_content\' );
function custom_page_archive_content() {

    global $post;
    echo \'<ul class="archives">\';
        $lastposts = get_posts(\'numberposts=-1\');
        $year = \'\';
        $month = \'\';
        foreach($lastposts as $post) :
            setup_postdata($post);

            if(ucfirst(get_the_time(\'F\')) != $month && $month != \'\'){
                echo \'</ul></li><!-- /monthly-archives -->\';
            }
            if(get_the_time(\'Y\') != $year && $year != \'\'){
                echo \'</ul></li>\';
            }
            if(get_the_time(\'Y\') != $year){
                $year = get_the_time(\'Y\');
                echo \'<li><h2>\' . $year . \'</h2><ul class="monthly-archives">\';
            }
            if(ucfirst(get_the_time(\'F\')) != $month){
                $month = ucfirst(get_the_time(\'F\'));
                echo \'<li><h3>\' . $month . \'</h3><ul>\';
            }
        ?>
            <li>
                <span class="the_date"><?php the_time(\'d\') ?>:</span>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span class="comments-number"><?php comments_number(\'(0)\',\'(1)\',\'(%)\'); ?></span>
            </li>
        <?php endforeach; ?>
        </ul><!-- /archives -->
        <?php
}

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

在做了更多的工作之后,我开发了一个解决方案,可以按年、按月、按日输出档案,同时最大限度地减少数据库调用。在此处发布供其他人使用:

//* Add custom archives below entry content
add_action( \'genesis_entry_content\', \'custom_archives_page_content\' );
function custom_archives_page_content() {

global $post;

// set post arguments
$args = array(
    \'posts_per_page\'    => -1,
    \'orderby\'       => \'post_date\',
    \'order\'         => \'DESC\',
    \'post_type\'     => \'post\',
    \'post_status\'       => \'publish\',
);

// get posts from wordpress
$posts = get_posts($args);

// setup an associative array with three levels, for every year, month and posts
$years = array();

foreach ($posts as $post) {

    $year  = mysql2date(\'Y\', $post->post_date);
    $month = mysql2date(\'F\', $post->post_date);

    // specify the position of the current post
    $years[$year][$month][] = $post;
}

// begin the archives ?>

<ul class="yearly-archives">

<?php foreach ($years as $year => $months) : // shows the years ?>

    <li><h2><?php echo $year ?></h2><ul class="monthly-archives">

    <?php foreach ($months as $month => $posts ) : // shows the months ?>

        <li><h3><?php printf("%s (%d)", $month, count($months[$month])) ?></h3><ul class="daily-archives">

        <?php foreach($posts as $post) : setup_postdata($post); // shows the posts ?>

            <li>
            <span class="date"><?php the_time(\'d\') ?>:</span>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <span class="comments-number"><?php comments_number(\'(0)\',\'(1)\',\'(%)\'); ?></span>
            </li>

        <?php endforeach; // ends foreach for $posts ?>
        </ul><!-- /daily-archives --></li>

    <?php endforeach; // ends foreach for $months ?>
    </ul><!-- /monthly-archives --></li> 

<?php endforeach; // ends foreach for $years ?>
</ul><!-- /yearly-archives -->
<?php
}
道具到davidmhmikos.

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请