WordPress Internal @ Mentions

时间:2012-12-25 作者:AndrettiMilas

我正在寻找一种在WordPress安装中链接到作者页面的方法,只需在帖子中的用户名前加上@符号。。。。就像在推特上一样。

示例:

如果用户名是“Bill256”,我写的是“@Bill256”,它将链接到他的作者页面。

2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

这有点棘手,因为sanitize_user 允许在用户名中使用空格,这意味着很难避免抓住整个短语“@johndoe said that…”与实际用户名“@johndoe”相反,您在末尾没有分隔符,这会有所帮助。为了避免这种情况,我强制要求用户名中的空格替换为“+”。

function look_for_author($login) {
  if (!empty($login[1])) {
    $lname = str_replace(\'+\',\' \',$login[1]);
    $user = get_user_by(\'login\',$lname);
    if (!empty($user)) return \' <a href="\'.get_author_posts_url($user->ID).\'">\'.$lname.\'</a> \';
  }
  return \' \'.$login[0].\' \';
}

function hyperlink_authors( $content ){
  $content = preg_replace_callback(
    \'/[\\s>]+@([A-Za-z0-9_.\\-*@\\+]+)[^A-Za-z0-9_.\\-*@\\+]/\',
    \'look_for_author\',
    $content
  );
  return $content;
}
add_filter( \'the_content\', \'hyperlink_authors\', 1 );
我不希望这个解决方案非常健壮,除非对regex进行大量调整。我想你最好是shortcode, 但你来了。

注意:我突然想到,这个网站有一个类似于提及的功能。写评论时,你可以通过写“@用户名”通知其他用户,但这里的用户名可以像WordPress一样有空格。这里的“空间”问题由requiring that spaces just be removed, 而不是用“+”符号代替。这可能是解决问题的另一种方法。

SO网友:Otto

看看P2主题。它做到了这一点,称之为“提及”。

http://themes.svn.wordpress.org/p2/1.4.2/inc/mentions.php

结束

相关推荐

post author if statement

嗨,我正在尝试使用if语句。如果帖子是由特定作者创建的,我想显示一些HTML代码。这就是我想到的,但它不起作用。<?php if ( get_posts ($post->ID, \'post_author\', true) == \'20\') { ?> HTML <?php } ?> 谢谢你