【问题标题】:Adding external links in Wordpress tag list and get the names of tags without their own links在 Wordpress 标签列表中添加外部链接并获取没有自己链接的标签名称
【发布时间】:2019-01-26 19:00:52
【问题描述】:

我在 Wordpress 中有这个标签列表的代码:

$tags_list = get_the_tag_list( '', __( '</li><li>', 'wp-theme') );
if ( $tags_list ) {
    printf( '' . __( '<ul><li>%1$s</li></ul>', 'wp-theme' ) . '', $tags_list );
        }

变成这样的 HTML:

<ul>
<li><a href="http://internal-link/tag1/>TAG NAME 1</a></li>
<li><a href="http://internal-link/tag2/>TAG NAME 2</a></li>
</ul>

但我需要得到这个:

<ul>
<li><a href="http://internal-link/tag1/>TAG NAME 1</a> <a href="https://external-link/?search=TAG+NAME+1">img</a></li>
<li><a href="http://internal-link/tag2/>TAG NAME 2</a> <a href="https://external-link/?search=TAG+NAME+2">img</a></li>
</ul>

我应该如何编辑上面的代码以在每个标签之后添加外部链接以及如何获得没有自己链接的标签名称,以便将其添加到外部链接中?

谢谢!

【问题讨论】:

    标签: php wordpress tags wordpress-theming


    【解决方案1】:

    您可以手动生成输出,而不是使用get_the_tag_list()

    $terms = get_the_tags();
    
    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { // Check if $terms is OK.
        echo '<ul>';
    
        foreach ( $terms as $term ) {
            $link = get_term_link( $term );
            if ( is_wp_error( $link ) ) {
                continue;
            }
    
            // Here, just change the URL.
            $external_link = 'https://external-link/?search=' . $term->name;
    
            echo '<li>' .
                '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>' .
                ' <a href="' . esc_url( $external_link ) . '">' . $term->name . '</a>' .
            '</li>';
        }
    
        echo '</ul>';
    }
    

    这将替换您现有的代码:

    $tags_list = get_the_tag_list( '', __( '</li><li>', 'wp-theme' ) );
    if ( $tags_list ) {
        printf( '' . __( '<ul><li>%1$s</li></ul>', 'wp-theme' ) . '', $tags_list );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      相关资源
      最近更新 更多