繁体   English   中英

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

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

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抄本,但我真的不知道如何将target="_blank"a标签中。 我正在尝试添加该链接,以便该链接将在新标签页中打开。

有谁知道如何做到这一点?

使用此过滤器将_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

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

暂无
暂无

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

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