Loading
0

WordPress博客教你如何实现智能排名网址导航站
被墙跳转TG:@qianhenetwork QQ 851617266

301免备案跳转微信公众号
腾讯云服务器大促销。
华为服务器

今天逛了一下李学江大神的博客,发现这个功能特别不错,于是转载过来了跟大家分享。

今天终于抽出时间,把代码和教程分享给大家。我所构建的网址导航主要分为以下几个模块:访客网站自动排名第一模块、知名博客模块、人气博客模块、博客大全模块、边栏模块。

WordPress博客教你如何实现智能排名网址导航站

下面分模块给大家介绍:

一、访客网站自动排名第一模块

WordPress博客如何构造一个智能排名网址导航站

通过调取博客留言者的名字和网址,并去重,按照留言时间顺序排列,最近代链接的留言排在第一位(留言者不填写链接则不显示)

1、把以下代码放入到所用主题的functions.php最后一个%>

  1. //最新评论排第一
  2. function Autofirst($af){
  3.     global $wpdb;
  4.     $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”;
  5.     $wally = $wpdb->get_results($queryaf);
  6.     foreach ($wally as $commentaf){
  7.         $tmpy= “<li><a target=\”_blank\” href=\””.$commentaf->comment_author_url.“\”>”.$commentaf->comment_author.“</a></li>”;
  8.         $outputy .= $tmpy;
  9.     }
  10.     $outputy = “<ul>”.$outputy.“</ul>”;
  11.     echo $outputy ;
  12. }

其中输出的样式可以自定义,毕竟每一个博客的样式都不一样,这个就靠自己发挥了,只需要在以上代码中的<ul>中添加一个class属性即可。

2、把以下代码放入需要显示最新访客的地方,如留言板。

  1. <?php Autofirst(100);?>

其中100表示显示100个最新访客,这个数值可以自定义,一定不能为空,要不然会出错,最起码要大于0以上。

二、知名博客模块

显示一定时期内在本站留言比较活跃的用户网站。

  1. <?php top_comment_authors(24); ?>

三、人气博客
显示一定时期内在本站留言数最多的用户网站。

  1. <?php top_comments(24); ?>

四、博客大全

调用你网站的友情链接,由于主题差异,每个网站的调用方法可能不尽相同,如果不懂的话,你可以资讯主题作者。

WordPress博客如何构造一个智能排名网址导航站

五、边栏

调用主题边栏,在后台小工具中添加你想要显示的内容。

PS:

1、如果你正在使用知更鸟Begin主题,以上代码全部适用,非知更鸟Begin主题,通过本教程你只能实现最近访客访客自动排名第一功能,如果感兴趣,你也可以参照这篇文章:《WordPress博客如何利用最新评论实现类似导航网站自动排第一功能》。

2、各位在使用的时候,会发现知名博客和人气博客显示的内容与本站有些诧异,如果你希望和本站显示的一样,你可以找到/wp-content/themes/begin/inc/inc.php文件,将约2013至1268行代码替换成以下代码。

  1. // 读者排行
  2. function top_comment_authors($amount = 98) {
  3.     global $wpdb;
  4.         $prepared_statement = $wpdb->prepare(
  5.         ‘SELECT
  6.         COUNT(comment_author) AS comments_count, comment_author, comment_author_url, comment_author_email, MAX( comment_date ) as last_commented_date
  7.         FROM ‘.$wpdb->comments.’
  8.         WHERE comment_author != “” AND comment_type = “” AND comment_approved = 1  AND user_id = “”
  9.         GROUP BY comment_author
  10.         ORDER BY comments_count DESC, comment_author ASC
  11.         LIMIT %d’,
  12.         $amount);
  13.     $results = $wpdb->get_results($prepared_statement);
  14.     $output = ‘<div class=“top-comments”>’;
  15.     foreach($results as $result) {
  16.         $c_url = $result->comment_author_url;
  17.         $output .= ‘
  18.         <div class=“lx8”>
  19.             <div class=“top-author”>
  20.                 <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>
  21.                 <div class=“top-comment”>’ . human_time_diff(strtotime($result->last_commented_date)) . ‘前来过</div>
  22.             </div>
  23.         </div>’;
  24.     }
  25.     $output .= ‘<div class=“clear”></div></div>’;
  26.     echo $output;
  27. }
  28. function top_comments($number = 98) {
  29.     global $wpdb;
  30.     $counts = wp_cache_get( ‘mostactive’ );
  31.     if ( false === $counts ) {
  32.         $counts = $wpdb->get_results(“SELECT COUNT(comment_author) AS cnt, comment_author, comment_author_url, comment_author_email
  33.         FROM {$wpdb->prefix}comments
  34.         WHERE comment_date > date_sub( NOW(), INTERVAL 90 DAY )
  35.         AND comment_approved = ‘1’
  36.         AND comment_author_email != ‘[email protected]
  37.         AND comment_author_email != ”
  38.         AND comment_author_url != ”
  39.         AND comment_type = ”
  40.         AND user_id = ‘0’
  41.         GROUP BY comment_author_email
  42.         ORDER BY cnt DESC
  43.         LIMIT $number”);
  44.     }
  45.     $mostactive =  ‘<div class=“top-comments”>’;
  46.     if ( $counts ) {
  47.         wp_cache_set( ‘mostactive’, $counts );
  48.         foreach ($counts as $count) {
  49.             $c_url = $count->comment_author_url;
  50.             $mostactive .= ‘
  51.             <div class=“lx8”>
  52.                 <div class=“top-author”>
  53.                     <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>
  54.                     <div class=“top-comment”>留下’.$count->cnt.’个脚印</div>
  55.                 </div>
  56.             </div>’;
  57.         }
  58.         $mostactive .= ‘<div class=“clear”></div></div>’;
  59.         echo $mostactive;
  60.     }
  61. }
  62. if (zm_get_option(‘meta_delete’)) {} else {require get_template_directory() . ‘/inc/meta-delete.php’;}
  63. require get_template_directory() . ‘/inc/meta-boxes.php’;
  64. require get_template_directory() . ‘/inc/show-meta.php’;

然后使用以下CSS文件。

  1. <?php
  2. /*
  3. Template Name: 博客导航
  4. */
  5. if ( ! defined( ‘ABSPATH’ ) ) { exit; }
  6. ?>
  7. <?php get_header(); ?>
  8. <link rel=“stylesheet” href=“<?php bloginfo(‘template_url’); ?>/css/sites.css” />
  9. <style type=“text/css”>
  10. .favorites-top{
  11.     margin-top:10px;
  12. }
  13. .breadcrumb,
  14. .yheader{
  15.     display:none;
  16. }
  17. #primary {
  18.     float: left;
  19. }
  20. #sidebar {
  21.     float: right;
  22. }
  23. </style>
  24. <style type=“text/css”>
  25. .top-comments {
  26.     margin: -5px 0 0 0;
  27. }
  28. .top-comments img {
  29.     float: left;
  30.     height: auto;
  31.     max-width: 100%;
  32.     width: auto;
  33. }
  34. .top-comments .lx8 {
  35.     float: left;
  36.     min-height: 1px;
  37.     width: 16.6666666666666%;
  38.     padding: 2px;
  39. }
  40. .top-author {
  41.     background: #fff;
  42.     margin: 5px;
  43.     padding: 0 0 10px 0;
  44.     border: 1px solid #ddd;
  45. }
  46. .top-comment {
  47.     color: #999;
  48.     text-align: center;
  49. }
  50. .author-url {
  51.     width: 100%;
  52.     padding: 5px;
  53.     overflow: hidden;
  54.     text-overflow: ellipsis;
  55.     white-space: nowrap;
  56. }
  57. .comment-authors {
  58.     padding: 0 10px;
  59. }
  60. .dh_title{
  61.     background: #fff;
  62.     margin: 0 0 10px 0;
  63.     padding: 10px;
  64.     border: 1px solid #ddd;
  65.     box-shadow: 0 0 1px rgba(0,0,0,.04);
  66.     border-radius: 2px;
  67. }
  68. .dh_title h3{
  69.     font-size: 18px;
  70.     margin-left:10px;
  71. }
  72. .dh_title .dh_blog{
  73.     margin:10px 0;
  74. }
  75. .dh_title .dh_more{
  76.     position: absolute;
  77.     top: 5px;
  78.     right: 20px;
  79. }
  80. }
  81. article{
  82.     background:#fff;
  83. }
  84. #sites-widget-one h3 {
  85.     font-size: 18px;
  86.     background: #fff;
  87.     width: 100%;
  88.     height: 30px;
  89.     line-height: 40px;
  90.     padding: 0 20px;
  91.     border-bottom: 0px solid #ddd;
  92. }
  93. .widget ul{
  94.     width: 100%;
  95. }
  96. .widget_links li{
  97.     float: left;
  98.     width: 20% !important;
  99.     border:0px;
  100. }
  101. .widget_links a{
  102.     border: 1px solid #fff;
  103. </style>
  104. <div class=“favorites-top”>
  105.     <div class=“top-date rili”>
  106.         <?php the_title( ‘<h1 class=“favorites-title“>’, ‘</h1>’ ); ?>
  107.         <span id=localtime></span>
  108.         <script type=“text/javascript“>
  109.         function showLocale(objD){
  110.             var str,colorhead,colorfoot;
  111.             var yy = objD.getYear();
  112.             if(yy<1900) yy = yy+1900;
  113.             var MM = objD.getMonth()+1;
  114.             if(MM<10) MM = ‘0‘ + MM;
  115.             var dd = objD.getDate();
  116.             if(dd<10) dd = ‘0‘ + dd;
  117.             var hh = objD.getHours();
  118.             if(hh<10) hh = ‘0‘ + hh;
  119.             var mm = objD.getMinutes();
  120.             if(mm<10) mm = ‘0‘ + mm;
  121.             var ss = objD.getSeconds();
  122.             if(ss<10) ss = ‘0‘ + ss;
  123.             var ww = objD.getDay();
  124.             if  ( ww==0 )  colorhead=“<font color=\”#FF0000\”>”;
  125.             if  ( ww > 0 && ww < 6 )  colorhead=“<font color=\”#373737\”>”;
  126.             if  ( ww==6 )  colorhead=“<font color=\”#008000\”>”;
  127.             if  (ww==0)  ww=“星期日”;
  128.             if  (ww==1)  ww=“星期一”;
  129.             if  (ww==2)  ww=“星期二”;
  130.             if  (ww==3)  ww=“星期三”;
  131.             if  (ww==4)  ww=“星期四”;
  132.             if  (ww==5)  ww=“星期五”;
  133.             if  (ww==6)  ww=“星期六”;
  134.             colorfoot=“</font>”
  135.             str = colorhead + yy + “年” + MM + “月” + dd + “日 “ + hh + “:” + mm + “:” + ss + ”  “ + ww + colorfoot;
  136.             return(str);
  137.         }
  138.         function tick()
  139.         {
  140.             var today;
  141.             today = new Date();
  142.             document.getElementById(“localtime”).innerHTML = showLocale(today);
  143.             window.setTimeout(“tick()”1000);
  144.         }
  145.         tick();
  146.         </script>
  147.     </div>
  148.     <div class=“tianqi rili”>
  149.         <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>
  150.     </div>
  151.     <div class=“clear”></div>
  152. </div>
  153. <div id=“primary” class=“content-area”>
  154.     <main id=“main” class=“site-main” role=“main”>
  155.         <div class=“dh_pc”>
  156.             <p>每当在本站评论时填写网址,贵站将自动排在第一位</p>
  157.             <span class=“dh_more”><a href=“https://lixuejiang.com/fangke”>more</a></span>
  158.             <?php Autofirst(25);?>
  159.         </div>
  160.         <article class=“dh_title“>
  161.             <h3>知名博客</h3>
  162.             <div class=“clear”></div>
  163.                 <?php top_comment_authors(24); ?>
  164.         </article>
  165.         <article class=“dh_title“>
  166.             <h3>人气博客</h3>
  167.             <div class=“clear”></div>
  168.             <div class=“dh_blog”>
  169.                 <?php top_comments(24); ?>
  170.             </div>
  171.         </article>
  172.         <!– 网址小工具 –>
  173.         <div id=“sites-widget-one” class=“sites-widget”>
  174.             <?php dynamic_sidebar( ‘favorites-one’ ); ?>
  175.             <div class=“clear”></div>
  176.         </div>
  177.         <!– 全部网址分类 –>
  178.         <?php
  179.         $taxonomy = ‘favorites’;
  180.         $terms = get_terms($taxonomy); foreach ($terms as $cat) {
  181.         $catid = $cat->term_id;
  182.         $args = array(
  183.             ‘showposts’ => 100,
  184.             ‘meta_key’ => ‘sites_order’,
  185.             ‘orderby’ => ‘meta_value’,
  186.             ‘order’ => ‘date’,
  187.             ‘tax_query’ => array( array( ‘taxonomy’ => $taxonomy, ‘terms’ => $catid, ‘include_children’ => false ) )
  188.         );
  189.         $query = new WP_Query($args);
  190.         if( $query->have_posts() ) { ?>
  191.         <article class=“dh_title“ data-wow-delay=“0.3s”>
  192.             <h3><?php echo $cat->name; ?></h3>
  193.             <div class=“dh_blog”>
  194.                 <div class=“sites-5″>
  195.                     <?php while ($query->have_posts()) : $query->the_post();?>
  196.                         <?php if ( get_post_meta($post->ID, ‘sites_img_link’, true) ) { ?>
  197.                             <?php $sites_img_link = sites_nofollow(get_post_meta($post->ID, ‘sites_img_link’, true)); ?>
  198.                             <span class=“sites-title wow fadeInUp” data-wow-delay=“0s”>
  199.                                 <figure class=“picture-img sites-img”><?php if (zm_get_option(‘lazy_s’)) { zm_sites_thumbnail_h(); } else { zm_sites_thumbnail(); } ?></figure>
  200.                                 <a href=“<?php echo $sites_img_link ?>” target=“_blank” rel=“external nofollow”><?php the_title(); ?></a>
  201.                             </span>
  202.                         <?php } else { ?>
  203.                             <?php $sites_link = sites_nofollow(get_post_meta($post->ID, ‘sites_link’, true)); ?>
  204.                             <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>
  205.                         <?php } ?>
  206.                     <?php endwhile; ?>
  207.                     <div class=“clear”></div>
  208.                 </div>
  209.             </div>
  210.         </article>
  211.         <?php } wp_reset_query(); ?>
  212.         <?php } ?>
  213.         <!– 备用
  214.         <article class=“sites sites-cat”>
  215.             <div class=“sites-cats”>
  216.                 <span class=“sites-cat”>分类名称</span>
  217.                 <span class=“sites-more”><a href=“文字替换为网址分类链接地址”>更多</a>
  218.             </div>
  219.             <div class=“sites-link”>
  220.                 <ul class=“sites-5″>
  221.                     <?php
  222.                         $args = array(‘tax_query’ => array( array(‘taxonomy’ => ‘favorites’, ‘field’ => ‘id’, ‘terms’ => explode(‘,’,830 ))), ‘posts_per_page’ => 100);
  223.                         query_posts($args); while ( have_posts() ) : the_post();
  224.                     ?>
  225.                     <?php if ( get_post_meta($post->ID, ‘sites_link’, true) ) { ?>
  226.                     <?php $sites_link = get_post_meta($post->ID, ‘sites_link’, true); ?>
  227.                         <li class=“sites-title“><a href=“<?php echo $sites_link ?>” target=“_blank” rel=“external nofollow”><?php the_title(); ?></a></li>
  228.                     <?php } else { ?>
  229.                         <?php the_title( sprintf( ‘<li class=“sites-title“><a href=“%s” rel=“bookmark”>’, esc_url( get_permalink() ) ), ‘</a></li>’ ); ?>
  230.                     <?php } ?>
  231.                     <?php endwhile; ?>
  232.                     <?php wp_reset_query(); ?>
  233.                     <div class=“clear”></div>
  234.                 </ul>
  235.             </div>
  236.         </article>
  237.          –>
  238.     </main>
  239. </div>
  240. <div id=“sidebar” class=“widget-area”>
  241.     <div class=“wow fadeInUp” data-wow-delay=“0.5s”>
  242.         <?php if ( ! dynamic_sidebar( ‘favorites’ ) ) : ?>
  243.             <aside id=“add-widgets” class=“widget widget_text”>
  244.                 <h3 class=“widget-title“><i class=“be be-warning”></i>添加小工具</h3>
  245.                 <div class=“textwidget”>
  246.                     <a href=“<?php echo admin_url(); ?>widgets.php” target=“_blank”>为“网址侧边栏”添加小工具</a>
  247.                 </div>
  248.                 <div class=“clear”></div>
  249.             </aside>
  250.         <?php endif; ?>
  251.     </div>
  252. </div>
  253. <?php get_footer(); ?>

注意,本代码仍然只适用于知更鸟Begin主题。

本文所有权归李学江博客所有,转载请注明来源:https://lixuejiang.com/wordpress/2496.htm

301免备案跳转微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://www.zfcdn.xyz/showinfo-19-18759-0.html
亲爱的:被墙域名跳转TG:@qianhenetwork QQ:851617266,可否收藏+评论+分享呢?

最后编辑于:2019-04-25 11:22:36作者:

上一篇:网站粒子背景特效添加跟随鼠标动态线条特效插件
下一篇:Foxpay 9.4收费下载资源WordPress插件+前端用户中心源码 免费下载