Wordpress 从描述中获取类别
Wordpress get category from description
我想从类别描述中获取类别 ID。我以编程方式添加类别和帖子。如果类别不存在,我的脚本会添加类别。我从面板更改类别名称和 slug。所以我的脚本每次都添加类别。
像这样:
$categoryDescription = 'bla bla';
$category = get_category_by_description($categoryDescription);
echo $category['name'];
没有对此进行测试,但尝试将以下功能添加到主题中的 functions.php:
function get_category_by_description($categoryDescription) {
global $wpdb;
$res = $wpdb->get_results("
select
t.slug
from
{$wpdb->prefix}terms t,
{$wpdb->prefix}term_taxonomy tx
where
t.term_id = tx.term_id and
tx.description = '{$categoryDescription}'
");
if (!empty($res)) {
return get_category_by_slug($res[0]->slug);
}
return null;
}
那么你应该可以做到:
$categoryDescription = 'bla bla';
$category = get_category_by_description($categoryDescription);
echo $category->name;
我想从类别描述中获取类别 ID。我以编程方式添加类别和帖子。如果类别不存在,我的脚本会添加类别。我从面板更改类别名称和 slug。所以我的脚本每次都添加类别。
像这样:
$categoryDescription = 'bla bla';
$category = get_category_by_description($categoryDescription);
echo $category['name'];
没有对此进行测试,但尝试将以下功能添加到主题中的 functions.php:
function get_category_by_description($categoryDescription) {
global $wpdb;
$res = $wpdb->get_results("
select
t.slug
from
{$wpdb->prefix}terms t,
{$wpdb->prefix}term_taxonomy tx
where
t.term_id = tx.term_id and
tx.description = '{$categoryDescription}'
");
if (!empty($res)) {
return get_category_by_slug($res[0]->slug);
}
return null;
}
那么你应该可以做到:
$categoryDescription = 'bla bla';
$category = get_category_by_description($categoryDescription);
echo $category->name;