I need to create a "Today's Daily News" page for an internal company 'newspaper'. It is run on a wordpress, so the not-so-savvy can handle it. The page just needs to display a tree-ish view of the categories with ONLY TODAY's posts listed in them. If there are no news items in any of the categories for today, then they should not be displayed. I have found some useful bits of code which I can patch together to achieve this, but really I want to utilize as much of wordpress' built in functions as possible. This is the format I am looking for:
Today's Daily News
-Category 1
---News Item Title 1A
---New开发者_JAVA技巧s Item Title 1B
---News Item Title 1C
---News Item Title 1D
-Category 2
---News Item Title 2A
---News Item Title 2B
You can get your custom post query and ordering using either one of the following functions/techniques:
- The query_posts function
- Custom queries using filter methods to sort/order posts
After some research, I figured out how to get this to work. I create a page template called "Today's Post" and you can see the source below:
<?php
/*
Template Name: Today's Posts
*/
?>
<?php get_header(); ?>
<ul>
<?php
$today = getdate();
$categories = get_categories("orderby=name&parent=0");
foreach ($categories as $category) {
query_posts('year='.$today["year"].'&monthnum='.$today["mon"].'&day='.$today["mday"].'&post_type=post&post_status=publish&cat='.$category->term_id);
echo " <li>\n"
." <a href=\"".get_category_link($category->term_id)."\">".$category->name."</a>\n"
." <ul>\n";
while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo " </ul>\n"
." </li>\n";
}
?>
</ul>
<?php get_footer(); ?>
To show child categories, remove "&parent=0" and likewise change it to a category ID if you just want to display that categories children.
精彩评论