对于最近的一个项目,我必须这样做。
以下是我用来收集自定义帖子类型类别的代码:
$profession_terms = get_terms( \'professions\', array(
\'orderby\' => \'count\',
\'order\' => \'DESC\',
\'hide_empty\' => 0
) );
foreach( $profession_terms as $term ) {
$term_link = get_term_link( $term );
... do other stuff
}
假设John是一个标记,变量$term\\u link将获取该链接,以便您可以将其放入代码中。
我还有自定义的post类型标记,这是我用来检索这些标记的代码:
echo \'<div class="states">\';
$argsState = array(
\'smallest\' => 16,
\'largest\' => 16,
\'unit\' => \'px\',
\'number\' => 50,
\'format\' => \'flat\',
\'separator\' => "·",
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'exclude\' => null,
\'include\' => null,
\'topic_count_text_callback\' => default_topic_count_text,
\'link\' => \'view\',
\'taxonomy\' => \'state\',
\'echo\' => true,
\'child_of\' => null, // see Note!
);
echo \'<h4 class="widget-title">Peruse by State</h4>\';
wp_tag_cloud( $argsState );
echo \'</div>\';
当然,您必须修改此代码以符合自定义帖子类型的标记和类别。
以下是该模板的所有代码:
<?php
/* Template Name: Stories List
* Description: Displays Professions with list of stories and link to custom taxonomy archive
* Author: Marj Wyatt aka Virtually Marj
*
* Schema http://schema.org/ItemList
*/
if ( !is_user_logged_in() ) {
wp_redirect( home_url(\'/oops-this-content-is-members-only\') );
exit;
}
remove_action (\'genesis_loop\', \'genesis_do_loop\'); // Remove the standard loop
add_action( \'genesis_loop\', \'vm_stories_archive\' ); // Add custom loop
function vm_stories_archive() {
echo \'<div class="cities">\';
$argsCity = array(
\'smallest\' => 16,
\'largest\' => 16,
\'unit\' => \'px\',
\'number\' => 50,
\'format\' => \'flat\',
\'separator\' => "·",
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'exclude\' => null,
\'include\' => null,
\'topic_count_text_callback\' => default_topic_count_text,
\'link\' => \'view\',
\'taxonomy\' => \'city\',
\'echo\' => true,
\'child_of\' => null, // see Note!
);
echo \'<h4 class="widget-title">Peruse by City</h4>\';
wp_tag_cloud( $argsCity );
echo \'</div>\';
echo \'<div class="states">\';
$argsState = array(
\'smallest\' => 16,
\'largest\' => 16,
\'unit\' => \'px\',
\'number\' => 50,
\'format\' => \'flat\',
\'separator\' => "·",
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'exclude\' => null,
\'include\' => null,
\'topic_count_text_callback\' => default_topic_count_text,
\'link\' => \'view\',
\'taxonomy\' => \'state\',
\'echo\' => true,
\'child_of\' => null, // see Note!
);
echo \'<h4 class="widget-title">Peruse by State</h4>\';
wp_tag_cloud( $argsState );
echo \'</div>\';
echo \'<p> </p>\';
echo \'<div class="story-content" itemscope="itemscope" itemtype="http://schema.org/ItemList">\';
$profession_terms = get_terms( \'professions\', array(
\'orderby\' => \'count\',
\'order\' => \'DESC\',
\'hide_empty\' => 0
) );
foreach( $profession_terms as $term ) {
$count = $term->count;
$term_link = get_term_link( $term );
$blogurl = get_bloginfo(\'url\');
// Define the query
$args1 = array(
\'posts_per_page\' => -1,
\'post_type\' => \'shows\',
\'professions\' => $term->slug
);
$myQuery = new WP_Query( $args1 );
if ( $count == 1 ) {
$label = \'Story\'; }
if ( ( $count > 1 ) || ( $count == 0 ) ) {
$label = \'Stories\';
}
echo \'<h2 itemprop="name"><a href="\' . $term_link .\'">\' . $term->name . \'</a><span class="entry-meta"> (\' . $count . \' \' . $label . \')</span></h2>\';
echo \'<meta itemprop="itemListOrder" content="Unordered" />\';
if ( $count == 0 ) {
echo \'<p>No stories found.</p>\';
echo \'<p class="postlist-entry-meta">Be the first to <a href="\' . $blogurl . \'/add-your-story/">add your <b>\' . $term->name . \'</b> story</a>!</p>\';
}
while ( $myQuery->have_posts() ) : $myQuery->the_post();
$postID = get_the_ID();
$postDate = get_the_date(\'\', $post->ID);
$postTerms = wp_get_post_terms($postID, \'city\', array("fields" => "all"));
$postCity = $postTerms[0]->slug;
$postCityName = $postTerms[0]->name;
$cityLinkConstruct = $blogurl . \'/cities/\' . $postCity;
$postCityLink = \'<a href="\' . $cityLinkConstruct . \'">\' . $postCityName . \'</a>\';
$postStateTerms = wp_get_post_terms($postID, \'state\', array("fields" => "all"));
$postState = $postStateTerms[0]->slug;
$postStateName = $postStateTerms[0]->name;
$stateLinkConstruct = $blogurl . \'/states/\' . $postState;
$postStateLink = \'<a href="\' . $stateLinkConstruct . \'">\' . $postStateName . \'</a>\';
echo \'<ul class="ckicon">\';
echo \'<hr>\';
echo \'<li itemprop="itemListElement">\';
echo \'<span itemprop="url"><a href="\' . get_post_permalink($postID) .\'" rel="bookmark" title="\' . get_the_title($postID) . \'">\' . get_the_title($postID) .\'</a></span><br>\';
echo \'<span class="entry-meta">Dateline: <span itemprop="datePublished"> \' . $postDate . \'</span> <span class="middot-large">·</span> \' . $postCityLink . \', \' . $postStateLink . \'</span>\';
//echo \'<hr>\';
echo ( wpautop (the_excerpt()) );
echo \'</li>\';
echo \'</ul>\';
endwhile;
if ( $count <> 0 ) {
echo \'<p class="postlist-entry-meta">View all Stories in <a href="\' . $term_link .\'">\' . $term->name . \'</a>. <a href="\' . $blogurl . \'/add-your-story/">Share your <b>\' . $term->name . \'</b> story</a>!\'; }
wp_reset_postdata();
}
echo \'</div><!-- end itemscope -->\';
}
genesis();
我应该提醒大家,在一个有很多帖子的网站上,上面的模板可能在错误的托管服务上处理得很慢。
希望这有帮助。