Skip to main content

Master Function - alt() and get_alt()

Author

Robert Loncaric

Timestamps

Created: 01 August 2024
Modified: 01 August 2024

This function just saves a bit of typing. I always put alt for images (sometimes use it elswhere too) and this way I don't have to think.

alt() grabs the pages ' title and website name, concatenates them with a separator and renders them.
get_alt() grabs the pages ' title and website name, concatenates them with a separator and only returns it without rendering.

This function is already in functions.php

Steps

Goes to functions.php if not already there.

Simply call it in front with <?php alt(); ?>

Goes to functions.php if not already there.

Simply call it in front with<?php echo get_alt(); ?>

if ( ! function_exists( 'alt' ) ) {
    /**
     * Display the title and blog info.
     */
    function alt() {
        echo get_the_title() . ' - ' . get_bloginfo();
    }
}
if ( ! function_exists( 'get_alt' ) ) {
    /**
     * Display the title and blog info.
     */
    function get_alt() {
        return get_the_title() . ' - ' . get_bloginfo();
    }
}