Función para obtener y utilizar la url del tema en un shortcode

Shortcode para ahorrarnos poner ruta absoluta a la hora d eir a buscar recursos al directorio del tema. La imagen se encontrará dentro del tema utilizado:a función la insertamos dentro de functions.php

function theme_url_shortcode( $attrs = array (), $content = '' ) {
    $theme = ( is_child_theme() ? get_stylesheet_directory_uri() : get_template_directory_uri() );
    return $theme;  
}
add_shortcode('theme', 'theme_url_shortcode' );

Ejemplo de uso

<img src="[theme]/images/image-name.jpg" width="250" height="250" alt="This is an image" />

Visto en TheWebTaylor

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Scroll al inicio

We are using cookies on our website

Please confirm, if you accept our tracking cookies. You can also decline the tracking, so you can continue to visit our website without any data sent to third party services.