31 Jul 2009iNove 分类显示友链
数据库中分类的存储结构:
可以看出,我们只需要在 terms 表中出去相应数据元组对应的 name 属性就 OK 了。 所以这里需要通过 term_id 和 terms_taxonomy 表联一下,并且将 taxonomy 的值限定为 ‘link_category’。SQL 如下:
1 2 3 4 5 | SELECT T1.name AS name FROM $wpdb->terms T1, $wpdb->term_taxonomy T2 WHERE T1.term_id = T2.term_id AND T2.taxonomy = 'link_category' |
通过以上 SQL 我们可以将所有链接分类的名字找出来,然后做一个循环处理,把链接按照分类查找出来,并置于页面中。
将 links.php 中原代码:
1 2 3 4 5 | <div class="boxcaption"><h3><?php _e('Blogroll', 'inove'); ?></h3></div> <div class="box linkcat"> <ul><?php wp_list_bookmarks('title_li=&categorize=0&orderby=rand'); ?></ul> <div class="fixed"></div> </div> |
替换为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php $linkcats = $wpdb->get_results("SELECT T1.name AS name FROM $wpdb->terms T1, $wpdb->term_taxonomy T2 WHERE T1.term_id = T2.term_id AND T2.taxonomy = 'link_category'"); if($linkcats) : foreach($linkcats as $linkcat) : ?> <div class="boxcaption"><h3><?php echo $linkcat->name; ?></h3></div> <div class="box linkcat"> <ul> <?php $bookmarks = get_bookmarks('orderby=rand&category_name=' . $linkcat->name); if ( !empty($bookmarks) ) { foreach ($bookmarks as $bookmark) { echo '<li><a href="' . $bookmark->link_url . '" title="' . $bookmark->link_description . '">' . $bookmark->link_name . '</a></li>'; } } ?> </ul> <div class="fixed"></div> </div> <?php endforeach; endif; ?> |
原显示方式:
新显示方式:
Til next time,
Jason at 00:00