• I try to change loading=”lazy” to loading=”eager” from the first post thumbnail in categories and tags. I tried to conditionally exclude posts and pages with !is_single() && !is_page() but the function doesn’t work.

    add_filter( 'wp_get_attachment_image_attributes', function( $attrs ) {
        if ( !is_single() && !is_page() && is_front_page() && is_archive() && is_search() ) {
        global $wp_query;
    
        if ( $wp_query->current_post === 0 )
            $attrs['loading'] = 'eager';
            return $attrs;
        }
        return $attrs;
    } );
Viewing 3 replies - 1 through 3 (of 3 total)
  • it might be enough to check:

    (if you want the same behaviour in the search results)
    if ( is_archive() !! is_search() )

    or just for the archives:
    if ( is_archive() )

    Thread Starter tw8sw8dw8

    (@tw8sw8dw8)

    I want to exclude all posts and pages. Only to change attributes on Categories, Tags, Search results and home.

    Moderator bcworkz

    (@bcworkz)

    There are is_*() functions for all the requests you listed. Logically OR them all.
    if( is_category() || is_tag() || is_search() || is_home() ):

    Where you get into trouble with multiple checks like this is when you mix in ! NOT operators. The necessary logic becomes difficult to parse for us because what makes sense expressed in English doesn’t always deliver the expected result in code.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change attribute on the first post thumbnail in Categories’ is closed to new replies.