简体   繁体   English

如何使特色图片可点击? - WordPress

[英]How to make featured image clickable? -Wordpress

How to make all the featured image clickable automatically?如何让所有特色图片自动点击? As it's Wordpress theme, I can only edit php or add css.由于它是 Wordpress 主题,我只能编辑 php 或添加 css。

Simply add this code to your theme's functions.php file.只需将此代码添加到主题的functions.php 文件中即可。

This code simply adds a link around the code generated to display featured images or post thumbnails on your website.此代码只是在生成的代码周围添加一个链接,以在您的网站上显示特色图像或发布缩略图。

function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {
    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    return $html;
    }
    add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

This code will also add a link around featured images on single post pages.此代码还将在单个帖子页面上添加围绕特色图像的链接。 If you don't want to link featured images on single post to the same post, then use this code.如果您不想将单个帖子上的特色图片链接到同一帖子,请使用此代码。

function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {

If (! is_singular()) { 

$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;

} else { 

return $html;

}

}
add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

Normally the functionality of a featured image has no link attached to it.通常,特色图像的功能没有附加链接。 Different themes use the functionality of featured image differently.不同的主题以不同的方式使用特色图像的功能。 The new Twenty Nineteen theme, for example, uses it as the background for the page header, behind the post title.例如,新的 29 主题将其用作页面标题的背景,位于帖​​子标题后面。

So it will be better to make a child theme of your parent theme and edit the php file to set a link.所以最好做一个父主题的子主题,编辑php文件设置链接。 Without editing the code you cannot set the link of the featured image.如果不编辑代码,您将无法设置特色图片的链接。

插入图像 > 单击要插入的所需单元格 > 在右侧向下:“链接到” > 个性化 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM