快好知 kuaihz订阅观点

 

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

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

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

下面分模块给大家介绍:

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

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

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= “

        $outputy .= $tmpy;

    }

    $outputy = “

  • ”.$outputy.“

”;

    echo $outputy ;

}

其中输出的样式可以自定义,毕竟每一个博客的样式都不一样,这个就靠自己发挥了,只需要在以上代码中的

  • 中添加一个class属性即可。

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

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

二、知名博客模块

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

三、人气博客

显示一定时期内在本站留言数最多的用户网站。

四、博客大全

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

五、边栏

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

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 = ‘

’;

    foreach($results as $result) {

        $c_url = $result->comment_author_url;

        $output .= ‘

        

            

                

                

’ . human_time_diff(strtotime($result->last_commented_date)) . ‘前来过

            

        ’;

    }

    $output .= ‘

’;

    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 =  ‘

’;

    if ( $counts ) {

        wp_cache_set( ‘mostactive’, $counts );

        foreach ($counts as $count) {

            $c_url = $count->comment_author_url;

            $mostactive .= ‘

            

                

                    

                    

留下’.$count->cnt.’个脚印

                

            ’;

        }

        $mostactive .= ‘

’;

        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; }

?>

<link rel=“stylesheet” href=“/css/sites.css” />

    

        <?php the_title( ‘

’, ‘

’ ); ?>

        

        

    

    

        

    

    


    

        

            

每当在本站评论时填写网址,贵站将自动排在第一位

            more

            

        

        

            

知名博客

            

                

        

        

            

人气博客

            

            

                

            

        

        

        

            

            

        

        

        <?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() ) { ?>

        

            

name; ?>

            

                

                    have_posts()) : $query->the_post();?>

                        ID, ‘sites_img_link’, true) ) { ?>

                            ID, ‘sites_img_link’, true)); ?>

                            

                                

                                <a href=“” target=“_blank” rel=“external nofollow”>

                            

                        

                            ID, ‘sites_link’, true)); ?>

                            <a href=“” target=“_blank” rel=“external nofollow”>

                        

                    

                    

                

            

        

        

        

        <!– 备用

        

            

                分类名称

                更多

            

            

                

                        <?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();

                        ?>

                        ID, ‘sites_link’, true) ) { ?>

                        ID, ‘sites_link’, true); ?>

                            

    • <a href=“

      ” target=“_blank” rel=“external nofollow”>

                        

                            <?php the_title( sprintf( ‘

                        

                        

                        

                        

                    

                

            

             –>

        


        

            

                

                    

    添加小工具

                    

                        <a href=“widgets.php” target=“_blank”>为“网址侧边栏”添加小工具

                    

                    

                

            

        


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

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

    本站资源来自互联网,仅供学习,如有侵权,请通知删除,敬请谅解!
    搜索建议:导航站  导航站词条  WordPress  WordPress词条  排名  排名词条  实现  实现词条  网址  网址词条  
    优化

     企业网站判断是否成功的几个因素

     建站的每一个步骤或条件都会对产生或多或少的影响,那对于企业网站来说有哪些因素可以判断是否成功呢? 企业网站判断是否成功的几个因素   企业的宗旨就是只有用心才...(展开)