如何使用短码调用自定义分类标准类别

时间:2011-05-12 作者:maikunari

我刚刚在这里解决了一个问题:How to display posts from a single category within a custom taxonomy

现在我想知道如何使用此代码:

<?php
    $args = array(
        \'posts_per_page\' => 1,
        \'post_type\' => \'inventory\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'inventory-category\',
                \'field\' => \'slug\',
                \'terms\' => array( 
                    \'bulk-racks\' 
                )
            )
        )       
    );
query_posts( $args ); while ( have_posts() ): the_post();

// do stuff here
?>

<?php endwhile; ?>
并使用短代码调用它,例如,通过使用此代码或类似的代码来显示bulk racks类别[库存类别=“bulk racks”]

我知道如何创建一个基本的短代码,但我不知道如何编写根据短代码类别名称输入返回帖子的函数。

再次感谢您的帮助。

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

看看category post shortcode 下面是一个插件,稍作修改即可调用您的帖子类型和分类法:

// Taxonomy category shortcode
function cat_func($atts) {
    extract(shortcode_atts(array(
            \'class_name\'    => \'cat-post\',
            \'totalposts\'    => \'-1\',
            \'category\'      => \'\',
            \'thumbnail\'     => \'false\',
            \'excerpt\'       => \'true\',
            \'orderby\'       => \'post_date\'
            ), $atts));

    $output = \'<div class="\'.$class_name.\'">\';
    global $post;
    $args = array(
        \'posts_per_page\' => $totalposts, 
        \'orderby\' => $orderby,
        \'post_type\' => \'inventory\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'inventory-category\',
                \'field\' => \'slug\',
                \'terms\' => array( $category)
            )
        ));
    $myposts = NEW WP_Query($args);


    while($myposts->have_posts()) {
        $myposts->the_post();
        $output .= \'<div class="cat-post-list">\';
        if($thumbnail == \'true\') {
        $output .= \'<div class="cat-post-images">\'.get_the_post_thumbnail($post->ID, \'thumbnail\').\'</div>\';
        }
        $output .= \'<div class="cat-content"><span class="cat-post-title"><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></span>\';
        if ($excerpt == \'true\') {
            $output .= \'<span class="cat-post-excerpt">\'.get_the_excerpt().\'</span>\';
        }
        $output .= \'</div>
            <div class="cat-clear"></div>
        </div>\';
    };
    $output .= \'</div>\';
    wp_reset_query();
    return $output;
}
add_shortcode(\'inventory-category\', \'cat_func\');
usage:

只需在你的帖子或页面中添加此短代码

[inventory-category totalposts="3" category="bulk-racks" thumbnail="true" excerpt="true" ]
totalposts—要显示的帖子总数。默认值为-1 category-category slug。对于多个Slug缩略图,请使用逗号-如果要显示缩略图,请设置为true。默认值为false
  • 摘录-如果要显示摘录,请将其设置为true。默认值为true-您的帖子将按排序。默认post\\u日期。检查http://codex.wordpress.org/Template_Tags/get_posts 有关详细信息

  • 结束

    相关推荐

    Nested Shortcode Detection

    如果您熟悉此代码<?php $pattern = get_shortcode_regex(); preg_match(\'/\'.$pattern.\'/s\', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == \'YOURSHORTCODE\') { //shortcode is being used }&#