Custom taxonomy in short code

时间:2013-03-10 作者:Allen

我在网格显示的主题中使用短代码。代码仅考虑类别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&amp;w=70&amp;h=130&amp;zc=1&amp;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().
}

1 个回复
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_Query

Note: <分类学和术语不是一回事:)

结束

相关推荐

SHORTCODE_ATTS-一个URL变量不起作用

我开发了一个基于短代码的小插件。下面是一个简化版的代码,用于短代码本身:function lishortcode($liatts) { extract(shortcode_atts(array( \"li\" => get_option(\'li\'), ), $liatts)); foreach ($liatts as $key => $option) { $li[$key] =