我在Wordpress帖子中的标签列表中有以下代码,在每个标签旁边添加了一个外部链接:
$terms = get_the_tags();
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
$external_link = 'https://www......../?search3=' . $term->name . '&pn=xxxx';
echo '<li>' .
'<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a> ' .
'<a href="' . esc_url( $external_link ) . '" target="_blank">img</a>' .
'</li>';
}
echo '</ul>';
}
现在,我向标签添加了一个自定义字段,其子标签为“ _tag_link”。我想用自定义字段“ _tag_link”替换实际的“ $ external_link”,并仅在存在时显示它。我以这种方式更改了代码,但是我不知道如何获取“ $ term_id”(如果代码总体上正确):
$terms = get_the_tags();
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
echo '<li>' .
'<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a> ';
if ( $external_link = get_term_meta($term_id, '_tag_link', true) ) {
echo '<a href="' . $external_link .'" target="_blank" rel="noopener">img</a>';
}
echo '</li>';
}
echo '</ul>';
}
谢谢!
您可以term_id
从术语对象获得:
$terms = get_the_tags();
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
echo '<li>' . '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a> ';
if ( $external_link = get_term_meta( $term->term_id, '_tag_link', true ) ) {
echo '<a href="' . $external_link . '" target="_blank" rel="noopener">img</a>';
}
echo '</li>';
}
echo '</ul>';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句