0

I wrote a shortcode that I am trying to refine. It basically just gets a list of bookmarks and prints them. But I need to to add HTML around it depending on the type that is defined.

extract(shortcode_atts(array('cat' => '', 'type' => ''), $atts));

$bookmarks = get_bookmarks( array(
  'orderby'  => 'name',
  'order'    => 'ASC',
  'category' => $cat
));

if (is_single()){
  if($type == 'audio'){echo '<div class="sub-audio"><h6 class="sub_btn">Audio</h6>';}
  if($type == 'video'){echo '<div class="sub-video"><h6 class="sub_btn">Video</h6>';}
  foreach ( $bookmarks as $bookmark ) { 
    printf( '<a class="wpb_button_a" href="%s"><span class="wpb_button wpb_%s wpb_default2"><i class="icon-%s"> </i>%s</span></a>', $bookmark->link_url, $bookmark->link_name, $bookmark->link_name, $bookmark->link_name );
  }
  echo '</div>';
}

So depending on whether I define the type as either audio or video it should output HTML according to the type. But I am not sure why this isn't working.

Any help would be appreciated.

Thanks.

2

I don't know what "this isn't working" means but a shortcode should not echo anything. You should concatenate a string and return it.

if (is_single()){
  $str = '';
  if($type == 'audio'){
    $str .= '<div class="sub-audio"><h6 class="sub_btn">Audio</h6>';
  }
  if($type == 'video'){
    $str .= '<div class="sub-video"><h6 class="sub_btn">Video</h6>';
  }
  foreach ( $bookmarks as $bookmark ) { 
    $str .= sprintf( 
      '<a class="wpb_button_a" href="%s"><span class="wpb_button wpb_%s wpb_default2"><i class="icon-%s"> </i>%s</span></a>', 
      $bookmark->link_url, 
      $bookmark->link_name, 
      $bookmark->link_name, 
      $bookmark->link_name 
    );
  }
  $str .= '</div>';
}
return $str;

I don't see anything else obviously wrong, though the code you posted is incomplete, so I am guessing that that is the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.