簡體   English   中英

Wordpress-在博客文章中創建指向單獨作者檔案的鏈接

[英]Wordpress - Create link to separate author archive inside blog post

我們正在Wordpress中開發一個博客,並且遇到了一些難以實現的功能。

我們的客戶希望能夠在博客文章中提及博客用戶,然后將最終用戶帶到提及的作者個人資料。 下面的例子:

"We recently spoke to our newest team member Pete Smith" "We recently spoke to our newest team member @PeteSmith" "We recently spoke to our newest team member Pete Smith""We recently spoke to our newest team member @PeteSmith"

如果您單擊Pete的名字,它將帶您到website.com/authors/petesmith

我知道Wordpress在注釋部分內置了提及功能,但是有沒有辦法在實際的博客文章中實現此功能?

抱歉,我無法在此問題上包含任何代碼,因此沒有什么可以向大家展示。

您可以在functions.php中使用類似這樣的內容:

add_filter('the_content', 'the_content_func');

function the_content_func($content) {
    $ret = preg_replace_callback ( "/(@[A-Za-z]+)/",
        function ($match) {
            $authorName = mb_substr($match[0], 1);
            $author = get_user_by('slug', $authorName);
            if ( ! empty( $author ) )
                return '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . $author->display_name . '</a>';
            else
                return $authorName;
        },
    $content);
    return $ret;
}

我假設@符號后的文本是作者的頭銜,因此在過濾器中我搜索了給定的作者,如果找到了他,則輸出具有顯示名稱的相應作者個人資料鏈接。 否則,我只輸出不帶@符號和URL的字符串。

如果我誤解了您的目標,請隨時修改preg_replace_callback的內部函數,使其中$ match [0]包含從帖子內容中發現帶有@符號的用戶子彈。

這是作者存檔頁面的代碼

<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM