selected option if current category is the value

I want to make a categories list of this site: https://premium.wpmudev.org/blog/

So I make this code:

<select class=”select”>
<?php
$args = array(
‘orderby’ => ‘name’,
‘order’ => ‘ASC’
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo ‘<option value=”‘.esc_url( get_category_link( $cat->term_id ) ).'” ‘.selected( esc_url( get_category_link( $cat->term_id ) ) ).’>’.$cat->name.'</option>’;
}
?>
</select>

The code is working and redirects me to the category link that I selected but the problem that after I redirect to the category the category option inside the categories select list is not changed to the current category.

The problem is here: selected( esc_url( get_category_link( $cat->term_id ) )

but i can’t solve it.

Read more here:: selected option if current category is the value

Leave a Reply

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