• I’m using get_bookmarks instead of wp_list_bookmarks because I need to be able to display the notes field, and I have it working but can’t figure out how to segment the list by categories. I could hard-code in a category name or ID, but I’d rather structure this so that if in the future more links categories are added the list will automatically update with the new category and sub-list of bookmarks.

    This is what I’m using that works but doesn’t segment by category:

    $bookmarks = get_bookmarks( array(
    	'orderby'        => 'name',
    	'order'          => 'ASC',
    ));
    foreach ( $bookmarks as $bookmark ) {
        printf( '<li><a href="%s">%s</a>',$bookmark->link_url, $bookmark->link_name );
            if(!($bookmark->link_notes == "")) {
    	       printf( '<br /><span class="linksnotes">');
    		   printf(  $bookmark->link_notes ); }
    	printf( '<span></li>' );
    }

    I have also a Custom Post Type that prints posts by the CPT custom category, so I tried modifying that to work with link categories, but it doesn’t work – it prints the category name, followed by the entire list of links, followed by the next category name and again the entire list of links….

    $tax = 'link_category';
    $tax_terms = get_terms($tax);
    
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        echo '<h2>'. $tax_term->name .'</h2>';
        $bookmarks = get_bookmarks($args);
        $args = array(
          'category_name' => $tax_term->name,
    	  'orderby' => 'name',
    	  'order' => 'ASC',
        );
          foreach ( $bookmarks as $bookmark ) {
                printf( '<li><a href="%s">%s</a>',$bookmark->link_url, $bookmark->link_name );
                   if(!($bookmark->link_notes == "")) {
    	          printf( '<br /><span class="linksnotes">');
    		      printf(  $bookmark->link_notes ); }
    	       printf( '<span></li>' );
        }
       }
    }

    Can anyone help me figure out what I’m doing wrong?

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

    (@bcworkz)

    The nested loops look pretty close to doing what you want.

    For the first category the reason you get a full list of bookmarks is $args is not yet defined. It is defined after the get_bookmarks() call instead of before.

    Which means the next category list should have had the bookmarks from the previous category. For some reason, the $args query parameters are not working, but I don’t see why not ??

    Instead of beating ourselves up trying to figure out the problem, it may be more expedient to try a different approach. I would try specifying the $args query parameters using the term ID ($tax_term->.term_id) for the ‘category’ key instead of category name.

    Thread Starter TrishaM

    (@trisham)

    SUCCESS!

    Thank you very much bcworkz, moving $args to above the get_bookmarks() worked….I can’t believe I didn’t see that…..jeeeesh……it works using category_name but I like the cleanliness of using term_id instead, so that’s how I have it now.

    In case anyone else is looking to do this, this code creates a list of bookmarks listed by Category, but unlike wp_list_bookmarks() this code lets me display the “Notes” field in order to show more information for each link in addition to using the description field.

    Here’s the working code:

    $tax = 'link_category';
    $tax_terms = get_terms($tax);
    
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        echo '<h2>'. $tax_term->name .'</h2>';
        $args = array(
          'category' => $tax_term->term_id,
    	  'orderby' => 'name',
    	  'order' => 'ASC',
        );
        $bookmarks = get_bookmarks($args);
          foreach ( $bookmarks as $bookmark ) {
                printf( '<li><a href="%s">%s</a>',$bookmark->link_url, $bookmark->link_name );
                   if(!($bookmark->link_notes == "")) {
    	          printf( '<br /><span class="linksnotes">');
    		      printf(  $bookmark->link_notes ); }
    	       printf( '<span></li>' );
        }
       }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom bookmarks list by category’ is closed to new replies.