如何在Single.php中链接仅供非登录用户使用的特色图片?

时间:2018-09-05 作者:maxwassim

我想自动链接每个WordPress帖子的特色图片,只供访问者使用。因此,它不应该对WordPress的登录用户可用。它应该只链接single.php. 不适用于index.php, archive.php 或其他。

我有此代码,但不知道此代码应放在何处(WordPress中的哪个文件):

<?php
        $image = get_the_post_thumbnail_url( $post->ID, \'large\' );
        $link  = is_user_logged_in() ? $image : \'https://example.com/\';
    ?>
也许我们可以把这个放进去functions.phpsingle.php? 但当我把这个放进去的时候,它破坏了网站,或者什么都不做。

1 个回复
SO网友:Marc

您引用的代码需要放在single.php 文件编写此代码时,您不会看到它向屏幕输出任何类型的内容,因为它只是在保存$image$link 到变量。

您需要执行以下操作才能输出图像:

<?php

$image = get_the_post_thumbnail_url( $post->ID, \'large\' );
$link  = is_user_logged_in() ? $image : \'https://example.com/\';

?>

<a href="<?php echo $link; ?>"><img src="<?php echo $image; ?>"></a>

结束