之前emlog和wordpress都可以直接使用插件来实现文章列表页面,但是typecho并没有这个功能,这个和sitemap是不一样的。

页面效果

2024-03-04T07:27:02.png

这个用处是什么?

也许有人会问这个有什么用,都有sitemap了,这个是干嘛的。

DamonScreenShot 2017-12-25 13.53.17.png

首先sitemap的格式是这样的,主要就是表明了文章的链接、更新时间等信息,在百度后台、谷歌后台都可以提交,等着搜索引擎来抓取。

而搜索引擎还有一个功能是自己主动提交。提交的格式就是一行一个链接,或者一行一个链接的文件。这个列表的链接就是为了做这个用的。

制作模板页面

typecho在后台可以根据模板创建自定义页面,这里就是创建一个自定义模板,里面的功能就是在数据库中找出所有的文章链接并且提取输出出来,新建一个文件URLLinks.php,内容写以下内容。

<?php 
/**
 * 网站链接列表
 * 
 * @package custom 
 * 
 */
?>
<?php
$db = Typecho_Db::get();
$options = Typecho_Widget::widget('Widget_Options');
$posts = $db->fetchAll($db->select()->from('table.contents')
    ->where('table.contents.status = ?', 'publish')
    ->order('table.contents.created', Typecho_Db::SORT_DESC));
$postIdx = 0;
foreach ($posts as $key => $p) {
    /** 取出所有分类 */
    $p['categories'] = $db->fetchAll($db
        ->select()->from('table.metas')
        ->join('table.relationships', 'table.relationships.mid = table.metas.mid')
        ->where('table.relationships.cid = ?', $p['cid'])
        ->where('table.metas.type = ?', 'category')
        ->order('table.metas.order', Typecho_Db::SORT_ASC));

    /** 取出第一个分类作为slug条件 */
    $p['category'] = current(Typecho_Common::arrayFlatten($p['categories'], 'slug'));
    //去掉附件
    $type = $p['type'];
    if ($type == "post") {
        $postIdx++;
        $routeExists = (NULL != Typecho_Router::get($type));
        $pathinfo = $routeExists ? Typecho_Router::url($type, $p) : '#';
        $permalink = Typecho_Common::url($pathinfo, $options->index);
        if ($postIdx % 50 == 0) {
            echo "<a href=\"" . $permalink . "\">" . $permalink . "</a><br/><hr/>";
        } else {
            echo "<a href=\"" . $permalink . "\">" . $permalink . "</a><br/>";
        }
    }
}
?>

将文件上传到模板的根目录,然后去后台新建独立页面,在自定义模板中选择网站链接列表这个模板就可以了。

DamonScreenShot 2017-12-25 14.02.24.png

当然这个创建出来的页面就是单纯的链接页面,为了更加好看,我们可以在这个页面的基础上完善下,当然不是必须的,如果想需要知道,可以往下看。

模板页面美化

上面是输出的内容,如果页面美化,可以看模板文件夹里面的page.php文件是怎么加头部和底部的,然后把输出文章内容的函数替换为该日志函数即可,因为不同的模板引入的路径不同,

然后将链接的布局位置微调一下更好看。

因为这个打印出来的是所有的链接,但是有时候提交的限额有限,所以可以每隔五十条将链接颜色修改为红色,在前端可以更直观。

比如我用的这个handsome模板,可以将page.php文件的文章内容输出的函数替换即可

<?php $content = Content::postContentHtml($this,
                 $this->user->uid); ?>

这里我根据不同场景,写了两个模板,一个是纯链接展示

纯链接展示

演示效果:https://ddceo.com/blog/urls.html

<?php

/**
 * 网站链接列表
 * 
 * @package custom 
 * 
 */
?>
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('component/header.php'); ?>

<!-- aside -->
<?php $this->need('component/aside.php'); ?>
<!-- / aside -->

<!-- <div id="content" class="app-content"> -->
<a class="off-screen-toggle hide"></a>
<main class="app-content-body <?php echo Content::returnPageAnimateClass($this); ?>">
    <div class="hbox hbox-auto-xs hbox-auto-sm">
        <!--文章-->
        <div class="col center-part gpu-speed" id="post-panel">
            <!--标题下的一排功能信息图标:作者/时间/浏览次数/评论数/分类-->
            <?php echo Content::exportPostPageHeader($this, $this->user->hasLogin()); ?>
            <div class="wrapper-md">
                <!--正文顶部的部件,如“返回首页”-->
                <?php echo Content::BreadcrumbNavigation($this, $this->options->rootUrl); ?>
                <!--博客文章样式 begin with .blog-post-->
                <div id="postpage" class="blog-post">
                    <article class="single-post panel">
                        <!--文章页面的头图-->
                        <?php echo Content::exportHeaderImg($this); ?>
                        <!--文章内容-->
                        <div id="post-content" class="wrapper-lg">

                            <div style="margin: 100px;">
                                <?php
                                $db = Typecho_Db::get();
                                $options = Typecho_Widget::widget('Widget_Options');
                                $posts = $db->fetchAll($db->select()->from('table.contents')
                                    ->where('table.contents.status = ?', 'publish')
                                    ->order('table.contents.created', Typecho_Db::SORT_DESC));
                                $postIdx = 0;
                                $urlPage = "";
                                $jsonPage = "<pre><code>";
                                foreach ($posts as $key => $p) {
                                    /** 取出所有分类 */
                                    $p['categories'] = $db->fetchAll($db
                                        ->select()->from('table.metas')
                                        ->join('table.relationships', 'table.relationships.mid = table.metas.mid')
                                        ->where('table.relationships.cid = ?', $p['cid'])
                                        ->where('table.metas.type = ?', 'category')
                                        ->order('table.metas.order', Typecho_Db::SORT_ASC));

                                    /** 取出第一个分类作为slug条件 */
                                    $p['category'] = current(Typecho_Common::arrayFlatten($p['categories'], 'slug'));
                                    //去掉附件
                                    $type = $p['type'];
                                    if ($type == "post") {
                                        $postIdx++;
                                        $routeExists = (NULL != Typecho_Router::get($type));
                                        $pathinfo = $routeExists ? Typecho_Router::url($type, $p) : '#';
                                        $permalink = Typecho_Common::url($pathinfo, $options->index);
                                        if ($postIdx % 50 == 0) {
                                            $urlPage = $urlPage . "<a href=\"" . $permalink . "\">" . $permalink . "</a><br/><hr/>";
                                        } else {
                                            $urlPage = $urlPage . "<a href=\"" . $permalink . "\">" . $permalink . "</a><br/>";
                                        }
                                        $jsonPage = $jsonPage . "\"" . $permalink . "\",\n";
                                    }
                                }
                                $jsonPage = $jsonPage . "</code></pre>";
                                echo $urlPage;
                                echo $jsonPage;
                                ?>
                            </div>

                            <?php Content::pageFooter($this->options, $this) ?>
                        </div>
                    </article>
                </div>
                <!--评论-->
                <?php $this->need('component/comments.php') ?>
            </div>
            <?php echo WidgetContent::returnRightTriggerHtml() ?>
        </div>
        <!--文章右侧边栏开始-->
        <?php $this->need('component/sidebar.php'); ?>
        <!--文章右侧边栏结束-->
    </div>
</main>
<?php echo Content::returnReadModeContent($this, $this->user->uid, $content); ?>

<!-- footer -->
<?php $this->need('component/footer.php'); ?>
<!-- / footer -->

另一个是每50条分割一组

演示效果:https://ddceo.com/blog/urlPage.html

<?php 
/**
 * 网站链接分页列表
 * 
 * @package custom 
 * 
 */
?>
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('component/header.php'); ?>

<!-- aside -->
<?php $this->need('component/aside.php'); ?>
<!-- / aside -->

<!-- <div id="content" class="app-content"> -->
<a class="off-screen-toggle hide"></a>
<main class="app-content-body <?php echo Content::returnPageAnimateClass($this); ?>">
    <div class="hbox hbox-auto-xs hbox-auto-sm">
        <!--文章-->
        <div class="col center-part gpu-speed" id="post-panel">
            <!--标题下的一排功能信息图标:作者/时间/浏览次数/评论数/分类-->
            <?php echo Content::exportPostPageHeader($this, $this->user->hasLogin()); ?>
            <div class="wrapper-md">
                <!--正文顶部的部件,如“返回首页”-->
                <?php echo Content::BreadcrumbNavigation($this, $this->options->rootUrl); ?>
                <!--博客文章样式 begin with .blog-post-->
                <div id="postpage" class="blog-post">
                    <article class="single-post panel">
                        <!--文章页面的头图-->
                        <?php echo Content::exportHeaderImg($this); ?>
                        <!--文章内容-->
                        <div id="post-content" class="wrapper-lg">

                            <div style="margin: 100px;">
                                <?php
                                $db = Typecho_Db::get();
                                $options = Typecho_Widget::widget('Widget_Options');
                                $posts = $db->fetchAll($db->select()->from('table.contents')
                                    ->where('table.contents.status = ?', 'publish')
                                    ->order('table.contents.created', Typecho_Db::SORT_DESC));
                                $postIdx = 0;
                                foreach ($posts as $key=>$p) {
                                    /** 取出所有分类 */
                                    $p['categories'] = $db->fetchAll($db
                                        ->select()->from('table.metas')
                                        ->join('table.relationships', 'table.relationships.mid = table.metas.mid')
                                        ->where('table.relationships.cid = ?', $p['cid'])
                                        ->where('table.metas.type = ?', 'category')
                                        ->order('table.metas.order', Typecho_Db::SORT_ASC));

                                    /** 取出第一个分类作为slug条件 */
                                    $p['category'] = current(Typecho_Common::arrayFlatten($p['categories'], 'slug'));
                                    //去掉附件
                                    $type = $p['type'];
                                    if ($type == "post") {
                                        $routeExists = (NULL != Typecho_Router::get($type));
                                        $pathinfo = $routeExists ? Typecho_Router::url($type, $p) : '#';
                                        $permalink = Typecho_Common::url($pathinfo, $options->index);
                                        if ($postIdx%50 == 0) {
                                            if ($postIdx == 0) {
                                                echo "<pre><code>" . $permalink . "\n";
                                            } else {
                                                echo "</code></pre>" . "<h2>分页:" . $postIdx/50 . "</h2><br/>" ."<pre><code>" . $permalink . "\n";
                                            }
                                        } else {
                                            echo $permalink . "\n";
                                        }
                                        $postIdx++;
                                    }
                                }
                                if ($postIdx%50 != 0) {
                                    //追加结束符
                                    echo "</code></pre>";
                                }
                                ?>
                            </div>

                            <?php Content::pageFooter($this->options, $this) ?>
                        </div>
                    </article>
                </div>
                <!--评论-->
                <?php $this->need('component/comments.php') ?>
            </div>
            <?php echo WidgetContent::returnRightTriggerHtml() ?>
        </div>
        <!--文章右侧边栏开始-->
        <?php $this->need('component/sidebar.php'); ?>
        <!--文章右侧边栏结束-->
    </div>
</main>
<?php echo Content::returnReadModeContent($this, $this->user->uid, $content); ?>

<!-- footer -->
<?php $this->need('component/footer.php'); ?>
<!-- / footer -->

链接提交到搜索引擎

通过模板创建好对应的页面之后,就可以访问那个页面了,那个页面的内容就是所有的链接列表。这时候就可以复制这些链接去搜索引擎的后台提交了,比如百度。去百度的搜索资源平台:http://ziyuan.baidu.com/,验证完网站的所有权之后,有一个链接提交的功能。

后台提交

如果你是基础使用,那么可以将这些链接直接粘贴到提交框,然后去提交就行了。

DamonScreenShot 2017-12-25 14.15.31.png

使用curl推送

这种使用脚本推送的就更简单了,在桌面新建一个文件,命名为urls.txt,将这些链接复制进去,然后通过自己的推送接口使用命令行提交就行了。

DamonScreenShot 2017-12-25 14.14.46.png

参考链接


☟☟可点击下方广告支持一下☟☟

最后修改:2024 年 03 月 04 日
请我喝杯可乐,请随意打赏: ☞已打赏列表