Original Message:
I am trying to find a way to limit the amount of categories the [post_categories] shortcode outputs. Is there a way to do this? For example if a post belongs to 10 other categories it current displays every category, is there a way to limit it to only list x amount of categories? if so, how?
This was very new for me and I loved the idea. For this reason I published this tips. Drop the following code in your functions.php or any other helpers file(.php).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
add_shortcode( 'limited_post_categories', 'gd_limited_post_categories' ); function gd_limited_post_categories( $atts ) { global $wp_rewrite; $thelist = ''; $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"'; $defaults = array( 'sep' => ', ', 'before' => __( 'Filed Under: ', 'genesis' ), 'after' => '', 'limit' => 3, 'exclude' => '', ); $atts = shortcode_atts( $defaults, $atts, 'post_categories' ); //* Fetching the categories of current post $categories = get_the_terms( false, 'category' ); if ( ! $categories || is_wp_error( $categories ) ) $categories = array(); $categories = array_values( $categories ); foreach ( array_keys( $categories ) as $key ) { //* excluding some terms from array if( ! empty( $atts['exclude'] ) || $atts['exclude'] != '' ) { $exclude_cats = explode( ',', $atts['exclude'] ); if( in_array( $categories[ $key ]->term_id, (array) $exclude_cats ) ) { unset( $categories[ $key ] ); continue; } } _make_cat_compat( $categories[$key] ); } //* Removing the extra portion $cats = array_splice( $categories, 0, $atts['limit'] ); //* Do nothing if no cats if ( ! $cats ) { return ''; } //* Making the list $i = 0; foreach ( $cats as $category ) { if ( 0 < $i ) $thelist .= $atts['sep']; $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>'; ++$i; } if ( genesis_html5() ) $output = sprintf( '<span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $thelist . $atts['after'] . '</span>'; else $output = '<span class="categories">' . $atts['before'] . $thelist . $atts['after'] . '</span>'; return apply_filters( 'genesis_post_categories_shortcode', $output, $atts ); } |
I created new shortcode [limited_post_catgories] for post info/meta. You will alter default [post_categories] with this new shortcode and get the desired output on your site.
By default new shortcode will show the 3 categories. But you can alter the value by passing the limit parameter into the shortcode e.g. [limited_post_catgories limit=1].
Explaining the steps. First I am retrieving post categories using WordPress functions get_the_terms. I am getting the all categories of current post in array. Then I am removing the unwanted portion from array by PHP function array_splice(). Run a foreach loop using truncated array and generate the categories list.
This code does not seem to be working for me. It displays the shortcode in brackets. Could this be because I am using the term “topics” instead of “categories”?
Can you share the full code via contact form? Your process was correct if you are using taxonomy.
This is great! What dictates which categories are shown? In other words, if a post is assigned to 10 categories and we have the limit above set to ‘3’, how are the 3 displayed categories “selected” or chosen? Automatic based on order applied to the post? Alphabetical?
Thanks!
-B
There have no select option. Automatically 3 categories will show there.
Thanks. But what decides which 3 categories are shown?
Nope. You can’t decide it
This is great! I’m working on modifying it for custom taxonomies. Do you have any suggestions?
What kind of suggestions are you wanting?
This is great, thanks! I wonder, is there a way to *not* have a certain category show up? For instance, I have created one called Featured so I can have it on my front page, but I don’t want that shown, I’d rather just have the *real* categories show, i.e. books, authors.
This is also a great idea. I shall test it one. If possible then I shall modified the code and inform you.
Wow, much appreciated! Thanks!
Snippet is updated now. New parameter “exclude” is added in the shortcode atts array. You will pass the term ID(s) via exclude parameter like exclude=3 or exclude=3,4,10 etc.
Hope that it will help you.
Best
Chinmoy
Is there something like this “exclude” switch that will work for breadcrumbs?
Did not understand your question. This shortcode is for post meta only.