wordpress - wordpress - 如何在functions.php中获取当前术语
问题描述
我想获得函数的当前术语。但显然这个功能在functions.php
. 有解决这个问题的想法吗?
$term = get_term_by('name', get_query_var('download_tag'), 'download_tag' );
解决方案
<?php
/**
* getCurrentTerm
* get the current term which archive is being displayed
* good for categories, tags and custom taxonomies
* @author Ohad Raz
* @return current term object or false is not in a term archive.
*/
function getCurrentTerm(){
if (!is_category() && !is_tag() && !is_tax())
return false;
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
return get_term_by( 'slug', $term_slug, $taxonomyName );
}
推荐阅读
- bash - 厨师资源,“not_if”中的脚本不起作用
- linux - 检查子 URL 的状态是否随 Bash 改变
- javascript - 用 Ramda 统一对象的信息
- python - 访问 pandas 系列中的第一个列表项
- python - 在查询集中返回 Django Model ChoiceField 标签
- c# - 使用 Diffie Hellman 密钥交换和使用 C# 的 Aes 进行客户端/服务器通信
- python - Random.shuffle 将重复列表中的元素
- linux - Kubernetes Minikube:如何使 Grafana 实例对 Intranet 可见?
- reactjs - 老服务员不放手
- r - 当日期范围更改时,`valueBox` 中的值不会更改