[英]Archive.php filter by custom post type
I have this code in the function.php which I create a list of the months which have created post as follows: 我在function.php中有此代码,它创建了创建帖子的月份列表,如下所示:
2015 Nov Dec 2015年11月12日
2014 Oct Nov Dec 2014十月十一月十二月
" Nov " " dec " are link that shows me the url: www.xxx.com/2015/11 post that date. “ Nov”“ dec”是显示我网址的链接:www.xxx.com/2015/11发布该日期。
This using archive.php 这个使用archive.php
The question is : how could do this yourself but by filtering these custom post type link ?? 问题是:如何自己完成此任务,但通过过滤这些自定义帖子类型链接?
function twentyeleven_get_archives_callback($item, $index, $currYear) {
global $wp_locale;
if ( $item['year'] == $currYear ) {
$url = get_month_link( $item['year'], $item['month'] );
// translators: 1: month name, 2: 4-digit year
$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($item['month']), $item['year']);
$text = substr($text,0,3);
echo get_archives_link($url, $text);
}
}
function twentyeleven_get_archives() {
global $wpdb;
$query = "SELECT YEAR(post_date) AS `year` FROM $wpdb->posts WHERE `post_type` = 'post' AND `post_status` = 'publish' GROUP BY `year` ORDER BY `year` DESC limit 4";
$arcresults = $wpdb->get_results($query);
$years = array();
if ($arcresults) {
foreach ( (array)$arcresults as $arcresult ) {
array_push($years, $arcresult->year);
}
}
$query = "SELECT YEAR(post_date) as `year`, MONTH(post_date) as `month` FROM $wpdb->posts WHERE `post_type` = 'post' AND `post_status` = 'publish' GROUP BY `year`, `month` ORDER BY `year` DESC, `month` ASC";
$arcresults = $wpdb->get_results($query, ARRAY_A);
$months = array();
if ( $arcresults ) {
foreach ($years as $year) {
//My Display
//echo "\t<li>\n\t\t<a href=\"#\">$year</a>\n\t\t<ul>\n";
//array_walk($arcresults, "twentyeleven_get_archives_callback", $year);
//echo "\t\t</ul>\n\t</li>\n";
//Your Display
echo "\t<div class='listYearArchive'><span class='yearArchive'>$year</span>\n\t<ul class='listMonthArchive'>\n";
array_walk($arcresults, "twentyeleven_get_archives_callback", $year);
echo "\t</ul></div>\n";
}
}
}
You can change the post_type right in the query 您可以在查询中更改post_type
FROM $wpdb->posts WHERE `post_type` = 'post'
Just change post
for your custom post name. 只需将
post
更改为您的自定义帖子名称即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.