简体   繁体   English

如何在此代码中添加target =“ _ blank”

[英]How to add target=“_blank” into this code

PHP 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>

It produces this HTML: 它产生以下HTML:

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

I tried reading the wordpress codex but I really couldn't figure out how to add target="_blank" into the a tag. 我尝试阅读wordpress抄本,但我真的不知道如何将target="_blank"a标签中。 I'm trying to add that so that the link will open in a new tab. 我正在尝试添加该链接,以便该链接将在新标签页中打开。

Does anyone know how to do this? 有谁知道如何做到这一点?

Use this filter to apply _blank to get_the_category_list() , because get_the_category_list() runs it output through the filter the_category 使用此过滤器将_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);
}

add this in your functions.php file. 将此添加到您的functions.php文件中。 For more information take a look on these filters http://codex.wordpress.org/Plugin_API/Filter_Reference#Database_Reads_3 有关更多信息,请查看这些过滤器http://codex.wordpress.org/Plugin_API/Filter_Reference#Database_Reads_3

get the category list is meant to be very basic. get the category list意味着非常基本。 You have 3 choices: 您有3种选择:

1) change that function to something else such as using http://codex.wordpress.org/Function_Reference/wp_list_categories 1)将该功能更改为其他功能,例如使用http://codex.wordpress.org/Function_Reference/wp_list_categories

2) parse the returned $categories_list using PHP 2)使用PHP解析返回的$ categories_list

3) use some javascript - let's say you have jquery in your them then you could just add this somewhere: 3)使用一些javascript-假设您有jquery,则可以将其添加到某处:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM