如何添加target =" _blank"进入这段代码

时间:2014-03-21 05:24:53

标签: php wordpress

PHP

<p class="postmeta">
    <?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
        <?php
            /* translators: used between list items, there is a space after the comma */
            $categories_list = get_the_category_list( __( ', ', 'gridster' ) );
            if ( $categories_list && gridster_categorized_blog() ) :
        ?>
            <?php printf( __( '%1$s', 'gridster' ), $categories_list ); ?>
        <?php endif; // End if categories ?>


    <?php endif; // End if 'post' == get_post_type() ?>
</p>

它生成此HTML:

<p class="postmeta">
    <a href="http://myurl.com/category/blahblah/" title="View all posts in blahblah" rel="category tag">blahblah</a>            
</p>

我尝试阅读wordpress codex但我真的无法弄清楚如何将target="_blank"添加到a标记中。我正在尝试添加它,以便链接将在新选项卡中打开。

有谁知道怎么做?

2 个答案:

答案 0 :(得分:1)

使用此过滤器将_blank应用于get_the_category_list(),因为get_the_category_list()通过过滤器the_category

运行输出
add_filter('the_category', 'wp55_the_category');
function wp55_the_category($cat_list) {
    return str_ireplace('<a', '<a target="_blank"', $cat_list);
}

functions.php文件中添加此内容。有关详细信息,请查看这些过滤器http://codex.wordpress.org/Plugin_API/Filter_Reference#Database_Reads_3

答案 1 :(得分:0)

get the category list意味着非常基本。你有3个选择:

1)将该功能更改为其他功能,例如使用http://codex.wordpress.org/Function_Reference/wp_list_categories

2)使用PHP

解析返回的$ categories_list

3)使用一些javascript - 假设你有jquery,然后你可以在某处添加:

jQuery.ready(function() {
 jQuery('.postmeta a').attr('target','_blank');
});
相关问题