[英]Get tags IDs in post
我在 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>';
}
現在我為標簽添加了一個自定義字段,它的 slug 是“_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>';
}
謝謝!
您可以從術語 object 中獲取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>';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.