function Service_shortcode($atts , $content = null){
extract(shortcode_atts(array(
'title'=>'',
),$atts
));
$q = new WP_Query(
array('posts_per_page'=>3, 'post_type'=>'services')
);
$list = '
<h1>'.$title.'</h1>
<ul>
';
while ($q->have_posts()) : $q->the_post();
$list.='
<li>
<h3>'.get_the_title ().'</h3>
<h6>'.the_category(',').'</h6>
<p>'.get_the_content().'</p>
</li>
';
endwhile;
$list.='</ul>';
wp_reset_query();
return $list;
}
add_shortcode('service','Service_shortcode');
在主题中使用the_category(&#39;,&#39;)时,它会显示类别名称但在短代码中使用它时不起作用。哪个代码用于通过短代码显示类别名称。
答案 0 :(得分:1)
试试这段代码:
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "Read more posts from : %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></li>';
}
$second_output = trim($output);
}
$return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );