Php 按月将wordpress帖子分组
我想在一个页面上显示所有Wordpress帖子,结果如下所示: 9月份员额(本月) 1-第一个职位 2秒职位 3-第三个员额 下个月的职位 2-第一个职位 2秒职位 3-第三个员额 我已经完成了这个结果,以显示按月份排序的所有帖子,但我无法对它们进行分组,并将每个月的帖子显示为一个组:Php 按月将wordpress帖子分组,php,wordpress,Php,Wordpress,我想在一个页面上显示所有Wordpress帖子,结果如下所示: 9月份员额(本月) 1-第一个职位 2秒职位 3-第三个员额 下个月的职位 2-第一个职位 2秒职位 3-第三个员额 我已经完成了这个结果,以显示按月份排序的所有帖子,但我无法对它们进行分组,并将每个月的帖子显示为一个组: <?php $month = date('M'); $arrgs = array('orderby' => $month); $myposts = get_posts($arrgs); foreac
<?php
$month = date('M');
$arrgs = array('orderby' => $month);
$myposts = get_posts($arrgs);
foreach($myposts as $post) :
setup_postdata($post);
?>
<div class="post-item">
<div class="post-info">
<h2 class="post-title">
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<p class="post-meta">Posted by <?php the_author(); ?> Published in <strong><?php echo $month; ?></strong></p>
</div>
</div>
<?php endforeach; wp_reset_postdata(); ?>
由发布于
因此,我不确定是否还有其他方法,但我不得不直接查询数据库以获得您想要的内容。这个例子在很多类似的场合都适用于我
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<ul>
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date ASC");
foreach($years as $year) : ?>
<h4><li><a href="<?php echo get_year_link($year ); ?> "><?php echo $year; ?></a>
<ul>
<?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");
foreach($months as $month) : ?>
<h4>
<li>
<a href="<?php echo get_month_link($year, $month); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
<ul>
<?php $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");
foreach ($theids as $theid): ?>
<h4 style= font-style:italic;><li><a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>"><?php echo $theid->post_title; ?></a></li></h4>
<?php endforeach; ?>
</ul>
</li></h4>
<?php endforeach;?>
</ul>
</li></h4>
<?php endforeach; ?>
</ul>
</div>
</div>
@zillaofthegoods以下是我的解决方案:我只需要使用一个查询来获取我想要显示的所有帖子,然后使用PHP来完成繁重的工作。一个好主意是要求将年和月作为额外的列,因为我们稍后将需要这些列
global $wpdb; // Don't forget
$collection = $wpdb->get_results("
SELECT YEAR(p.post_date) AS post_year, MONTH(p.post_date) AS post_month, p.*
FROM {$wpdb->posts} AS p
WHERE p.post_type = 'post' AND p.post_status = 'publish'
ORDER BY p.post_date DESC
", OBJECT );
获取结果并使用适当的条件设置一系列循环,以获得符合所需条件的数据。我没有尝试过这个,但我认为它解释了我的观点:
// Loop once to grab the years
foreach ( $collection as $year ):
// Loop for a second time to grab the months inside a year
foreach ( $collection as $month ):
// Continue unless years match
if ( $month->post_year != $year ) continue;
// Loop one more time to grab the posts inside the month, inside the year
foreach ( $collection as $post ):
// Continue unless months and years match
if ( $post->post_year != $year || $post->post_month != $month ) continue;
// You can use the posts data here
endforeach;
endforeach;
endforeach;
有什么好处?您只需点击一次数据库,其他所有操作都由PHP完成,这是模板呈现的一部分,可以轻松缓存。在循环中使用多个查询的向下投票。“那就错了!”我引述道,你想提供一个更充分的解决方案吗?投票选择更有效的解决方案。谢谢你接受我的请求!