Aness
Aness

Reputation: 670

Is there a Wordpress php function that can return just the URL of the Custom Logo image(not the full image tag)?

I am trying to code a small custom wordpress theme. So I am overriding on the twentytwenty theme of wordpress.

Just to be clear I am talking PHP and overriding home.php, where I want to create some custom html header.

I am trying to get the url of the logo (that I (or the user, can change) using the wordpress customizer.

What I did is :

<img src="<?php echo (get_custom_logo()) ? get_custom_logo() : 'somefallback_url'; ?>" >

What is happening is :

get_custom_logo() function is returning an Image which is normal, I can't find the function that should return the URL in the codex. An error is happening i have an image inside another one.

So basically what I want is :

A PHP function that returns just the URL not the full Image tag.

Upvotes: 7

Views: 16470

Answers (3)

Arman H
Arman H

Reputation: 1754

if you want to return only the image src URL you can use this with the simplest way.

$custom_logo_id = get_theme_mod( 'custom_logo' );
$image_url = wp_get_attachment_image_url ( $custom_logo_id , 'full' );

Upvotes: 1

For a short one-liner:

<?php echo esc_url( wp_get_attachment_image_src( get_theme_mod( 'custom_logo' ), 'full' )[0] ); ?>

Upvotes: 10

Eriks Klotins
Eriks Klotins

Reputation: 4180

The codex lies it out for you

function get_custom_logo_url()
{
    $custom_logo_id = get_theme_mod( 'custom_logo' );
    $image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
    return $image[0];
}

Upvotes: 7

Related Questions