我想使用下面的add\\u filter函数将外部HTML链接添加到帖子缩略图,但代码不起作用。
add_filter( \'post_thumbnail_html\', \'my_post_link_html\', 10, 3 );
function my_post_link_html( $html, $post_id, $post_image_id ) {
if ( $GLOBALS[\'ghostpool_affiliate_button_link\'] ) {
$affiliate_link = $GLOBALS[\'ghostpool_affiliate_button_link\'];
$affiliate_target = apply_filters( \'gp_affiliate_link_target\', \'\' );
}
$html = \'<a href="\' . get_permalink( $affiliate_link ) . \'" rel="nofollow" . target="\' . $affiliate_target . \'">\' . $html . \'</a>\';
return $html;
}
UPDATE:
<?php if ( $GLOBALS[\'ghostpool_affiliate_button_link\'] ) { ?>
<a href="<?php echo $GLOBALS[\'ghostpool_affiliate_button_link\']; ?>" id="gp-affiliate-button" rel="nofollow" target="<?php echo apply_filters( \'gp_affiliate_link_target\', \'\' ); ?>">
<?php echo $GLOBALS[\'ghostpool_affiliate_button_text\']; ?>
</a>
<?php } ?>
最合适的回答,由SO网友:Dave Romsey 整理而成
听起来你想用get_permalink()
在URL上。get_permalink()
需要帖子ID,而不是URL。尝试以下操作:
add_filter( \'post_thumbnail_html\', \'my_post_link_html\', 10, 3 );
function my_post_link_html( $html, $post_id, $post_image_id ) {
// Uncomment for debugging. This will replace the featured image with the debugging output.
// exit ( var_dump( $GLOBALS[\'ghostpool_affiliate_button_link\'] ) );
if ( $GLOBALS[\'ghostpool_affiliate_button_link\'] ) {
$affiliate_link = $GLOBALS[\'ghostpool_affiliate_button_link\'];
$affiliate_target = apply_filters( \'gp_affiliate_link_target\', \'\' );
$html = \'<a href="\' . esc_url( $affiliate_link ) . \'" rel="nofollow" . target="\' . esc_attr( $affiliate_target ) . \'">\' . $html . \'</a>\';
}
return $html;
}