• Hello,

    Been fiddling with PHP and I’m quite new. I been trying to add a filter to the code below to add the title attribute. It’s not quite right.

    <?php
        global $wyde_options, $wyde_blog_layout, $wyde_sidebar_position;
        if(!$wyde_blog_layout) $wyde_blog_layout = $wyde_options['blog_layout'];
    
        $has_images = has_post_thumbnail();
        
        $content_col = '';
        if(!is_single() && $wyde_blog_layout == 'medium') $content_col = ' col-sm-6';
        
        $image_size = 'blog-medium';
        if(is_single() || $wyde_blog_layout == 'large'){
            if($wyde_sidebar_position != '1') $image_size = 'blog-large';
            else $image_size = 'blog-full';
        }
    
        $post_class = array();
        $post_class[] = $has_images ? 'has-cover':'no-cover';
        $post_class[] = $wyde_blog_layout != 'masonry' ? 'clear' : '';
    
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class( implode(' ', $post_class) ); ?>>
        <div class="post-header<?php echo esc_attr( $content_col ); ?>">
            <?php if(!is_single()){ ?>
            <div class="post-date">
                <span class="date"><?php echo get_the_date(); ?></span>
            </div>
            <?php }?>
            <div class="image-wrapper">
                <?php
                if($has_images){
    
                    //Add filter to include title tag
                    add_filter('wyde_post_thumbnails', function($image_size) {
                      $post_permalink = get_permalink();
                      $title = $post_permalink->post_title;
                      $image_size = str_replace('src=', 'title="'.$title.'" src=', $image_size);
                      return $image_size;
                    });
                    //   
    
                    wyde_post_thumbnails($image_size);
    
                }else if(!is_single()){
                    wyde_post_title();
                }  
                ?>
            </div>
        </div>

    Also, I tried to place an array behind it to add on the title but that doesn’t work either as shown below:
    wyde_post_thumbnails($image_size, array( 'title' => get_the_title() ) );

    Any help will be great! Thank you.

Viewing 1 replies (of 1 total)
  • That’s not where you should be putting the filter code. It’s not clear what you’re trying to do, but maybe you need to change it to:

    $image_size = wyde_post_thumbnails($image_size);

    One thing I do to debug that type of thing is to output the html wrapped in a comment before and after calling a function, perhaps with htmlspecialchars() if appropriate.

    echo '<!-- ' . $image_size . ' -->';

    or something like that.

Viewing 1 replies (of 1 total)
  • The topic ‘Filter to Add the Title Attribute to Post_Thumbnail’ is closed to new replies.