下面是用WP帖子创建播客页面的最终代码。以防任何人都能从中受益。
<?php
$categories = get_categories(\'child_of=31\');
foreach ($categories as $category) {
//Display the sub category information using $category values like $category->cat_name
// display category image, if one exists - place image in /images/podcast_images/ dir
$cat_img = \'\';
if(get_bloginfo("url") .\'/wp-content/images/podcast_images/\' . $category->slug . \'.jpg\' != \' \') {$cat_img = \'<img class="podcast_category_image" src="\'.get_bloginfo("url") .\'/wp-content/images/podcast_images/\' . $category->slug . \'.jpg" />\';}
echo \'<h2 class="podcast_h2">\'.$cat_img.$category->name.\'</h2>\';
// start a list for the podcasts
echo \'<ul class="podcast_series">\';
foreach (get_posts(\'orderby=post_date&category=\'.$category->term_id) as $post) {
setup_postdata( $post );
// format date
$my_date = mysql2date(\'F j\\<\\s\\u\\p\\>S\\<\\/\\s\\u\\p\\>, Y\', $post->post_date);
// load the custom fields for this post, if they have content
if(get_post_meta($post->ID, \'Speaker\', true)){
$speaker_name = \'<div class="speaker"><strong>Speaker: </strong>\'. get_post_meta($post->ID, "Speaker", true).\'</div>\';
} else {
$speaker_name = \'\';
}
if(get_post_meta($post->ID, \'Scripture\', true)){
$scripture = \'<div class="scripture"><strong>Scripture: </strong>\'. get_post_meta($post->ID, "Scripture", true).\'</div>\';
} else {
$scripture = \'\';
}
// echo out the results into a list item
echo \'<li><a href="\'.get_permalink($post->ID).\'">\'.get_the_title($post->ID).\'</a>\'. $speaker_name . $scripture.\'<div class="podcast_date"> Recorded On: \'. $my_date .\'</div></li>\';
}
// close the list
echo \'</ul>\';
} ?>
此代码将循环遍历类别31中的所有帖子(在本例中),并显示子类别及其帖子。我首先为播客(第31类)创建了一个类别,并在其中为播客系列创建了子类别。
结果是我们的主要播客cat中的子类别列表。。以及下面列出的每个子类别的帖子(播客):
子cat1播客1播客2播客3
子cat2播客1播客2播客3
谢谢大家的帮助!