How to call custom taxonomy categories with shortcodes

I've just had one questions solved over here: How to display posts from a single category within a custom taxonomyN

I've just had one questions solved over here: How to display posts from a single category within a custom taxonomy

Now I'm wondering how to take this code:

<?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; ?>

and call it using shortcodes, for example to display the bulk-racks category by using this or something similar [inventory-category="bulk-racks"]

I know how to create a basic shortcode but I don't know how to write the function that would return the posts based on the shortcode category name input.

Once again any help would be greatly appreciated.

I've just had one questions solved over here: How to display posts from a single category within a custom taxonomy

Now I'm wondering how to take this code:

<?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; ?>

and call it using shortcodes, for example to display the bulk-racks category by using this or something similar [inventory-category="bulk-racks"]

I know how to create a basic shortcode but I don't know how to write the function that would return the posts based on the shortcode category name input.

Once again any help would be greatly appreciated.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked May 12, 2011 at 4:10 maikunarimaikunari 1652 gold badges7 silver badges14 bronze badges 2
  • Indeed, marking solutions is a matter of respecting the invested time into your task by other users. – kaiser Commented May 12, 2011 at 11:04
  • Thanks guys, I have accepted first answer, my fault for not reading the faqs properly. Still kinda mean to vote down my valid question though as it may help others. – maikunari Commented May 12, 2011 at 13:34
Add a comment  | 

1 Answer 1

Reset to default 5

Take a look at category post shortcode to get an idea and here is the plugin with minor modifications to call your post type and taxonomy:

// 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:

just put this shortcode in your post or pages

[inventory-category totalposts="3" category="bulk-racks" thumbnail="true" excerpt="true" ]
  • totalposts - your total number of post to display. default is -1
  • category - category slug. use comma , for multiple slugs
  • thumbnail - set true if you want to display thumbnail. default is false
  • excerpt - set true if you want to display excertp. default is true
  • orderby - your post will order by . default post_date . check http://codex.wordpress/Template_Tags/get_posts for detail

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745141494a4613453.html

相关推荐

  • How to call custom taxonomy categories with shortcodes

    I've just had one questions solved over here: How to display posts from a single category within a custom taxonomyN

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信