How to add dynamic Alt Tags to mb.get_avatar

Support MB Views How to add dynamic Alt Tags to mb.get_avatarResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #33521
    AJ TatumAJ Tatum
    Participant

    I'm trying to build my own author box and would like to have the author's image pick have the alt tag set to the author's name.

    Currently, I have it set to:

    {{ mb.get_avatar( author.ID, 150, 'wavatar', '{{ author.first_name }} {{ author.last_name }}' ) }}

    Which just renders:

    alt="{{ author.first_name }} {{ author.last_name }}"

    Any help would be greatly appreciated!

    Thanks,
    AJ

    #33534
    Long NguyenLong Nguyen
    Moderator

    Hi AJ,

    If you want to add the custom alt text, you can use the <img> tag and the WordPress function get_avatar_url(). For example:

    <img src="{{ mb.get_avatar_url( author.ID ) }}" alt="{{ author.first_name }} - {{ author.last_name }}" />

    Refer to the documentation https://developer.wordpress.org/reference/functions/get_avatar_url/

    #33548
    AJ TatumAJ Tatum
    Participant

    Thanks, that got it working but noticed it didn't generate a source set. After doing some digging, I decided to add a filter to the get_avatar function (see here: https://gist.github.com/ajtatum/4d8c1590b5205e9e6b93b111610fe191)

    And the MetaBox view now just has

    {% set author_name_array = [author.first_name, author.last_name] %}
    {% set author_name = author_name_array|join(' ') %}
    {{ mb.get_avatar( author.ID, 150, 'wavatar', author_name ) }}

    If you know of a better way to accomplish it, let me know, but, for now, this works!

    Thanks,
    AJ

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