无插件给博客加个说说

现在很多博客上都添加了说说或类似的页面,各种样式的都有,看着我心痒痒,真想逮个空给俺那小破站也“武装”一下。看了老张和阿杰等都是调用Memos,样式不错,是俺喜欢的,我也照样画葫芦似的去搭了个Memos(这东西是不错,把它当作随手记也好,当作个人小微博也罢,挺好),但目前尚没有合适的主机跑Docker,要不咱就无插件来一个………………

在借助Chatgpt的帮忙下,生成一些代码还是蛮轻松的,本站的归档、标签、友链都是在“家伙”的帮忙下搞定的,要不然就我这榆木脑袋估计还得折腾好久。大致的步骤如下:

  • 在文章分类目录下新建一个说说,slug写为:shuoshuo
  • 在主题的functions.php中添加如下代码:
/*屏蔽说说在首页中显示*/
function exclude_category_from_home($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $excluded_category = get_category_by_slug('shuoshuo');
        $excluded_category_id = $excluded_category->term_id;
        $query->set('cat', '-' . $excluded_category_id);
    }
}
add_action('pre_get_posts', 'exclude_category_from_home');
  • 新建一个PHP文件,名字可以用template-shuoshuo命名,并在其中添加以下代码:
<?php
/*
Template Name: 说说
*/

get_header(); // 调用主题的header部分
?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <?php
        // 获取"说说"分类的信息
        $category = get_category_by_slug('shuoshuo');

        // 查询属于"说说"分类的文章列表
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 10,
            'paged' => $paged,
            'cat' => $category->term_id
        );
        $query = new WP_Query($args);

        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                // 显示每篇文章的内容
                ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <div class="author-info">

                   <div class="author-meta">
                   <span class="author-name"><?php the_author(); ?></span>
                   <span class="post-date"><?php the_date(); ?></span>
                   </div>
                </div>

                    <div class="entry-content">
                        <?php the_content(); ?>

                    </div>

                    <footer class="entry-footer">
                        <?php if (comments_open() || get_comments_number()) : ?>
                            <div class="comments-link">
                                <?php comments_popup_link(__('来个评论', 'your-theme'), __('1 Comment', 'your-theme'), __('% Comments', 'your-theme')); ?>

                            </div>
                            <br>
                        <?php endif; ?>
                    </footer>
                </article>
                <?php
            }

            // 显示翻页链接
            echo '<div class="pagination">';
            echo paginate_links(array(
                'total' => $query->max_num_pages
            ));
            echo '</div>';
        }
        ?>

    </main>
</div>

<?php
get_footer(); // 调用主题的footer部分

保存并上传到主题根目录下

  • 加到WordPress后台,在页面中新建一个页面,模板选择刚才建立的说说模板,以说说为名保存后,再在菜单中添加刚才建立的页面,保存后再刷新首页,就可以去看看效果如何了。当然别忘了新建一篇文章,名字随意,因为说说页面只调用文章的内容。

本文所提到的无插件实现说说功能,原理就是在首页中屏蔽一个分类输出,并将这个分类按要求输出到指定的页面中去,暂未进么美化(本个对CSS也就那点水平,实在是……不能说233),不需要评论的话也可以去掉(评论点进去的界面是不是很眼熟——这不就是文章页嘛)。至于调用Memos回头哪天再找个主机研究研究,说不定啥时也就挤上了
个人看完感觉也没啥好玩的,像我这么懒的,博客更新都慢,还会经常去更新Memos?估计难,没那习惯呀,所以我觉得真有这功夫还是踏实坐下来水几篇是不是更带劲?
2024-01-14 最终还是改成Memos了,用这玩意手机端就可以发送,方便。目前也调试好了,那就开始用吧。

19 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注