Tagged: Blog, categories, category, masonry, separator
-
AuthorPosts
-
March 1, 2019 at 8:53 pm #1073382
Hi,
please refer to this snippet of @ismael which works fine:
https://kriesi.at/support/topic/masonry-blog-show-category-name-without-filter-but-directly-in-theme-file/#post-1036481the only disadvantage:
it shows the separator | AFTER the category term; the separator should only separate if there are more than 1 categories inbetween them (see the green arrow), but it should not be shown at the end of the category/categories (= red arrow):
How can I modify Ismael’s snippet:
add_filter( 'avf_masonry_loop_prepare', 'avf_masonry_loop_prepare_mod_cat', 10, 2 ); function avf_masonry_loop_prepare_mod_cat( $key, $entries ) { $categories = get_the_category($key['ID']); $separator = ', '; $output = ''; $items = count($categories); $i = 0; if ( ! empty( $categories ) ) { foreach( $categories as $category ) { if($i++ === $items) { $separator = ''; } $output .= '<span alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</span>' . $separator; } } $key['text_before'] .= trim( $output, $separator ); return $key; }
Thank you.
March 4, 2019 at 11:15 am #1074284Hey Gitte,
I have tested the code and it works fine on my end.
Also I get comma as a separator which is defined in the code but the screenshot shows a pipe |
This part of the code prevents the separator to be added after the last category:if($i++ === $items) { $separator = ''; }
Best regards,
NikkoMarch 4, 2019 at 7:19 pm #1074483sorry, the snippet above is the wrong one, I do not use the filter, I use @ismael modification here:
https://kriesi.at/support/topic/masonry-blog-show-category-name-without-filter-but-directly-in-theme-file/#post-1036902/* show category name in ALB element "blog masonry" */ $categories = get_the_category($entry['ID']); $separator = ' | '; $count = count($categories); $i = 0; $output_cat = "<div class='masonry-categories'>"; if ( ! empty( $categories ) ) { foreach( $categories as $category ) { if($i++ === $count) { $separator = ''; } $output_cat .= '<span alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</span>' . $separator; } } $output_cat .= "</div>"; $cat = trim( $output_cat, $separator ); $text_before .= $cat;
March 7, 2019 at 12:15 pm #1075947Hi,
Thanks for the update.
Do you have a test page where we can see this modification in action? The login token in the other thread has expired, so we didn’t get a chance to take a look.
Best regards,
IsmaelMarch 7, 2019 at 6:37 pm #1076081see private data
March 11, 2019 at 11:34 am #1077270Hi,
Thanks for the update. There’s nothing in the private field though. Please create a test page.
Best regards,
IsmaelMarch 11, 2019 at 1:49 pm #1077334sorry, here it is
March 18, 2019 at 9:41 am #1079782Hi,
Thanks for the update.
1.) Try to edit the following code.
if($i++ === $count) {
Replace it with:
if($i++ == $count) {
2.) We have already mentioned on the previous thread that you won’t be able to click on the category because the masonry item or container itself is a link.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.