获取 post 中的标签 ID

Get tags IDs in post

我在 Wordpress 帖子中有这个标签列表代码,我在每个标签旁边添加了一个外部 link:

$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>';
}