好的,下面是我想做的。我想用特定帖子类型的WordPress帖子创建一个“词汇表”。换句话说,每个术语表条目的“标题”和“描述”将从每个帖子中的元值中提取出来。最后,它应该是这样的,其中每个“条目”都是从帖子中生成的:
A
Android: Description
Apple: Description
Aztec: Description
B
Bacon: Description
C
Cat: Description
Category: Description
每个词汇表条目的“标题”和“描述”都是从各自帖子中的元值中提取出来的。到目前为止我掌握的代码:
// A function to display recent glossary posts
function list_glossary_posts() {
global $post;
$args = array( \'posts_per_page\' => -1, \'post_type\' => \'crb_glossary\', \'orderby\'=> \'meta_value\', \'meta_key\' => \'glossary_title\', \'order\' => \'ASC\' );
$postslist = get_posts( $args );
ob_start();
foreach ( $postslist as $post ) :
setup_postdata( $post );
$glossary_title = get_post_custom_values($key = \'glossary_title\');
$glossary_description = get_post_custom_values($key = \'glossary_description\');
$glossary_letter = substr($glossary_title[0], 0, 1);
?>
<h3><?php echo $glossary_letter; ?></h3>
<p><a href="<?php the_permalink(); ?>"><?php echo $glossary_title[0]; ?></a>: <?php echo $glossary_description[0]; ?></p>
<?php
endforeach;
wp_reset_postdata();
$output = ob_get_clean();
return $output;
}
// A shortcode for the glossary
add_shortcode(\'list_glossary_posts\', \'list_glossary_posts\');
这就是我正在努力解决的问题。我希望每个都是独一无二的
$glossary_letter
要显示一次的变量。例如,我不想这样:
A
Android: Description
A
Apple: Description
A
Aztec: Description
B
Bacon: Description
C
Cat: Description
C
Category: Description
救命啊!