{/'title/':/'WordPress Query Loop: Get Parent Categories Only/',/'description/':/'Learn how to fetch parent level categories in a WordPress query loop using the get_categories function with the 'parent' parameter. This guide provides a code example and explains how to access category properties./',/'keywords/':/'wordpress, query loop, categories, parent categories, get_categories, php, code example/',/'content/':/'To get the categories of posts in a WordPress query loop and only retrieve the parent level categories, you can use the get_categories function with the parent parameter set to 0. Here//'s an example://n//nphp//n/$args = array(//n 'orderby' => 'name',//n 'parent' => 0, // Limit to parent level categories only//n);//n/$categories = get_categories(/$args);//n//nforeach (/$categories as /$category) {//n echo /$category->name;//n // You can also access other category properties like /$category->slug, /$category->term_id, etc.//n}//n//n//nThis code will retrieve all the parent level categories and loop through them to display their names. You can modify the foreach loop to suit your needs, such as displaying category links or performing other operations with the categories.//n/'}'}

WordPress Query Loop: Get Parent Categories Only

原文地址: https://www.cveoy.top/t/topic/qCNL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录