嵌套年份/月份/帖子标题存档代码困难

时间:2013-02-28 19:46:11

标签: php wordpress

我正在尝试使用此问题(和答案)中的代码来创建嵌套的年/月/帖子标题存档菜单:Nested Year/Month/Post title archive

不幸的是,代码无效(显示年份和月份,但在帖子标题下没有显示)。知道我可能做错了吗?

<div class="blog-list-archive">

<?php
/**/
$years = $wpdb->get_col( "SELECT DISTINCT YEAR(post_date)
FROM $wpdb->posts WHERE post_status = 'publish'
AND post_type = 'post' ORDER BY post_date DESC");
foreach($years as $year) :
?>
<li><a href="JavaScript:void()"><?php echo $year; ?></a>

    <ul class="archive-sub-menu">
        <?    $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date)
        FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'
        AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
        foreach($months as $month) :
        ?>
            <li><a href="<?php echo get_month_link($year, $month); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>

                <ul class="archive-sub-menu">

                    <?  global $wpdb; $sposts = $wpdb->get_col( "SELECT ID 
                        FROM $wpdb->posts WHERE MONTH(post_date) = '$month'
                        AND YEAR(post_date) =  '$year' AND post_status = 'publish'
                        AND 'post_type' = 'post' ORDER BY post_date DESC" );
                        foreach( $sposts as $spost ) {
                    ?>
                        <li><a href="<?php echo get_permalink( $spost ); ?>"><?php echo get_the_title( $spost ); ?></a></li>

                        <?php } endforeach; ?>
                </ul>

            </li>

        <?php endforeach;?>

    </ul>

</li>

<?php endforeach; ?>

1 个答案:

答案 0 :(得分:1)

这个循环:

foreach( $sposts as $spost ) {
?>
    <li><a href="<?php echo get_permalink( $spost ); ?>"><?php echo get_the_title( $spost ); ?></a></li>

<?php } endforeach; ?>

对我来说很奇怪。如果使用花括号,则不需要endforeach。如果您想使用endforeach来保持一致性,请使用冒号而不是大括号,就像您为其他循环所做的那样。