嗨,我试着显示5篇特定类别的文章标题,标题也显示日期,但它不会显示最近发布的文章,我不得不将最近的文章日期更改为2个月,这样它就会显示出来。。
我使用in\\u category来过滤postdoes in\\u category使用sql查询??
谢谢
<ul id="work_items">
<?php
$max_loop=0;
$month = array(\'\',\'January\', \'Febuary\', \'March\', \'April\', \'May\', \'June\', \'July\',
\'August\', \'September\', \'October\', \'November\', \'December\');
$work_items = $wpdb->get_results("SELECT YEAR( post_date ) AS year, MONTH( post_date )
as month, post_title as title, ID as post_id, COUNT( id ) as post_count FROM $wpdb-
>posts WHERE post_status = \'publish\' and post_type = \'post\' GROUP BY year ORDER BY
post_date DESC");
foreach($work_items as $work_item) {
if(in_category("photography",$work_item->post_id)){
echo "<li>";
echo "<a href=\'#\' onClick=\'title_to_contents(".json_encode($work_item->title).")\'>";
echo $work_item->title;
echo "<span class=\'project_date\'>".$work_item->year." / ".$month[$work_item-
>month]."</span>";
echo "</a>";
echo "</li>";
$max_loop++;
if($max_loop==4){break;}
}
}
?>
</ul>
最合适的回答,由SO网友:David Gard 整理而成
我建议使用WP_Query()
这一节课。希望下面的内容能对您有所帮助-
<?php
$args = array(
\'category_name\' => \'photography\',
\'post_status\' => \'publish\',
\'post_type\' => \'post\',
\'posts_per_page\' => 5
)
$work_items = new WP_Query($args);
if($work_items->have_posts()) : while($work_items->have_posts()) : $work_items->the_post()
echo "<li>";
echo "<a href=\'#\' onClick=\'title_to_contents(".json_encode(get_the_title()).")\'>";
echo get_the_title(); echo "<span class=\'project_date\'>".get_the_time(\'Y\')." / ".get_the_time(\'M\')."</span>";
echo "</a>";
echo "</li>";
endwhile;
endif;
wp_reset_query();
?>
SO网友:Ray Tsai
我仔细研究了一下,我意识到这是我的计数(id),分组请求是对我的所有数据进行分组,我只能访问前几个数据
移除它们后,这也会起作用。。。
<?php
// declare what category you wanna get here
$cat_name= "project";
$max_loop=0;
$month = array(\'\',\'January\', \'Febuary\', \'March\', \'April\', \'May\', \'June\', \'July\', \'August\', \'September\', \'October\', \'November\', \'December\');
$work_items= $wpdb->get_results("SELECT post_title as title, ID as post_id, YEAR(post_date) as y, MONTH(post_date) as month
FROM $wpdb->posts
WHERE post_status = \'publish\' AND post_type = \'post\'
ORDER BY post_date DESC");
foreach($work_items as $work_item) {
if(in_category($cat_name,$work_item->post_id)){
echo "<li>";
echo "<a href=\'#\' onClick=\'title_to_contents(".json_encode($work_item->title).",".json_encode($cat_name).")\'>";
echo $work_item->title;
echo "<span class=\'project_date\'>".$work_item->y." / ".$month[$work_item->month]."</span>";
echo "</a>";
echo "</li>";
$max_loop++;
if($max_loop==4){break;}
}
}
?>