Home

PHP Programming to get Custom Category list in Dropdown from Shortcode

PHP Programming to get Custom Category list in Dropdown from Shortcode
Like Tweet Pin it Share Share Email

[myad1] WordPress is providing default Widget to get the default category drop-down. If you have a custom category in your blog or website then you have to add a plugin or action hooks in the functions.php.

Below is the code which will help you to show the custom category list.  We are using Solutions custom post type and platforms as a taxonomy.  Created a shortcode to get the get the all Platforms list in the dropdown using below code.

<?php

	$args = array( 
 		'taxonomy' => 'platforms',
 		'title_li' => '',
 		'orderby'    => 'name',
        'show_count' => true
 	);
 	// We wrap it in unordered list 
 	?>
 	<ul><?php echo wp_list_categories($args); ?> </ul>

?>

Copy above code and paste in the template where you want to show the category list. If you want to use the shortcode then, please use below code and use solutions_caregories as a shortcode and place in the sidebar.

function list_terms_custom_taxonomy( $atts ) {
	ob_start();
	// Inside the function we extract custom taxonomy parameter of our shortcode
    extract( shortcode_atts( array(
        'custom_taxonomy' => 'platforms',
    ), $atts ) );
 	// arguments for function wp_list_categories
 	$args = array( 
 		'taxonomy' => $custom_taxonomy,
 		'title_li' => '',
 		'orderby'    => 'name',
        'show_count' => true
 	);
 	// We wrap it in unordered list 
 	?>
 	<ul><?php echo wp_list_categories($args); ?> </ul>
 	<?php
 	$output = ob_get_contents();   
	ob_end_clean();   
	return $output;
}
 
// Add a shortcode that executes our function
add_shortcode( 'solutions_caregories', 'list_terms_custom_taxonomy' );

In the above example, we have shown the post counts. If you don’t want  to show the count then replace ‘show_count’ => true to ‘show_count’ => false.

Please do let me know if you have any queries on this and write your thoughts via comments.

 

Thanks for reading our article. What Next?

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

three × two =