我正在尝试在插件选项页面中获取帖子类别名称和ID的值。但是循环未在foreach循环中获取。如何获得。我的代码在下面,谢谢您的帮助.....
<?php
/*
Plugin Name: Sql Query For Category
Plugin URI:
Description:
Author: Nayeem Hyder
Version: 1.00
Author URI: http://nayeemriddhi.info
*/
function myplugin_register_options_page() {
add_options_page('Page Title', 'Plugin Menu', 'manage_options', 'myplugin', 'myplugin_options_page');
}
add_action('admin_menu', 'myplugin_register_options_page');
function myplugin_options_page()
{
echo 'string';
$categories = get_the_category();
foreach($categories as $category){
echo $category->name; //category name
$cat_link = get_category_link($category->cat_ID);
echo '<a href="'.$cat_link.'">'.$category->name.'</a>'; // category link
}
}
答案 0 :(得分:0)
get_the_category()检索与发布对象相关的类别。 从您的代码看来,您应该使用get_terms()。您将为它传递一个分类法(例如“ category”或“ post_tag”),它将返回该列表:
https://developer.wordpress.org/reference/functions/get_terms/