Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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-1036481

    the 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):

    View post on imgur.com

    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.

    #1074284

    Hey 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,
    Nikko

    #1074483

    sorry, 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;
    #1075947

    Hi,

    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,
    Ismael

    #1076081

    see private data

    #1077270

    Hi,

    Thanks for the update. There’s nothing in the private field though. Please create a test page.

    Best regards,
    Ismael

    #1077334

    sorry, here it is

    #1079782

    Hi,

    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.

    // https://kriesi.at/support/topic/masonry-blog-show-category-name-without-filter-but-directly-in-theme-file/#post-1036481

    Best regards,
    Ismael

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.