有朋友有许多友情链接,难免有个别站长无法长期坚持,中途放弃。于是就有必要去除那些无法访问的链接,一个个访问查看很麻烦。若以原站头像是否显示来判断,有些不准确,比如开了防盗链或头像不来自原站。这样网址自动检测就非常有必要了。

实现方法:创建一个自动检测链接的页面。

一、效果展示

https://www.jishusongshu.com/links-check/

二、配置过程

1、新建页面模板

比如命名为links-check.php,然后上传到页面模板文件夹。

使用的时候:新建页面-选择模板即可。

<?php 

/**
 Template Name: 链接检测模版
 */

get_header(); 
// 网址访问判断
function httpcode($url){
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_exec($ch);
        return $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        curl_close($ch);
}
//链接访问状态判断函数
function link_status($link)
{
    //ini_set('max_execution_time',15);
    $status=httpcode($link);
    if($status == 200 ) 
        return "<p><i class=\"fa fa-check-circle\" aria-hidden=\"true\"></i> 访问正常</p>";
    else
        return "<font color=\"#CCA700\"><i class=\"fa fa-exclamation-circle\" aria-hidden=\"true\"></i>  无法访问</font>";
}

//列出指定链接
function the_link_items($id = null)
{
    $bookmarks = get_bookmarks('orderby=date&category=' . $id);
    $output = '';
    if (!empty($bookmarks)) {
        $output .= '<ul class="link-items fontSmooth">';
        foreach ($bookmarks as $bookmark) {
            if (empty($bookmark->link_description)) {
                $bookmark->link_description = __('This guy is so lazy ╮(╯▽╰)╭', 'sakurairo');
            }

            if (empty($bookmark->link_image)) {
                $bookmark->link_image = 'https://view.moezx.cc/images/2017/12/30/Transparent_Akkarin.th.jpg';
            }
            $output .= '<li class="link-item"><a class="link-item-inner effect-apollo" href="' . $bookmark->link_url . '" title="' . $bookmark->link_description . '" target="_blank" rel="friend"><img src="' . $bookmark->link_image . '" src="/load/inload.svg"><span class="sitename">' . $bookmark->link_name . '</span><span>' . link_status($bookmark->link_url). '</span><div class="linkdes">' . $bookmark->link_description . '</div></a></li>';
        }
        $output .= '</ul>';
    }
    return $output;
}

//列出所有链接
function link_items()
{
    $linkcats = get_terms('link_category');
    if (!empty($linkcats)) {
        foreach ($linkcats as $linkcat) {
            $result .= '<h3 class="link-title"><span class="link-fix">' . $linkcat->name . '</span></h3>';
            if ($linkcat->description) {
                $result .= '<div class="link-description">' . $linkcat->description . '</div>';
            }

            $result .= the_link_items($linkcat->term_id);
        }
    } else {
        $result = the_link_items();
    }
    return $result;
}

?>
    <?php while(have_posts()) : the_post(); ?>
    <?php if( !get_post_thumbnail_id(get_the_ID())) { ?>
    <span class="linkss-title"><?php the_title();?></span>
    <?php } ?>
        <article <?php post_class("post-item"); ?>>
            <?php the_content(); ?>
            <div class="links">
                <?php echo link_items();//列出所有友情链接,若列出分组id为49的则改为the_link_items('49') ?>
            </div>
        </article>
    <?php endwhile; ?>
<?php
get_footer();

2、引入css

add-style.css(比较长,就不贴出来了)

三、源码下载

见下面附件







文件名称:WordPress友链自动检测页面模板
适用版本:WordPress
更新日期:2021-05-07
文件大小:3.9 K

点击下载

Invitation
QQ Group
1095632335

created:04/01/2020

Welcome to the Group

Use this card to join us and participate in a pleasant discussion together .

Welcome to JISHUSONGSHU Group,wish you a nice day .