- This topic has 3 replies, 2 voices, and was last updated 3 years, 1 month ago by David.
-
AuthorPosts
-
February 26, 2021 at 10:48 am #1674143Marius
This is the function from @Tom to move categories and tags above the title of the post.
add_action( 'generate_before_content', 'tu_add_meta_below_title' ); function tu_add_meta_below_title() { if ( is_single() ) { $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) ); $categories_list = sprintf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>', _x( 'Categories', 'Used before category names.', 'generatepress' ), $categories_list ); $tags_list = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) ); $tags_list = sprintf( '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>', esc_html_x( 'Tags', 'Used before tag names.', 'generatepress' ), $tags_list ); ?> <div class="entry-meta"> <?php echo $categories_list; ?> | <?php echo $tags_list; ?> </div> <?php } }
How can we limit the number of categories to 1 and the number of tags to 2 or 3? Very important, when limiting the number of cats and tags, how can we randomize it? All the functions I found so far are only limiting the number but are showing the tags in alphabetical order meaning on the post you will always see the same tags, which I want to avoid.
So if I have a post with 10 tags I want to display only 3 at a time, and every time you load that page to display randomly other 3.Any suggestion is much appreciated.
February 27, 2021 at 6:45 am #1674830DavidStaffCustomer SupportHi there,
maybe this function is what you’re looking for:
February 27, 2021 at 9:54 am #1675217MariusHey David,
Thank you for your reply. I found something and adapted it. Can anyone confirm this code is OK to use and doesn’t create any problems? (I am not a programmer and have no clue what’s happening :P)//Tags limit random add_filter('term_links-post_tag','limit_to_five_tags'); function limit_to_five_tags($terms) { shuffle($terms); return array_slice($terms,0,3,true); } //Categories limit random add_filter('the_category_list','limit_to_one_cat'); function limit_to_one_cat($terms) { shuffle($terms); return array_slice($terms,0,1,true); }
February 27, 2021 at 2:42 pm #1675468DavidStaffCustomer SupportLooks good to me!
-
AuthorPosts
- You must be logged in to reply to this topic.