0

Hi guys I have a quick question:

My path which I used for images in the php-files locally was something like this:

src="<?php echo THEME_IMG_PATH; ?>/icons/cv_icon.png"

But from the wp-admin this doesnt work and I do have to for example:

src="wp-content/themes/mytheme/images/icons/about_icon.png"

First, why is that? Because i defined the THEME_IMG_PATH in function.php? But then why dont for example

<?php echo get_template_directory_uri(); ?>/images/image.jpg

or

<?php echo get_stylesheet_directory_uri(); ?>/images/image.jpg

work? And, is the wp-content/... way the right way to do it, or should you do it in another way . in a way of good practice? It works but maybe its "wrong" to hardcode it like that.

This img shows that witt using php the php is not treated as code but the src url and another link/imageone showing as it should with a relative path. enter image description here Thanks

Solution: look further down on example for shortcoding, editing your functions and then calling this shortcode from wp-admin.

15
  • How are you defining THEME_IMG_PATH ?
    – Ben Casey
    Commented Mar 9, 2017 at 12:15
  • Like this: if( !defined(THEME_IMG_PATH)){ define( 'THEME_IMG_PATH', get_template_directory_uri() . '/images' ); } And the path is /images/icons/cv_icon.png. Maybe it hasnt been set yet - that functions load after this somehow?
    – emcolsson
    Commented Mar 9, 2017 at 12:24
  • 1
    Add a trailing slash before all, src="/<?php echo THEME_IMG_PATH; ?>/icons/cv_icon.png"?
    – Ismail
    Commented Mar 9, 2017 at 12:31
  • @SamuelElh No, sadly not.
    – emcolsson
    Commented Mar 9, 2017 at 12:37
  • 1
    @SamuelElh The shortcode worked out great!
    – emcolsson
    Commented Mar 9, 2017 at 15:42

1 Answer 1

-1

Best to use a images folder in your child theme and output like this:

$output = sprintf( '<img class="your-class" src="%s" alt="%s" />', get_stylesheet_directory_uri() .'/images/image.jpg', get_the_title( $post->ID ) );
echo $output;
3
  • How would adding a child theme help? Even if there was no child theme, get_stylesheet_directory_uri() would still work. It would just select the active theme.
    – Tex0gen
    Commented Aug 25, 2021 at 10:05
  • Always use a child theme or a custom functions plugin. Commented Oct 20, 2021 at 8:19
  • That depends. You're assuming this is a pre-developed theme. If it was, yes, use a child theme. If bespoke then no.
    – Tex0gen
    Commented Oct 21, 2021 at 10:27

Your Answer

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

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