I am trying to get value of Post Category Name and ID in my plugin option Page.But loop is not retrieving the within the foreach loop. How to get that. My code is below, Thanks for help.....
<?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
}
}
get_the_category() retrieves categories related to a post object. From your code it seems you should use get_terms(). You'll pass it a taxonomy (like 'category' or 'post_tag') and it will return the list:
https://developer.wordpress.org/reference/functions/get_terms/