Reputation: 9
I'd like to link featured images of Wordpress to an existing page for non-members (not logged-in users) in single.php (just for posts).
Tried this code but no success:
<?php
if (has_post_thumbnail( $post->ID ) ) {
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
$image = $image[0];
}
?>
So how can I link all of the featured images in posts to one page on Wordpress for non-logged in users (visitors) ? Note: This is for all featured images on the site inside posts.
Upvotes: 0
Views: 53
Reputation: 13880
You've got a few issues with your code first of all, like the <?php
tag inside a <?php
tag, and the fact you're combing the shorthand echo <?= $image ?>
and <?php echo $image; ?>
- You should stick with a single syntax for consistency.
That said, all you have to do is change the link using the is_user_logged_in()
function. Right now you have the link set to the value of the $image
(which I've shortened as well).
Now $link
will be defined as the $image
URL returned by get_the_post_thumbnail_url()
if the user is logged in, otherwise it will be set to https://example.com
. Note, this syntax is basically a shorthand "if else" statement called a Ternary Operator - handy for simple "one or the other" type variable definitions.
<article <?php post_class('col-md-4 site-content'); ?>>
<?php
$image = get_the_post_thumbnail_url( $post->ID, 'large' );
$link = is_user_logged_in() ? $image : 'https://example.com/';
?>
<a href="<?php echo $link; ?>" target="_blank">
<div class="coupons-post" style="background-image: url('<?php echo $image; ?>')"></div>
</a>
</article>
Upvotes: 1