您可以使用add_shortcode
函数为您的站点注册自定义快捷码。比如像这样,
// add to (child) theme\'s functions.php
function my_download_featured_image_link( $atts, $content = \'\' ) {
// shortcode should return its output
return sprintf(
\'<a class="download_image" href="%s">%s</a>\', // html output of the shortcode
esc_url( get_the_post_thumbnail_url(null, \'full\') ), // defaults to current global $post
esc_html__( \'Download featured image\', \'textdomain\' ) // translatable anchor text
);
}
add_shortcode( \'my_download_featured_image\', \'my_download_featured_image_link\' );
要使用快捷码,请添加
[my_download_featured_image]
到您的帖子内容。
或者,您可以使用the_content
筛选以在帖子内容末尾自动添加下载链接。链接输出可能需要一些调整,才能与您正在使用的页面生成器很好地配合使用。
// add to (child) theme\'s functions.php
function my_download_featured_image_link_after_content( $content ) {
if ( is_singe() || has_post_thumbnail() ) {
return $content . sprintf(
\'<a class="download_image" href="%s">%s</a>\',
esc_url( get_the_post_thumbnail_url(null, \'full\') ),
esc_html__( \'Download featured image\', \'textdomain\' )
);
} else {
return $content;
}
}
add_filter( \'the_content\', \'my_download_featured_image_link_after_content\' );