banner
Feng

Feng's xLog

我在xLog上的窝

Add a comment to the blog without using plugins

Now many blogs have added a "Talk" or similar page, with various styles. Looking at them makes me itch, and I really want to "arm" my little broken site. I saw that Lao Zhang and AJie used Memos, and the style is good, which I like. So I also followed suit and set up a Memos (this thing is good, it can be used as a quick note or a personal microblog, it's great), but currently there is no suitable host to run Docker, otherwise I would have done it without plugins...

image

With the help of Chatgpt, generating some code is quite easy. The archives, tags, and friend links of this site are all taken care of by the "guy". Otherwise, with my wooden head, I would probably have to struggle for a long time. The general steps are as follows:

  • Create a "Talk" under the article category directory, and set the slug as: shuoshuo
  • Add the following code to the functions.php file of the theme:
/*Exclude showing "Talk" on the homepage*/
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');
  • Create a PHP file, you can name it template-shuoshuo, and add the following code:
<?php
/*
Template Name: Talk
*/

get_header(); // Call the header part of the theme
?>


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

        <?php
        // Get information about the "Talk" category
        $category = get_category_by_slug('shuoshuo');

        // Query the list of articles belonging to the "Talk" category
        $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();
                // Display the content of each article
                ?>
                <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(__('Leave a comment', 'your-theme'), __('1 Comment', 'your-theme'), __('% Comments', 'your-theme')); ?>
                                
                            </div>
                            <br>
                        <?php endif; ?>
                    </footer>
                </article>
                <?php
            }

            // Display pagination links
            echo '<div class="pagination">';
            echo paginate_links(array(
                'total' => $query->max_num_pages
            ));
            echo '</div>';
        }
        ?>

    </main>
</div>

<?php
get_footer(); // Call the footer part of the theme

Save and upload it to the root directory of the theme.

  • Add it to the Wordpress backend, create a new page in "Pages", choose the talk template that was just created, save it with the name "Talk", and then add the newly created page to the menu. After saving, refresh the homepage to see the effect. Of course, don't forget to create a new article with any name, because the talk page only calls the content of the article.

The plugin-free implementation of the talk function mentioned in this article is to exclude a category from being displayed on the homepage, and output this category to the specified page according to the requirements. It has not been beautified yet (I only have a little CSS level, it's really... can't say 233), and you can remove the comments if you don't need them (the interface you enter when you click on the comments should look familiar - isn't it the article page?). As for calling Memos, I will study it when I find a host someday, maybe I will squeeze it in.

After reading it, I feel that there is nothing interesting. I'm lazy, and my blog updates slowly. Will I update Memos frequently? I guess it's difficult, I'm not used to it. So I think it's more exciting to sit down and write a few articles if I have the time, right?

2024-01-14 In the end, I changed to Memos. With this thing, I can send it on my mobile phone, which is convenient. It is currently debugged and ready to use.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.