WordPress 帖子按类别和年份排序

发布于 2024-09-18 09:01:27 字数 1464 浏览 2 评论 0原文

我已经在谷歌上进行了很长时间的搜索,试图找到这个问题的解决方案...我正在使用 WordPress 安装来创建一个简历管理器主题来控制内容。我已经成功地按类别组织了所有 WP 帖子,但也想按年份分组列出这些帖子。目前我有这个:

<?php // Categories
         $cvm_cat_args = array('orderby' => 'name', 'order' => 'ASC' ); 
         $categories = get_categories($cvm_cat_args); ?>

<?php foreach($categories as $category) : ?>

<?php // Posts in category
         $cvm_post_args = array( 'showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1, 'order' => 'ASC' );
         $posts = get_posts($cvm_post_args); ?>

<?php if ($posts) : ?>

<h1><?php echo $category->name ?></h1>

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

<h3><?php the_title();  ?></h3>
<small><?php the_time(); ?></small>
<?php the_excerpt() ?>

<?php endforeach; ?>

<?php endif; ?>

<?php endforeach; ?>

我坚持如何在类别中按年份顺序显示帖子,以便输出如下:

<h1>Category Name 1</h1>

<h2>2010</h1>

<h3>Post name 1</h3>
<p>Excerpt...</p>

<h3>Post name 2</h3>
<p>Excerpt...</p>


<h2>2009</h1>

<h3>Post name 3</h3>
<p>Excerpt...</p>

<h3>Post name 4</h3>
<p>Excerpt...</p>

<h1>Category Name 2</h2>
etc

任何帮助将不胜感激。

谢谢!

I've been on a long google search trying to find the solution to this... I am creating a cv manager theme using a WordPress install to control content. I have managed to organise all WP posts in categories but would also like to list those posts in year groupings. Currently I have this:

<?php // Categories
         $cvm_cat_args = array('orderby' => 'name', 'order' => 'ASC' ); 
         $categories = get_categories($cvm_cat_args); ?>

<?php foreach($categories as $category) : ?>

<?php // Posts in category
         $cvm_post_args = array( 'showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1, 'order' => 'ASC' );
         $posts = get_posts($cvm_post_args); ?>

<?php if ($posts) : ?>

<h1><?php echo $category->name ?></h1>

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

<h3><?php the_title();  ?></h3>
<small><?php the_time(); ?></small>
<?php the_excerpt() ?>

<?php endforeach; ?>

<?php endif; ?>

<?php endforeach; ?>

I'm stuck on how to display the posts in year order within the category so that the output would be something like this:

<h1>Category Name 1</h1>

<h2>2010</h1>

<h3>Post name 1</h3>
<p>Excerpt...</p>

<h3>Post name 2</h3>
<p>Excerpt...</p>


<h2>2009</h1>

<h3>Post name 3</h3>
<p>Excerpt...</p>

<h3>Post name 4</h3>
<p>Excerpt...</p>

<h1>Category Name 2</h2>
etc

Any help would be mightily appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-09-25 09:01:27
<?php $year = ''; ?>
<?php foreach($posts as $post) : ?>
<?php setup_postdata($post); ?>
<?php if (get_the_time('Y') != $year): ?>
<?php $year = get_the_time('Y'); ?>
<h2><?php echo $year; ?></h2>
<?php endif; ?>

<h3>Post name 1</h3>
<p>Excerpt...</p>

<h3>Post name 2</h3>
<p>Excerpt...</p>

<?php endforeach; ?>
<?php $year = ''; ?>
<?php foreach($posts as $post) : ?>
<?php setup_postdata($post); ?>
<?php if (get_the_time('Y') != $year): ?>
<?php $year = get_the_time('Y'); ?>
<h2><?php echo $year; ?></h2>
<?php endif; ?>

<h3>Post name 1</h3>
<p>Excerpt...</p>

<h3>Post name 2</h3>
<p>Excerpt...</p>

<?php endforeach; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文