如何编辑“发布到类别” Wordpress 帖子“linkify”的一部分整个短语而不仅仅是类别?

发布于 2024-11-05 09:47:15 字数 663 浏览 2 评论 0原文

嘿伙计们,我有一个非常基本的问题(尽管我知道标题听起来很混乱),最好解释如下。这段代码是我目前拥有的:

<?php if (!is_page()): ?><?php ob_start(); ?><?php printf(__('Posted to %s', 'kubrick'), get_the_category_list(', ')); ?>

生成此 HTML 代码:

Posted to <a href="http://www.example.com/category/" title="View all posts in Category" rel="category tag">Category</a>

如您所见,该类别是通过此代码“链接”的,但是我希望整个短语成为链接的锚文本,如下所示

<a href="http://www.example.com/category/" title="View all posts in Category" rel="category tag">Posted to Category</a>

:我修改原始 PHP 代码以在超链接的锚文本中包含“发布到”文本吗?非常感谢! :)

Hey guys, I have a pretty basic question (even though I know the title sounds confusing) which is best explained as follows. This code is what I currently have:

<?php if (!is_page()): ?><?php ob_start(); ?><?php printf(__('Posted to %s', 'kubrick'), get_the_category_list(', ')); ?>

Which generates this HTML code:

Posted to <a href="http://www.example.com/category/" title="View all posts in Category" rel="category tag">Category</a>

As you can see, the category is 'linkified' by this code, however I would like the entire phrase to be the anchor text for the link, like this:

<a href="http://www.example.com/category/" title="View all posts in Category" rel="category tag">Posted to Category</a>

How would I modify the original PHP code to include the "Posted to" text in the hyperlink's anchor text? Much appreciated! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-11-12 09:47:15

您可以使用 get_the_category()get_category_link() 函数,如下所示:

<?php
    foreach(get_the_category() as $category)
        echo '<a href="'.get_category_link($category->cat_ID).'" title="View all posts in Category" rel="category tag">Posted to '.$category->cat_name.'</a> ';
?>

但是,您可能想考虑当帖子属于多个类别时会发生什么:您的原始代码将显示以逗号分隔的类别链接列表(例如“Posted in foo, bar, qux”),而此代码将为每个类别显示“Posted in [name]”(例如“Posted in foo Posted in bar Posted in qux”) )。

还应该注意的是,这个答案没有使用 WordPress 的 __() 在适当的情况下翻译结果文本的功能:您可能想考虑如何最好地支持非英语使用者。

另外,没有必要在每个连续命令之后关闭并重新打开 PHP 标签 - 只需用分号分隔它们即可。

You can use get_the_category() and get_category_link() functions as follows:

<?php
    foreach(get_the_category() as $category)
        echo '<a href="'.get_category_link($category->cat_ID).'" title="View all posts in Category" rel="category tag">Posted to '.$category->cat_name.'</a> ';
?>

However you may like to consider what happens when a post is in more than one category: your original code will show a list of category links separated by commas (e.g. "Posted in foo, bar, qux"), whereas this code will show "Posted in [name]" for each category (e.g. "Posted in foo Posted in bar Posted in qux").

It should also be noted that this answer does not use WordPress's __() function to translate the resulting text where appropriate: you may like to consider how best to support non-English speakers.

Also, it's not necessary to close and reopen PHP tags after each contiguous command - just separate them with semicolons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文