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.

bumped to the homepage by Community 23 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • How are you defining THEME_IMG_PATH ? – Ben Casey Mar 9 '17 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 Mar 9 '17 at 12:24
  • 1
    Add a trailing slash before all, src="/<?php echo THEME_IMG_PATH; ?>/icons/cv_icon.png"? – Samuel Elh Mar 9 '17 at 12:31
  • @SamuelElh No, sadly not. – emcolsson Mar 9 '17 at 12:37
  • 1
    @SamuelElh The shortcode worked out great! – emcolsson Mar 9 '17 at 15:42
0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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