Dropdown Box
Here's how to create a dropdown box of the subcategories of, say, a category that archives information on past events. This mirrors the example of the dropdown example of wp_get_archives which shows how to create a dropdown box for monthly archives.
Suppose the category whose subcategories you want to show is category 10, and that its category "nicename" is "archives".
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo esc_attr(__('Select Event')); ?></option>
<?php
$categories = get_categories('child_of=10');
foreach ($categories as $category) {
$option = '<option value="/category/archives/'.$category->category_nicename.'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>
List Categories and Descriptions
This example will list in alphabetic order, all categories presented as links to the corresponding category archive. Each category descripition is listed after the category link.
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>'; }
?>
Get only top level categories
To get the top level categories only, set parent value to zero. This example gets link and name of top level categories.
<?php
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
}
?>
Source: https://codex.wordpress.org/Function_Reference/get_the_category_list
WordPress is an effective method for web design and web development because it provides a creative and unique look to the website. That’s fantastic.
ReplyDelete