Tagged: ,

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1285070

    Inject category name into masonry element
    https://kriesi.at/support/topic/inject-category-name-into-masonry-element/

    Add in Masonry after title also the Author’s name and category

    I have implemented both of these scripts , However, The Category name does not show.

    I am using a custom post type.
    ALB and METABOXs have been enabled to use custom post type.

    using the scripts above when I
    var_dump($categories);
    I get an array
    array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }
    The HTML element is injected and I can see this in the source code
    <div class="ww-masonry-cat"></div>

    I am thinking something else needs to be added to terms query

    if I do this I at least get the dump of categories.

      $terms = get_terms(
    							array(
    								'hide_empty' => true,
    								'taxonomy' => 'name_category',
    						);
      var_dump($terms);

    Further guidance required.

    #1285077

    SOLVED

    For anyone else having the same problem – here is my final solution.

    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']);
       $categories = get_the_terms( $key['ID'], 'tax_category' );  //change 'tax_category' to suit your registered post type taxonomy
       // var_dump($categories);
        $separator = ' ';
        $output = '<div class="ww-masonry-cat">';
        if ( ! empty( $categories ) ) {
            foreach( $categories as $category ) {
                $output .= '<span alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</span>' . $separator;
            }
        }
        $output .= '</div>';
    
        $key['text_before'] .= trim( $output, $separator );
        return $key;
    }
    
    // new script
    add_action('wp_footer', 'ava_new_custom_script_masonry');
    function ava_new_custom_script_masonry(){
    ?>
    <script type="text/javascript">
    (function($) {
        function a() {
            var masonry = $('.av-masonry-entry');
            if(!masonry.length) return;
            masonry.each(function() {
                var cat = $(this).find('.ww-masonry-cat');
                $(this).find('h3.av-masonry-entry-title').insertBefore(cat);
            });
        }
    
        a();
    
        $(window).on('debouncedresize av-height-change', function() {
            setTimeout( a(), 300 );
        });
    })(jQuery);
    </script>
    <?php
    }
    #1285214

    Hi HuxburyQuinn,

    We’re glad to hear that :)
    And thanks for sharing the solution that you made.

    Best regards,
    Nikko

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