Lee Jones
Lee Jones

Reputation: 23

PHP tag in Image url?

Pretty basic problem, so sorry but have been banging my head off the wall for well over an hour.

basically writing a script to set a default Wordpress featured image for a category if there is no featured set.

            if ( get_the_post_thumbnail( $projects->post->ID ) ) { 
                $imagetag = get_the_post_thumbnail( $projects->post->ID, 'portfolio-thumb', array( 'class' => 'img-square', 'alt' => get_the_title($projects->post->ID) ) ).''; }
            else {

            $imagetag .='<img src="ThisIsMySite\img\"'.$tag->slug .'"image.jpg" />';                
            } 

All works, except I can't get the else image to work, the closest I can get is to output :

img src="ThisIsMySite\img\"maths"image.jpg"

(Where maths is the current category) It must be something stupid / really obvious I'm missing / doing wrong but I can't work it out.

Upvotes: 0

Views: 78

Answers (2)

Martin
Martin

Reputation: 22760

Clearshot66 almost had the right idea, but had flipped the quotes. You want to retain the single quotes and remove the excess double quotes.

single quotes here are PHP quotes, double quotes are HTML entity quotes.

   $imagetag .='<img src="ThisIsMySite/img/'.$tag->slug .'image.jpg" />';    

aka:

 $imagetag = '<string 
                      HTML ELEMENT = 
                      "HTML URL STING>' 
             CONCAT $tag->slug CONCAT 
             '< HTML ELEMENT "
              string>'; 

Also replace your \ with / because it's online, it uses forward slashes as seperators rather than backward slashes.

Upvotes: 2

mirvatJ
mirvatJ

Reputation: 376

this should do it for you

$imagetag .='<img src="ThisIsMySite/img/'.$tag->slug .'image.jpg" />'; 

hope this helps

Upvotes: 0

Related Questions