通常,您会使用pre\\u get\\u posts过滤器进行此操作,但对于您的特定情况,由于您可能希望自定义帖子的显示方式(单独),我建议设置静态首页,并在使用get_posts(但这也意味着你的主题的“博客”模板不会被使用。)
例如:。
add_shortcode(\'custom_news_and_events\',\'my_custom_news_and_events\');
function my_custom_news_and_events() {
$news = get_posts(array(\'category\'=>\'1\',\'posts_per_page\'=>\'7\'));
$childnews1 = get_posts(array(\'category\'=>\'2\',\'posts_per_page\'=>\'3\'));
$childnews2 = get_posts(array(\'category\'=>\'3\',\'posts_per_page\'=>\'3\'));
$events = get_posts(array(\'category\'=>\'4\',\'posts_per_page\'=>\'7\'));
$childevents1 = get_posts(array(\'category\'=>\'5\',\'posts_per_page\'=>\'3\'));
$childevents2 = get_posts(array(\'category\'=>\'6\',\'posts_per_page\'=>\'3\'));
$output = \'<div id="frontpagenews">\';
$output .= \'<h3>News</h3>\';
if (count($childnews1) > 0)) {foreach $news as $post) {
$output .= custom_post_display($post);
} }
$output .= \'<h4>Child News 1</h4>\';
if (count($childnews1) > 0)) {foreach $childnews1 as $post) {
$output .= custom_post_display($post);
} }
$output .= \'<h4>Child News 2</h4>\';
if (count($childnews2) > 0)) {foreach $childnews2 as $post) {
$output .= custom_post_display($post);
} }
$output .= \'</div>\';
$output = \'<div id="frontpageevents">\';
$output .= \'<h3>Events</h3>\';
if (count($events) > 0)) {foreach $events as $post) {
$output .= custom_post_display($post);
} }
$output .= \'<h4>Child Events 1</h4>\';
if (count($childevents1) > 0)) {foreach $childevents1 as $post) {
$output .= custom_post_display($post);
} }
$output .= \'<h4>Child Events 2</h4>\';
if (count($childevents2) > 0)) {foreach $childevents2 as $post) {
$output .= custom_post_display($post);
} }
$output .= \'</div>\';
return $output;
}
(当然,为每个get\\U帖子设置正确的类别ID。)posts\\u per\\u页面设置使您可以更细粒度地控制从每个类别中获取的帖子数量,但您当然可以传递更多参数来获取\\u帖子(请参阅codex页面)
。。。这是在每个显示循环中调用的示例函数:
function custom_post_display($post) {
$display = \'<div class="postitem">\';
$display .= \'<h5><a href=".get_permalink($post->ID).">\';
$display .= $post->post_title.\'</a></h5>\';
$display .= \'<p>\'.$post->post_excerpt.\'</p>\';
$display .= \'</div>\';
return $display;
}
。。。或显示从
WP_Post 对象
现在就这些了,在静态页面上使用快捷代码[自定义新闻和事件]。
然后可以设置#frontpagenews和#frontpageevents div和的样式。职位类别等
如果您不需要子类别副标题,可以删除它们并替换get\\u post调用,只需执行以下操作:
$news = get_posts(array(\'category\'=>\'1,2,3\',\'posts_per_page\'=>\'10\'));
$events = get_posts(array(\'category\'=>\'4,5,6\',\'posts_per_page\'=>\'10\'));
然后将从主类别和子类别中获取最新的10篇文章(因为orderby参数的默认值是date)