wordpress 通过 id 链接到自定义分类法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10105951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
link to custom taxonomy by id
提问by JorgeLuisBorges
Through a series of specific requirements, I find myself needing to link to a custom taxonomy category using its term id...
通过一系列特定要求,我发现自己需要使用术语 id 链接到自定义分类法类别...
I've got this - which displays a link to all taxonomies - I wish to change it so it only displays a link to the taxonomy with the term id dynamically pulled from a custom field I'm using.
我有这个 - 显示所有分类法的链接 - 我想改变它,所以它只显示分类法的链接,其中术语 id 从我正在使用的自定义字段中动态提取。
$taxonomy = 'event-categories';
$terms = get_terms($taxonomy);
if ($terms) {
foreach($terms as $term) {
echo '<li><a href="http:/mysite.com/events/categories/project-events/' . $term->slug . '">' . $term->name .'</a></li>';
}
};
essentiall I need "link_to_taxonomy_category(x)" where x = term_id
本质上,我需要“link_to_taxonomy_category(x)”,其中 x = term_id
Thanks
谢谢
回答by gradyetc
The function you are looking for is get_term_link
. It takes either a term object, ID or slug and a taxonomy name and returns a URL to the term landing page.
您正在寻找的功能是get_term_link
. 它接受一个术语对象、ID 或 slug 和一个分类名称,并返回一个指向术语登录页面的 URL。
As a side note hard coding the link as you have in the example above is fragile -- always keep your code as portable as possible. If the site is moved to a different domain, that link will break. WordPress has several functions that generate links dynamically based on the current installation environment. get_term_link
is one example.
作为旁注,对上面示例中的链接进行硬编码是脆弱的 - 始终保持您的代码尽可能可移植。如果站点被移动到不同的域,该链接将中断。WordPress 有几个功能可以根据当前的安装环境动态生成链接。 get_term_link
就是一个例子。
From the Codex:
来自法典:
$terms = get_terms('species');
echo '<ul>';
foreach ($terms as $term) {
echo '<li><a href="'.get_term_link($term->slug, 'species').'">'.$term->name.'</a></li>';
}
echo '</ul>';
回答by Aamer Shahzad
If you have single term_id
e.g: 10
, custom taxonomy series
then you can use the following code to get the taxonomy term link.
如果您有单个term_id
eg: 10
,自定义分类法,series
那么您可以使用以下代码获取分类法术语链接。
note : change 10 to your variable for term_id and 'series' to your taxonomy.
注意:将 10 更改为 term_id 的变量,并将“系列”更改为您的分类法。
$term = get_term( 10, 'series' );
$term_link = get_term_link( $term );
echo '<a href="' . $term_link . '">View All</a>';