Trouble calling Category ID
-
Hello forums,
I’ve been scouring support posts and google trying to find a solution to this, and I can’t figure it out. I’m an amateur coder, so I learn as I go along, and I might have something mixed up. Thanks, in advance, for your help.
I am trying to call either a category id or a category slug for an href link. Everything I’ve tried has given me either no response or a failure (0). Here is what the code is doing:
Picking a category through the widget panel:
<p> <label for="<?php echo $this->get_field_id('cat01'); ?>">1st Category:</label> <?php wp_dropdown_categories('hide_empty=0&hierarchical=1&id='.$this->get_field_id('cat01').'&name='.$this->get_field_name('cat01').'&selected='.$instance['cat01']); ?> </p>
Then,
$instance['cat01'] = $new_instance['cat01'];
Then, extracting through the widget function:
$cat01 = $instance['cat01'];
Then, I retrieve the posts with global $wpdb, and then:
$posts01 = get_posts('numberposts=4&category='.$cat01);
After that, inside of a ul:
$out .= '<li class="active"><a href="#'.get_cat_name($cat01).'" data-toggle="tab">'.get_cat_name($cat01).'</a></li>';
This is where the main problem seems to be. I can pull the category name fine through the above code, but when I replace that with get_cat_id, or any other alternative, it doesn’t show the ID. It either shows a ‘0’, or doesn’t show anything at all.
I’m coding a tabbable widget, so the href needs to be in either slug form, or as an id. The content panes don’t respond correctly with it as ‘name’ because of the additional spaces and special characters.
Please let me know if you need to see any additional code. I can definitely place more.
The foreach loop begins after the ul ends. It looks like this:
$out .= '<div id="tab1Content" class="tab-content">'; $out .= '<div class="tab-pane fade in active" id="'.get_cat_name($cat01).'">'; $out .= '<ul class="thumbnails">'; foreach($posts01 as $post) {
Also, it shouldn’t matter, but this is being built with bootstrap. So, the classes all correspond to that. Thanks!
- The topic ‘Trouble calling Category ID’ is closed to new replies.