今天逛了一下李学江大神的博客,发现这个功能特别不错,于是转载过来了跟大家分享。
今天终于抽出时间,把代码和教程分享给大家。我所构建的网址导航主要分为以下几个模块:访客网站自动排名第一模块、知名博客模块、人气博客模块、博客大全模块、边栏模块。
下面分模块给大家介绍:
一、访客网站自动排名第一模块
通过调取博客留言者的名字和网址,并去重,按照留言时间顺序排列,最近代链接的留言排在第一位(留言者不填写链接则不显示)
1、把以下代码放入到所用主题的functions.php最后一个%>。
- //最新评论排第一
- function Autofirst($af){
- global $wpdb;
- $queryaf=“select comment_author, comment_author_url, comment_date from $wpdb->comments where comment_ID in (select max(comment_ID) from $wpdb->comments where comment_approved=’1′ and comment_author_url !=” and user_id=’0′ GROUP BY comment_author_email) ORDER BY comment_date DESC LIMIT $af”;
- $wally = $wpdb->get_results($queryaf);
- foreach ($wally as $commentaf){
- $tmpy= “<li><a target=\”_blank\” href=\””.$commentaf->comment_author_url.“\”>”.$commentaf->comment_author.“</a></li>”;
- $outputy .= $tmpy;
- }
- $outputy = “<ul>”.$outputy.“</ul>”;
- echo $outputy ;
- }
其中输出的样式可以自定义,毕竟每一个博客的样式都不一样,这个就靠自己发挥了,只需要在以上代码中的<ul>中添加一个class属性即可。
2、把以下代码放入需要显示最新访客的地方,如留言板。
- <?php Autofirst(100);?>
其中100表示显示100个最新访客,这个数值可以自定义,一定不能为空,要不然会出错,最起码要大于0以上。
二、知名博客模块
显示一定时期内在本站留言比较活跃的用户网站。
- <?php top_comment_authors(24); ?>
三、人气博客
显示一定时期内在本站留言数最多的用户网站。
- <?php top_comments(24); ?>
四、博客大全
调用你网站的友情链接,由于主题差异,每个网站的调用方法可能不尽相同,如果不懂的话,你可以资讯主题作者。
五、边栏
调用主题边栏,在后台小工具中添加你想要显示的内容。
PS:
1、如果你正在使用知更鸟Begin主题,以上代码全部适用,非知更鸟Begin主题,通过本教程你只能实现最近访客访客自动排名第一功能,如果感兴趣,你也可以参照这篇文章:《WordPress博客如何利用最新评论实现类似导航网站自动排第一功能》。
2、各位在使用的时候,会发现知名博客和人气博客显示的内容与本站有些诧异,如果你希望和本站显示的一样,你可以找到/wp-content/themes/begin/inc/inc.php文件,将约2013至1268行代码替换成以下代码。
- // 读者排行
- function top_comment_authors($amount = 98) {
- global $wpdb;
- $prepared_statement = $wpdb->prepare(
- ‘SELECT
- COUNT(comment_author) AS comments_count, comment_author, comment_author_url, comment_author_email, MAX( comment_date ) as last_commented_date
- FROM ‘.$wpdb->comments.’
- WHERE comment_author != “” AND comment_type = “” AND comment_approved = 1 AND user_id = “”
- GROUP BY comment_author
- ORDER BY comments_count DESC, comment_author ASC
- LIMIT %d’,
- $amount);
- $results = $wpdb->get_results($prepared_statement);
- $output = ‘<div class=“top-comments”>’;
- foreach($results as $result) {
- $c_url = $result->comment_author_url;
- $output .= ‘
- <div class=“lx8”>
- <div class=“top-author”>
- <div class=“top-comment”><a href=“‘ . get_template_directory_uri().”/inc/go.php?url=“. $c_url . ‘” target=“_blank” rel=“external nofollow“><div class=“author-url”><strong> ‘ . $result->comment_author . ‘</div></strong></a></div>
- <div class=“top-comment”>’ . human_time_diff(strtotime($result->last_commented_date)) . ‘前来过</div>
- </div>
- </div>’;
- }
- $output .= ‘<div class=“clear”></div></div>’;
- echo $output;
- }
- function top_comments($number = 98) {
- global $wpdb;
- $counts = wp_cache_get( ‘mostactive’ );
- if ( false === $counts ) {
- $counts = $wpdb->get_results(“SELECT COUNT(comment_author) AS cnt, comment_author, comment_author_url, comment_author_email
- FROM {$wpdb->prefix}comments
- WHERE comment_date > date_sub( NOW(), INTERVAL 90 DAY )
- AND comment_approved = ‘1’
- AND comment_author_email != ‘example@example.com’
- AND comment_author_email != ”
- AND comment_author_url != ”
- AND comment_type = ”
- AND user_id = ‘0’
- GROUP BY comment_author_email
- ORDER BY cnt DESC
- LIMIT $number”);
- }
- $mostactive = ‘<div class=“top-comments”>’;
- if ( $counts ) {
- wp_cache_set( ‘mostactive’, $counts );
- foreach ($counts as $count) {
- $c_url = $count->comment_author_url;
- $mostactive .= ‘
- <div class=“lx8”>
- <div class=“top-author”>
- <div class=“top-comment”><a href=“‘ . get_template_directory_uri().”/inc/go.php?url=“. $c_url . ‘” target=“_blank” rel=“external nofollow”><div class=“author-url”><strong> ‘ . $count->comment_author . ‘</div></strong></a></div>
- <div class=“top-comment”>留下’.$count->cnt.’个脚印</div>
- </div>
- </div>’;
- }
- $mostactive .= ‘<div class=“clear”></div></div>’;
- echo $mostactive;
- }
- }
- if (zm_get_option(‘meta_delete’)) {} else {require get_template_directory() . ‘/inc/meta-delete.php’;}
- require get_template_directory() . ‘/inc/meta-boxes.php’;
- require get_template_directory() . ‘/inc/show-meta.php’;
然后使用以下CSS文件。
- <?php
- /*
- Template Name: 博客导航
- */
- if ( ! defined( ‘ABSPATH’ ) ) { exit; }
- ?>
- <?php get_header(); ?>
- <link rel=“stylesheet” href=“<?php bloginfo(‘template_url’); ?>/css/sites.css” />
- <style type=“text/css”>
- .favorites-top{
- margin-top:10px;
- }
- .breadcrumb,
- .yheader{
- display:none;
- }
- #primary {
- float: left;
- }
- #sidebar {
- float: right;
- }
- </style>
- <style type=“text/css”>
- .top-comments {
- margin: -5px 0 0 0;
- }
- .top-comments img {
- float: left;
- height: auto;
- max-width: 100%;
- width: auto;
- }
- .top-comments .lx8 {
- float: left;
- min-height: 1px;
- width: 16.6666666666666%;
- padding: 2px;
- }
- .top-author {
- background: #fff;
- margin: 5px;
- padding: 0 0 10px 0;
- border: 1px solid #ddd;
- }
- .top-comment {
- color: #999;
- text-align: center;
- }
- .author-url {
- width: 100%;
- padding: 5px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .comment-authors {
- padding: 0 10px;
- }
- .dh_title{
- background: #fff;
- margin: 0 0 10px 0;
- padding: 10px;
- border: 1px solid #ddd;
- box-shadow: 0 0 1px rgba(0,0,0,.04);
- border-radius: 2px;
- }
- .dh_title h3{
- font-size: 18px;
- margin-left:10px;
- }
- .dh_title .dh_blog{
- margin:10px 0;
- }
- .dh_title .dh_more{
- position: absolute;
- top: 5px;
- right: 20px;
- }
- }
- article{
- background:#fff;
- }
- #sites-widget-one h3 {
- font-size: 18px;
- background: #fff;
- width: 100%;
- height: 30px;
- line-height: 40px;
- padding: 0 20px;
- border-bottom: 0px solid #ddd;
- }
- .widget ul{
- width: 100%;
- }
- .widget_links li{
- float: left;
- width: 20% !important;
- border:0px;
- }
- .widget_links a{
- border: 1px solid #fff;
- </style>
- <div class=“favorites-top”>
- <div class=“top-date rili”>
- <?php the_title( ‘<h1 class=“favorites-title“>’, ‘</h1>’ ); ?>
- <span id=localtime></span>
- <script type=“text/javascript“>
- function showLocale(objD){
- var str,colorhead,colorfoot;
- var yy = objD.getYear();
- if(yy<1900) yy = yy+1900;
- var MM = objD.getMonth()+1;
- if(MM<10) MM = ‘0‘ + MM;
- var dd = objD.getDate();
- if(dd<10) dd = ‘0‘ + dd;
- var hh = objD.getHours();
- if(hh<10) hh = ‘0‘ + hh;
- var mm = objD.getMinutes();
- if(mm<10) mm = ‘0‘ + mm;
- var ss = objD.getSeconds();
- if(ss<10) ss = ‘0‘ + ss;
- var ww = objD.getDay();
- if ( ww==0 ) colorhead=“<font color=\”#FF0000\”>”;
- if ( ww > 0 && ww < 6 ) colorhead=“<font color=\”#373737\”>”;
- if ( ww==6 ) colorhead=“<font color=\”#008000\”>”;
- if (ww==0) ww=“星期日”;
- if (ww==1) ww=“星期一”;
- if (ww==2) ww=“星期二”;
- if (ww==3) ww=“星期三”;
- if (ww==4) ww=“星期四”;
- if (ww==5) ww=“星期五”;
- if (ww==6) ww=“星期六”;
- colorfoot=“</font>”
- str = colorhead + yy + “年” + MM + “月” + dd + “日 “ + hh + “:” + mm + “:” + ss + ” “ + ww + colorfoot;
- return(str);
- }
- function tick()
- {
- var today;
- today = new Date();
- document.getElementById(“localtime”).innerHTML = showLocale(today);
- window.setTimeout(“tick()”, 1000);
- }
- tick();
- </script>
- </div>
- <div class=“tianqi rili”>
- <iframe allowtransparency=“true” frameborder=“0” width=“385” height=“75” scrolling=“no” src=“//tianqi.2345.com/plugin/widget/index.htm?s=1&z=1&t=0&v=0&d=3&bd=0&k=&f=&q=1&e=1&a=1&c=54511&w=385&h=96&align=left”></iframe>
- </div>
- <div class=“clear”></div>
- </div>
- <div id=“primary” class=“content-area”>
- <main id=“main” class=“site-main” role=“main”>
- <div class=“dh_pc”>
- <p>每当在本站评论时填写网址,贵站将自动排在第一位</p>
- <span class=“dh_more”><a href=“https://lixuejiang.com/fangke”>more</a></span>
- <?php Autofirst(25);?>
- </div>
- <article class=“dh_title“>
- <h3>知名博客</h3>
- <div class=“clear”></div>
- <?php top_comment_authors(24); ?>
- </article>
- <article class=“dh_title“>
- <h3>人气博客</h3>
- <div class=“clear”></div>
- <div class=“dh_blog”>
- <?php top_comments(24); ?>
- </div>
- </article>
- <!– 网址小工具 –>
- <div id=“sites-widget-one” class=“sites-widget”>
- <?php dynamic_sidebar( ‘favorites-one’ ); ?>
- <div class=“clear”></div>
- </div>
- <!– 全部网址分类 –>
- <?php
- $taxonomy = ‘favorites’;
- $terms = get_terms($taxonomy); foreach ($terms as $cat) {
- $catid = $cat->term_id;
- $args = array(
- ‘showposts’ => 100,
- ‘meta_key’ => ‘sites_order’,
- ‘orderby’ => ‘meta_value’,
- ‘order’ => ‘date’,
- ‘tax_query’ => array( array( ‘taxonomy’ => $taxonomy, ‘terms’ => $catid, ‘include_children’ => false ) )
- );
- $query = new WP_Query($args);
- if( $query->have_posts() ) { ?>
- <article class=“dh_title“ data-wow-delay=“0.3s”>
- <h3><?php echo $cat->name; ?></h3>
- <div class=“dh_blog”>
- <div class=“sites-5″>
- <?php while ($query->have_posts()) : $query->the_post();?>
- <?php if ( get_post_meta($post->ID, ‘sites_img_link’, true) ) { ?>
- <?php $sites_img_link = sites_nofollow(get_post_meta($post->ID, ‘sites_img_link’, true)); ?>
- <span class=“sites-title wow fadeInUp” data-wow-delay=“0s”>
- <figure class=“picture-img sites-img”><?php if (zm_get_option(‘lazy_s’)) { zm_sites_thumbnail_h(); } else { zm_sites_thumbnail(); } ?></figure>
- <a href=“<?php echo $sites_img_link ?>” target=“_blank” rel=“external nofollow”><?php the_title(); ?></a>
- </span>
- <?php } else { ?>
- <?php $sites_link = sites_nofollow(get_post_meta($post->ID, ‘sites_link’, true)); ?>
- <span class=“sites-title wow fadeInUp” data-wow-delay=“0s”><a href=“<?php echo $sites_link ?>” target=“_blank” rel=“external nofollow”><?php the_title(); ?></a></span>
- <?php } ?>
- <?php endwhile; ?>
- <div class=“clear”></div>
- </div>
- </div>
- </article>
- <?php } wp_reset_query(); ?>
- <?php } ?>
- <!– 备用
- <article class=“sites sites-cat”>
- <div class=“sites-cats”>
- <span class=“sites-cat”>分类名称</span>
- <span class=“sites-more”><a href=“文字替换为网址分类链接地址”>更多</a>
- </div>
- <div class=“sites-link”>
- <ul class=“sites-5″>
- <?php
- $args = array(‘tax_query’ => array( array(‘taxonomy’ => ‘favorites’, ‘field’ => ‘id’, ‘terms’ => explode(‘,’,830 ))), ‘posts_per_page’ => 100);
- query_posts($args); while ( have_posts() ) : the_post();
- ?>
- <?php if ( get_post_meta($post->ID, ‘sites_link’, true) ) { ?>
- <?php $sites_link = get_post_meta($post->ID, ‘sites_link’, true); ?>
- <li class=“sites-title“><a href=“<?php echo $sites_link ?>” target=“_blank” rel=“external nofollow”><?php the_title(); ?></a></li>
- <?php } else { ?>
- <?php the_title( sprintf( ‘<li class=“sites-title“><a href=“%s” rel=“bookmark”>’, esc_url( get_permalink() ) ), ‘</a></li>’ ); ?>
- <?php } ?>
- <?php endwhile; ?>
- <?php wp_reset_query(); ?>
- <div class=“clear”></div>
- </ul>
- </div>
- </article>
- –>
- </main>
- </div>
- <div id=“sidebar” class=“widget-area”>
- <div class=“wow fadeInUp” data-wow-delay=“0.5s”>
- <?php if ( ! dynamic_sidebar( ‘favorites’ ) ) : ?>
- <aside id=“add-widgets” class=“widget widget_text”>
- <h3 class=“widget-title“><i class=“be be-warning”></i>添加小工具</h3>
- <div class=“textwidget”>
- <a href=“<?php echo admin_url(); ?>widgets.php” target=“_blank”>为“网址侧边栏”添加小工具</a>
- </div>
- <div class=“clear”></div>
- </aside>
- <?php endif; ?>
- </div>
- </div>
- <?php get_footer(); ?>
注意,本代码仍然只适用于知更鸟Begin主题。
本文所有权归李学江博客所有,转载请注明来源:https://lixuejiang.com/wordpress/2496.htm
文章评论 本文章有个评论