• Resolved m

    (@pelican_brown)


    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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The name should work in an href if it is urlencoded first, then spaces and special characters are properly escaped. The category ID is the ID used by the DB, probably not the value assigned to a html tag id attribute. What is assigned to a html tag is template dependent. Even the name attribute may not necessarily correspond to the category name in the DB.

    To be sure, examine the template code that outputs the target html tag’s id attribute and use the same technique to generate your skip links.

    Thread Starter m

    (@pelican_brown)

    Thanks for the response, bcworkz, and for pointing me towards urlencode. I did try that, and it was definitely the right direction, but my tab ids weren’t cooperating. It did lead me towards a solution, though, which was preg_replace.

    Here’s what I did:

    $safename02 = preg_replace('/[\s\W]+/','-',get_cat_name($cat02));
    // Strip off spaces and non-alpha-numeric
    $safename02 = strtolower($safename02);

    Then:

    $out .= '<li><a href="#'.$safename02.'" data-toggle="tab">'.get_cat_name($cat02).'</a></li>';

    And, also:
    $out .= '<div class="tab-pane fade" id="'.$safename02.'">';

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trouble calling Category ID’ is closed to new replies.