Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1034176

    Hi,

    first: I am not a coder and try to work with PHP with try&error :-D
    I would like to work not with a filter for the functions.php like @Ismael has posted somewhere in the forum, but for a better control for myself directly in the file

    av-helper-masonry.php

    around line 539

    where the part is

    
    $this->loop[$key]['text_before']	= "";
    $this->loop[$key]['text_after']       = "";
    $this->loop[$key]['ID'] = $id		= $entry->ID;
    $this->loop[$key]['post_type'] 		= $entry->post_type;
    $this->loop[$key]['thumb_ID'] 		= get_post_thumbnail_id($id);
    $this->loop[$key]['the_title'] 		= get_the_title($id);

    View post on imgur.com

    Could you please tell me, what PHP code exactly I need to add in that way
    between the thumbnail and the title,
    to output the category name in this theme file?

    Without a good PHP knowledge I have tried it with this part:

    $this->loop[$key]['category'] = get_the_category($id);

    but this has no effect. How should it be right?
    Thank you.

    #1035226

    Hey Gitte,

    Can we ask why not use the filter?
    Filters will help with the update and on how to use it faster without having to modify the file each time.

    The get_category function has to be worked somehow like that

    $post_categories = wp_get_post_categories( 4 );
    $categories = get_the_category($post_categories[0]);

    Best regards,
    Basilis

    #1035261

    I work within the theme files, it is easier for me and my bad PHP knowledge, and it is easier for me with further tweaks of this styling – that’s my way to work.

    So I placed your snippet as asked into av-helper-masonry.php around line 539 in this context:

    
    				$this->loop[$key]['text_before']	= "";
    				$this->loop[$key]['text_after']		= "";
    				$this->loop[$key]['ID'] = $id		= $entry->ID;
    				$this->loop[$key]['post_type'] 		= $entry->post_type;
    				$this->loop[$key]['thumb_ID'] 		= get_post_thumbnail_id($id);
    				
    				$post_categories = wp_get_post_categories( 4 );
    				$categories = get_the_category($post_categories[0]);
    													
    				$this->loop[$key]['the_title'] 		= get_the_title($id);
    				$this->loop[$key]['url']			= get_permalink($id);
    				$this->loop[$key]['date'] 			= "<span class='av-masonry-date meta-color updated'>".get_the_time($date_format, $id)."</span>";
    				$this->loop[$key]['author'] 		= "<span class='av-masonry-author meta-color vcard author'><span class='fn'>". __('by','avia_framework') .' '. $author."</span></span>";
    				$this->loop[$key]['class'] 			= get_post_class("av-masonry-entry isotope-item", $id); 
    				$this->loop[$key]['content']		= strip_tags( $entry->post_excerpt );
                    $this->loop[$key]['description']	= !empty($entry->post_content) ? $entry->post_content : $entry->post_excerpt;
    				

    and this output no category name above the title:

    View post on imgur.com

    Please test it at your demo installation, my site is not yet online. Thanks.

    #1036481

    Hi,

    Thanks for the update.

    Instead of editing the file directly, you can use this filter to insert the categories after the masonry image.

    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;
    }

    You won’t be able to click on the category items though, because they are inside the masonry item, which is a link element itself.

    Best regards,
    Ismael

    #1036661

    thank you, Ismael, appreciate your help, but sorry to bother you, but could you please give me the solution for editing the theme file directly please?

    The reason is, I cannot “tweak” that filter thing from you with my bad PHP knowledge for further adaptions, but I guess I would more success for further editing things in that file, when I have your solution for the theme file directly. Thanks.

    #1036902

    Hi,

    No problem. You can add this code right before line 409.

    $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;
    

    Or just add it below this code:

     else if(strpos($html_tags[0], 'a href=') !== false)
                    {
                        $display = empty( $title ) ? $the_title : $title;
                        $linktitle = 'title="' . esc_attr( $display ) . '"';
    				}
    

    Best regards,
    Ismael

    #1037137

    It works fine, thank you very, very much, Ismael. I appreciate your time and efforts very much!

    #1037157

    Hi,

    I’m glad this was resolved. If you need additional help, please let us know here in the forums.

    Best regards,
    Jordan Shannon

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Masonry blog: show category name without filter, but directly in theme file?’ is closed to new replies.