• Resolved TheJer

    (@thejer)


    I’ve searched all over and found a bunch of different topics on this, but I don’t believe anyone has been searching for as simple a solution as I am.

    I’ve decided to make all my page titles images, while it looks nice, it screws up another portion of my site that is based on pulling the title of the page. So the premise is just make the alt the previous name of the page.

    Example the page ‘Colleges’ is now <img src=’https://website/images/colleges.jpg&#8217; alt=’Colleges’ />

    All I need to do is know if there is an easier way to get that alt text other than calling the_title() and then using a regex to grab it.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • So the premise is just make the alt the previous name of the page.

    You do know what the alt text is meant to be used for, yes?

    Thread Starter TheJer

    (@thejer)

    Yes I do understand what the alt text is supposed to be used for (unless it has somehow changed in the past few years?). I guess I just worded that badly because the image is the name of the page just in image form.

    Ah! I thought you were trying to make the alt text the name of the previous page – which would result in a really confusing page in a non-graphical browser. Have you looked at the_title_attribute? That would seem to be ideal for your use.

    Thread Starter TheJer

    (@thejer)

    Previous was obviously the wrong word to use haha. I should have said ‘the premise is to make the alt text the same as what the page used to be called.’ Either way, thank you for the help. I had been using the_title to grab the name, but I’ll play around with that and see if I can figure it out.

    Thread Starter TheJer

    (@thejer)

    Well I got it figured out. I’m hoping that simple regular expressions wont slow down the site in the long run. I’m not entirely sure why, but calling the_title_attribute would not return what I wanted. So I ended up using the_title and creating a little function called get_alt.

    function get_alt( $str ) {
    $pattern = '/alt=\'(.*?)\'/';
    preg_match($pattern, $str, $match);
    return $match[1];
    }

    $match looks like
    [0] => alt=’whateveryouwerelookingfor’
    [1] => whateveryouwerelookingfor

    My PHP/regex knowledge is somewhat limited, but I think its because I’m actually matching for the alt= part, but the saving whats in between the ” and that turns up in [1] spot in the array?

    Either way, problem solved!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Getting the alt text from an embedded image’ is closed to new replies.