banner
Feng

Feng's xLog

我在xLog上的窝

无插件给博客加个说说

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

image

在借助 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 了,用这玩意手机端就可以发送,方便。目前也调试好了,那就开始用吧。

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。