Support » Themes and Templates » wp_list_bookmarks not enough?!

  • Resolved alexleonard

    (@alexleonard)


    Hi there,

    I’ve been really tinkering with the list bookmarks function in the last while. I was delighted to work out that I could add enough code through sensible use of title_after and after to get my description wrapped in a span for the purposes of styling.

    However on a new site I’m working on as well my friend wants to have a highly structured list page.

    The structure was intended as follows:

    <h3>category_name<h3>
    <ul>
     <li><img src="link_image" />
      <h4>link_name(not linking though)</h4>
      <p>Description</p>
      <p><a href="link_url">link_title</a></p>
     </li>
    </ul>

    and so on ad infinitum.

    In my investigation of things I could see no way that wp_list_bookmarks could do this for me.

    The biggest problem was that show_images=1 resulted in the link_name being replaced by the image and this then acted as the link. This would not fit into the overall style of the site.

    In addition there was no way to display the link_url separately as a link and not have the link_name as the link.

    So.. I went on a bit of a quest and came up with a solution. I’m not the best php/MySQL coder in the world and the resulting code feels messy to me. I was wondering could anyone have a look at this and tell me if I’m going a very long way around and whether this way of coding things results in excessive database calls or anything?

    <?php
    
    $defaults = array('orderby' => 'name', 'order' => 'ASC', 'limit' => -1, 'category' => '',
    	'category_name' => '', 'hide_invisible' => 1, 'show_updated' => 0, 'echo' => 1,
    	'categorize' => 1, 'title_li' => __('Bookmarks'), 'title_before' => '<h2>', 'title_after' => '</h2>',
    	'category_orderby' => 'name', 'category_order' => 'ASC', 'class' => 'linkcat',
    	'category_before' => '<li id="%id" class="%class">', 'category_after' => '</li>');
    	extract($defaults);
    $cats = get_categories("type=link&category_name=$category_name&include=$category&orderby=$category_orderby&order=$category_order&hierarchical=0");
    
    // Begin foreach loop 1 displaying the categories
    foreach ( (array) $cats as $cat ) {
    	$params = array_merge($defaults, array('category'=>$cat->cat_ID));
    	$bookmarks = get_bookmarks($params);
    	if ( empty($bookmarks) )
    		continue;
    	$catname = apply_filters( "link_category", $cat->cat_name );
    	$cat_ID = apply_filters( "link_category", $cat->cat_ID );
    	echo '<h3>' . $catname . '</h3>';
    
    	// Work out which links are in each listed category!!
    	$link_IDs = $wpdb->get_results("SELECT link_id FROM $wpdb->link2cat WHERE category_id=$cat_ID");
    	foreach ($link_IDs as $link_ID) {
    		$ID = substr($link_ID->link_id,0,1000); // This worked, but I'm not sure why. I turned the id into a string instead of an array?
    		$last = "$ID";
    		$loop .= " OR link_id=$last";
    	}		
    
    $links = $wpdb->get_results("SELECT * FROM $wpdb->links WHERE link_id=$last$loop ORDER BY link_name");
    echo '<ul>';
    foreach ($links as $link) {
    	echo '<li>';
    	$img_name = substr($link->link_image,0,4); // If there is an image this will set img name as "http"
    	if (($img_name)==("http")) {echo '<p><img src="' . $link->link_image . '" alt="' . $link->link_name . ' website" /></p>';} // If there is an image, display it
    	echo '<h4>' . $link->link_name . '</h4>';
    	echo '<p>' . $link->link_description . '</p>';
    	echo '<p class="clear">Visit this site: <a href="' . $link->link_url . '">' . $link->link_url . '</a></p></li>';
    
    } // end of link listing loop
    echo '</ul>';
    // Return the link id variables to null
    $last=null;
    $loop=null;
    // return to start of category loop. If no more cateogries continue
    } // end category loop
    ?>

    I hope this makes sense. It was the only way I could come up with a solution and I’m sure there is a more graceful way. Anyone have any thoughts?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter alexleonard

    (@alexleonard)

    By the way, I have a feeling my lack of understanding of arrays was part of my workaround here.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Eww! Direct database calls! Nasty! Also, it looks like you’re doing a lot of unnecessary work here.

    Correct me if I’m wrong, but it looks like you want to display something like this:

    <h3>category_name<h3>
    <ul>
     <li><img src="link_image" />
      <h4>link_name(not linking though)</h4>
      <p>Description</p>
      <p><a href="link_url">link_title</a></p>
     </li>
     <li>.. same for each link in this category...</li>
    </ul>
    <h3>.. same thing for next category, and so on..

    Right? Just display all the links, sort of thing? Okay. Lets write this in a much simpler way.

    First, we need the categories. We’ll use get_categories() for this.
    $cats = get_categories("type=link&hierarchical=0");

    That will get the link cats for us. Now we loop through them:

    foreach ($cats as $cat) {
    echo '<h3>'.$cat->cat_name.'</h3>';
    echo '<ul>';

    Okay, so we now need the bookmarks for that cat. We use get_bookmarks() for that.
    $books = get_bookmarks("category=$cat->cat_ID");

    And we loop through those, displaying what we need:

    foreach ($books as $book) {
    echo '<li>';
    if (substr($book->link_image,0,4) == 'http') {
    echo '<p><img src="'.$book->link_image.'" alt="'.$book->link_name.' website" /></p>';
    }
    echo '<h4>'.$book->link_name.'</h4>';
    echo '<p>'.$book->link_description.'</p>';
    echo '<p class="clear">Visit this site: <a href="'.$book->link_url.'">'.$book->link_name.'</a></p>';
    echo '</li>';
    } // end books loop

    And we finish up:

    echo '</ul>';
    } // end cats loop;

    And that’s it. The total code follows:

    $cats = get_categories("type=link&hierarchical=0");
    foreach ($cats as $cat) {
    echo '<h3>'.$cat->cat_name.'</h3>';
    echo '<ul>';
    $books = get_bookmarks("category=$cat->cat_ID");
    foreach ($books as $book) {
    echo '<li>';
    if (substr($book->link_image,0,4) == 'http') {
    echo '<p><img src="'.$book->link_image.'" alt="'.$book->link_name.' website" /></p>';
    }
    echo '<h4>'.$book->link_name.'</h4>';
    echo '<p>'.$book->link_description.'</p>';
    echo '<p class="clear">Visit this site: <a href="'.$book->link_url.'">'.$book->link_name.'</a></p>';
    echo '</li>';
    } // end books loop
    echo '</ul>';
    } // end cats loop;

    Done and done. Now, there may be bugs in this, since I wrote this on the fly and haven’t tested it. But they’re probably typos. 🙂

    Thread Starter alexleonard

    (@alexleonard)

    Brilliant! I had a feeling I was going a long way around and I think I understand a little better how Word Press works now.

    Thanks a lot. I’ll go and try to implement this now and get back to you if I have any troubles.

    Cheers.
    Alex

    Thread Starter alexleonard

    (@alexleonard)

    Deadly. That worked first time out! Not a typo in sight 🙂

    A much leaner tidier way to go about things – from 42 lines of messy code to 16 lines of sweetness.

    Thanks a lot. It’s given me some ideas for some other dynamic areas of the site I’m working on as well. I’m looking forward to getting it up on the web and showing it off 😉

    Awesome, a day of googling didn’t get me anywhere (mostly because I was searching for the old deprecated functions)…this was just what I needed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_list_bookmarks not enough?!’ is closed to new replies.