功能介绍:利用时间做一个列表,比如2024年3月(链接为:https://域名/2024-02)显示2024年2月份的所有文章,2024年3月(链接为:https://域名/2024-03)显示2024年3月份的所有文章…依此类推!
1、新增一个archive-custom.php模板,参考代码如下:
<?php
// 从URL中获取年份和月份
$year_month = get_query_var('name');
if ($year_month) {
list($year, $month) = explode('-', $year_month);
}
// 确保$year和$month是数字,并且$month在1到12之间
if (is_numeric($year) && is_numeric($month) && $month >= 1 && $month <= 12) {
// 创建WP_Query来查询文章
$args = array(
'year' => $year,
'monthnum' => $month,
'posts_per_page' => -1, // 显示所有文章
);
$query = new WP_Query($args);
// 检查是否有文章
if ($query->have_posts()) {
// 开始文章循环
while ($query->have_posts()) {
$query->the_post();
echo '<dl><dt>' . the_post_thumbnail() . '</dt>';
// 显示文章标题和链接
echo '<dd class="title"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></dd>';
// 显示文章内容摘要
echo '<dd class="desc">' . get_the_excerpt() . '</dd>';
// 显示标签
$tags = get_the_tags();
if ($tags) {
$tag_links = array();
foreach ($tags as $tag) {
$tag_link = get_tag_link($tag->term_id);
$tag_links[] = '<a href="' . $tag_link . '">' . $tag->name . '</a>';
}
echo '<dd class="tags">' . implode('', $tag_links) . '</dd></dl>';
}
// 显示文章内容(可选)
//the_content();
// 其他文章信息,比如发布日期、特色图片等(可选)
// the_date();
// the_post_thumbnail();
// 可以在这里添加更多自定义的HTML和模板标签
}
// 分页导航(如果设置了分页的话)
//the_posts_navigation();
} else {
// 如果没有文章,显示消息
echo '没有找到该月份的文章。';
}
// 恢复原始的文章数据
wp_reset_postdata();
} else {
// URL不匹配,显示错误消息
echo '无效的URL。';
}
?>
2、找到主题文件夹中的functions.php文件中,添加以下代码:
function custom_archive_rewrite_rules() {
add_rewrite_rule(
'^([0-9]{4})-([0-9]{1,2})/?$',
'index.php?name=$matches[1]-$matches[2]',
'top'
);
}
add_action('init', 'custom_archive_rewrite_rules');
function custom_archive_query_vars($query_vars) {
$query_vars[] = 'name';
return $query_vars;
}
add_filter('query_vars', 'custom_archive_query_vars');
3、打开wordpress后台管理页面,左边菜单选择“页面”→“新增页面”,标题改成2024/02,页面链接自动生成成2024-02,页面模板选择archive-custom新增就可以了!
php语法对index.php首页进行判断(根据需要显示不同页面)
wordpress新建页面中页面属性里没有“模板”选项解决方法
wordpress获取当前文章最后一个tags标签(不带链接)
上面是“wordpress做时间列表,比如2024年1月文章列表”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_15669.html
workflows工作流
一桌精致的美食,桌上几杯白葡萄酒
1个可爱的白色短发女孩
一个戴着破旧莎帽子的机械忍者站在冰林中
金色玫瑰花comfyui工作流
一辆机器人虎狮高端混合动力车ComfyUI工作流
一只巨大的极其精细的鞋子ComfyUI工作流
一只竹节虫ComfyUI工作流
一个超凡脱俗的女明星的特写镜头ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

利用css3做一个动态loading效果
iframe开发admin后台
3d文字360度旋转
制作一个好玩的倒计时
barcode条形码/qrcode二维码兼容所有浏览器(含ie6/ie7/ie8)
利用CSS3做一个星级评分样式
利用CSS3代码编写45款按钮效果
3D立体人物效果











