0

我正在尝试在 wordpress 主题中为我的新闻部分创建一个嵌套的帖子存档:

<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>

            </li>

        <?php endforeach;?>

    </ul>

</li>

<?php endforeach; ?>

</div>

如何扩展它以显示每个月的帖子标题?另外,我只想获取“新闻”类别“13”下的帖子。

只是为了说明我需要这种格式:

  • 2013

    • 一月
    • 帖子标题 1
    • 帖子标题 2
    • 帖子标题 3
  • 2012

    • 十二月
    • 帖子标题 1
    • 帖子标题 2
    • 帖子标题 3
    • 十一月
    • 帖子标题 1
    • 帖子标题 2
4

1 回答 1

2

将此代码放在打印月份名称的代码下方。

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 ) {
    echo '<a href="' . get_permalink( $spost ) . "'>" . get_the_title( $spost ) . "</a>";
}
于 2013-01-28T11:20:54.070 回答