2

How do i reference images in a page/post without installing the php plugin or typing it out manually like this:

domain.com/wp-content/themes/mytheme/image.jpg

1 Answer 1

3

Use get_template_directory_uri()

print get_template_directory_uri() . '/image.jpg';

In child themes use get_stylesheet_directory_uri() if you have replaced the image.

In a shortcode this would look like this:

<?php
/* Plugin Name: Theme URI Shortcode */

add_shortcode('theme_uri', 'wpse_66026_theme_uri_shortcode' );

function wpse_66026_theme_uri_shortcode( $attrs = array (), $content = '' )
{
    $theme_uri = is_child_theme()
        ? get_stylesheet_directory_uri()
        : get_template_directory_uri();

    return trailingslashit( $theme_uri );
}
4
  • that looks like a php command, how would i post php into pages? is there another way of doing it like shortcodes?
    – Exploit
    Sep 23, 2012 at 16:19
  • Sorry, I misread your question. See my update.
    – fuxia
    Sep 23, 2012 at 16:27
  • does that shortcode already exist? or i have to put it myself? and does it go into the functions.php file?(in the themes folder) thnx
    – Exploit
    Sep 23, 2012 at 16:37
  • 2
    Put it into a plugin. Otherwise the content will be broken after a theme switch. Shortcodes should never go into theme files.
    – fuxia
    Sep 23, 2012 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.