我在网格显示的主题中使用短代码。代码仅考虑类别ID。我还想添加自定义分类法,以便将输出作为分类帖子和自定义分类帖子。
function five_col( $atts ) {
extract( shortcode_atts( array(
\'cats\' => \'1\',
\'num\' => \'2\',
\'offset\' => \'0\',
), $atts ) );
$sq = new WP_Query(array(\'posts_per_page\' => $num, \'post_status\' => \'publish\', \'order\' => \'desc\', \'ignore_sticky_posts\' => 1, \'cat\' => $cats, \'offset\' => $offset));
if ($sq->have_posts()) :
$count = 1;
$col = 5;
$out = \'<ul class="three_col">\';
while ($sq->have_posts()) : $sq->the_post();
$thumbClass = ($count == $col) ? \'last\' : \'\';
$permalink = get_permalink();
$title = get_the_title();
$bloginfo = get_template_directory_uri();
if ( has_post_thumbnail()) {
$img_src = wp_get_attachment_image_src( get_post_thumbnail_id($GLOBALS[\'post\']->ID), \'\');
$thumbnail = $img_src[0];
}
else $thumbnail = \'\';
$default_thumb = $bloginfo.\'/images/post_thumb.jpg\';
$thumbnail = ( $thumbnail == \'\' ) ? $default_thumb : $thumbnail;
$format = \'<li class="\'.$thumbClass.\'"><a href="%3$s" title="%4$s"><div class="img_thumb"><img src="%1$s/scripts/timthumb.php?src=%2$s&w=70&h=130&zc=1&q=100" alt="%4$s"/></div><h5><a href="%3$s" rel="bookmark" title="\' . __( \'Permanent Link to %4$s\', \'volt\' ) . \'">%4$s</a></h5></li></a>\';
$out .= sprintf ($format, $bloginfo, $thumbnail, $permalink, $title );
$count++;
if($count > $col){
$col = $col + 5;
$out .= \'<li class="clear"></li>\';
}
endwhile;
$out .= \'</ul>\';
return $out;
endif;
wp_reset_postdata(); // Restore global post data stomped by the_post().
}
SO网友:Diana
不确定是否是这样,但您可以在查询中添加分类法:
new WP_Query(array(\'posts_per_page\' => $num, \'post_status\' => \'publish\',\'taxonomy\' => $taxonomy));
您可以使用按术语筛选
$taxonomy => $term
:
$taxonomy = \'seasons\';
$term = \'spring\'
/* $terms = array(\'summer\',\'winter\') an array */
new WP_Query(array(\'posts_per_page\' => $num, \'post_status\' => \'publish\',$taxonomy => $term));
更多关于
WP_QueryNote: <分类学和术语不是一回事:)