WordPress.org

Ready to get started?Download WordPress

Forums

Links Page Template, multicolumn & categorized (1 post)

  1. Annekee
    Member
    Posted 2 years ago #

    I was looking for a way to get the links (bookmarks) on a page in columns under several link categories, but couldn't find it, so I figured it out myself. The idea is:

    Page Title

    Category 1
    LinkCat1 LinkCat1 LinkCat1
    LinkCat1 LinkCat1 LinkCat1, etc.

    Category 2
    LinkCat2 LinkCat2 LinkCat2
    LinkCat2 LinkCat2 LinkCat2, etc.

    Page template:

    <?php
    /*
    Template Name: Links Page
    */
    ?>
    <?php get_header(); ?>
    
    <div id="content">
    	<h2><?php the_title(); ?></h2>
    	<div class="entry">
    
    <table width="100%" align="left" border="0" cellspacing="0" cellpadding="0">
    
    <?php
    $showlinks = '';
    $cols = 3; // set the number of cols
    $taxonomy = 'link_category';
    $args ='';
    $terms = get_terms( $taxonomy, $args );
    if ($terms) {
    	foreach($terms as $term) {
    		if ($term->count > 0) {
    			$bookmarks = get_bookmarks( array(
    							'orderby'        => 'name',
    							'order'          => 'ASC',
    							'category_name' => $term->name) );
    			$showlinks .= '<tr><td colspan="' . $cols . '"><h4>' . $term->name . '</h4></td></tr>' . "\n";
    
    			$i = 0; // set counter to zero
    				$showlinks .= '<tr>' . "\n";
    			// loop through our returned bookmarks
    			foreach ($bookmarks as $bm) {
    				$i++; // increment counter
    				$showlinks .= '<td width="33%">';
    			 	$showlinks .= sprintf( '<a class="relatedlink" href="%s">%s</a><br />', $bm->link_url, __($bm->link_name) );
    				$showlinks .= '</td>' . "\n";
    
    				// if counter is dividable by the number of cols, close the row
    				if ( ( $i % $cols ) == 0 ) {
    					$showlinks .= '</tr>' . "\n";
    					$showlinks .= '<tr>' . "\n";
    
    				}
    			}
    			$showlinks .= '</tr>' . "\n"; // close initial (or last) row
    			// space between categories:
    			$showlinks .= '<tr><td colspan="3">&nbsp;</td></tr>' . "\n";
    		}
     	}
    }
    echo $showlinks;
    ?>
    </table>
    	</div>
    </div>
    
    <?php get_sidebar("page"); ?>
    <?php get_footer(); ?>

    You can adjust the args for get_terms and get_bookmarks to your liking, and of course style the table. Enjoy!

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by Annekee
  • This topic is not a support question
  • WordPress version: 3.2.1