Sean
Sean

Reputation: 105

Trying to use inline conditional PHP on a WordPress site

I am trying to tell my WordPress template to use a specific open graph image if it's currently on the home page. WordPress has a built-in function to determine if you are currently on the homepage:

is_front_page(). 

My template had this in the header.php:

<meta property="og:image" content="<?php echo the_post_thumbnail_url('mint-full-post'); ?>" />

And I changed it to this:

<meta property="og:image" content="<?php is_front_page() ? echo 'http://rainydaystories.com/wp-content/uploads/social.jpg' : echo the_post_thumbnail_url('mint-full-post'); ?>" />

This causes the site to crash. What am I doing wrong, and can anyone help me find a better solution? Thanks!

Upvotes: 1

Views: 1086

Answers (1)

Spoody
Spoody

Reputation: 2882

You are using the ternary operator wrong, it should be like this:

<?php echo (is_front_page()) ? 'http://rainydaystories.com/wp-content/uploads/social.jpg' : the_post_thumbnail_url('mint-full-post'); ?>

You can also use short tags like this:

The <?= part is the same as <?php echo,

However, short tags are disabled in PHP versions older than 5.4.0 and enabled by default in 5.4.0 and above

Upvotes: 5

Related Questions