gpt4 book ai didi

php - 如何按年和月对 Wordpress 帖子进行分组?

转载 作者:行者123 更新时间:2023-12-04 13:07:04 26 4
gpt4 key购买 nike

我正在尝试创建一个带有后置查询并在输出中具有以下结构的函数:

    2021      January        1.Post Title        2.Post Title      March        3.Post Title    2020      May        4.Post Title

Here is what I've done so far:

global $post;

$posts = get_posts( array(
'post_type' => 'history',
'orderby' => 'date'
) );

$_year_mon = '';
$_has_grp = false;

foreach ( $posts as $post ) {
setup_postdata( $post );

$time = strtotime( $post->post_date );
$year = date( 'Y', $time );
$mon = date( 'F', $time );
$year_mon = "$year-$mon";

if ( $year_mon !== $_year_mon ) {
// Close previous group, if any.
if ( $_has_grp ) {
echo '</div>';
echo '</div>';
}
$_has_grp = true;

echo '<div class="year">';
echo "<span>$year</span>";

echo '<div class="month">';
echo "<span>$mon</span>";
}

// Display post title.
if ( $title = get_the_title() ) {
echo "<div>$title</div>";
} else {
echo "<div>#{$post->ID}</div>";
}

$_year_mon = $year_mon;
}

if ( $_has_grp ) {
echo '</div>';
echo '</div>';
}

wp_reset_postdata();

问题是多个月份的同一年没有分组在一个元素中..

当前输出:

    2021      January        1.Post Title        2.Post Title        2021      March        3.Post Title        2020      May        4.Post Title

最佳答案

The following solution has been fully tested and works seamlessly fine for the default wordpress post type. If you want to run it for your custome post type that has its own custom fields then you may encounter some discrepancies. Therefore, feel free to customize it for your own custom post type as needed.

正如我们在 this question 上讨论的那样,当您使用 get_posts 时,它将返回 post 对象。每个 post 对象 包含以下属性/数据:

WP_Post Object
(
[ID] =>
[post_author] =>
[post_date] =>
[post_date_gmt] =>
[post_content] =>
[post_title] =>
[post_excerpt] =>
[post_status] =>
[comment_status] =>
[ping_status] =>
[post_password] =>
[post_name] =>
[to_ping] =>
[pinged] =>
[post_modified] =>
[post_modified_gmt] =>
[post_content_filtered] =>
[post_parent] =>
[guid] =>
[menu_order] =>
[post_type] =>
[post_mime_type] =>
[comment_count] =>
[filter] =>
)

例如,如果您需要帖子的titlecontentauthorexerpt,那么您可以创建一个多维数组并使用 array_push 函数来创建您的自定义数组,如下所示:

$posts = get_posts(array(
'post_type' => 'history', // Since 'history' is a custom post type, I tested it on 'post'
//'post_type' => 'post'
'posts_per_page' => -1,
'orderby' => 'date'
));

// You could see the number of posts for your query
echo "Number of posts found: ". count($posts) . "<br>";

$your_custom_array_yearly = array();

foreach ($posts as $post) {
setup_postdata($post);
$time = strtotime($post->post_date);
$year = date('Y', $time);
$mon = date('F', $time);

if (!($your_custom_array_yearly[$year][$mon])) {
$your_custom_array_yearly[$year][$mon] = array();
array_push(
$your_custom_array_yearly[$year][$mon],
[
'title' => $post->post_title,
'excerpt' => $post->post_excerpt,
'author' => $post->post_author,
'content' => $post->post_content,
]
);
} else {
array_push(
$your_custom_array_yearly[$year][$mon],
[
'title' => $post->post_title,
'excerpt' => $post->post_excerpt,
'author' => $post->post_author,
'content' => $post->post_content,
]
);
};
}

wp_reset_postdata();

为了调试/调查数组,你可以使用这个:

echo "<pre>";
print_r($your_custom_array_yearly);
echo "</pre>";

现在您有了自己的自定义数组,然后您可以遍历它并输出您的数据:

foreach ((array)$your_custom_array_yearly as $yearly => $yvalue) {
echo $yearly . ": <br>";
echo "<ul>";
foreach ($yvalue as $monthly => $mvalue) {
echo "<li>" . $monthly . ": </li><ul>";
foreach ($mvalue as $monthly_k => $monthly_v) {
foreach ($monthly_v as $post_title_k => $post_title_v) {
echo "<li>" . $post_title_k . ": " . $post_title_v . "</li></ul>";
}
}
}
echo "</ul>";
}

以上代码会输出如下结果:

2021:
January:
1.Title: Post Title
1.Excerpt: Post excerpt
1.Author: Post author
1.Content: Post content
2.Title: Post Title
2.Excerpt: Post excerpt
2.Author: Post author
2.Content: Post content
March:
3.Title: Post Title
3.Excerpt: Post excerpt
3.Author: Post author
3.Content: Post content

2020:
May:
4.Title: Post Title
4.Excerpt: Post excerpt
4.Author: Post author
4.Content: Post content

关于php - 如何按年和月对 Wordpress 帖子进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68910349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com