使用版本:5.2.2

涉及文件:

  1. /wp-content/themes/主题/functions.php
  2. /wp-content/themes/主题/page_shuoshuo.php
  3. /wp-content/themes/主题/css/shuoshuo.css

1,在 functions.php 文件内容的最后加入以下代码:

  1. // 说说
  2. function shuoshuo_init()
  3. {
  4. $labels = [
  5. 'name' => '说说',
  6. 'singular_name' => '说说',
  7. 'all_items' => '所有说说',
  8. 'add_new' => '发表说说',
  9. 'add_new_item' => '撰写新说说',
  10. 'edit_item' => '编辑说说',
  11. 'new_item' => '新说说',
  12. 'view_item' => '查看说说',
  13. 'search_items' => '搜索说说',
  14. 'not_found' => '暂无说说',
  15. 'not_found_in_trash' => '没有已遗弃的说说',
  16. 'parent_item_colon' => '',
  17. 'menu_name' => '说说'
  18. ];
  19. $args = [
  20. 'labels' => $labels,
  21. 'public' => true,
  22. 'publicly_queryable' => true,
  23. 'show_ui' => true,
  24. 'show_in_menu' => true,
  25. 'query_var' => true,
  26. 'rewrite' => true,
  27. 'capability_type' => 'post',
  28. 'has_archive' => true,
  29. 'hierarchical' => false,
  30. 'menu_position' => null,
  31. 'supports' => array('title','editor','author')
  32. ];
  33. register_post_type('shuoshuo', $args);
  34. }
  35. add_action('init', 'shuoshuo_init');

关于 register_post_type() 函数的使用,你可以查阅这篇文章:https://developer.wordpress.org/reference/functions/register_post_type/
添加代码后,进入后台,即可看到说说模块。

2,创建 page_shuoshuo.php 文件

  1. <?php
  2. /**
  3. * Template Name: 说说
  4. */
  5. get_header(); ?>
  6. <?php get_header('masthead'); ?>
  7. <div id="main" class="container">
  8. <div class="tags-title">说说</div>
  9. <link href="/wp-content/themes/duomeng/css/shuoshuo.css" rel="stylesheet">
  10. <div class="shuoshuo">
  11. <?php query_posts("post_type=shuoshuo&post_status=publish&paged=".$wp_query->query['paged']);if (have_posts()) : while (have_posts()) : the_post(); ?>
  12. <div class="shuoshuo-item">
  13. <div class="shuoshuo-date"><?php the_time('Y年n月j日 G:i'); ?></div>
  14. <div class="shuoshuo-content"><?php the_content(); ?></div>
  15. </div>
  16. <?php endwhile;endif; ?>
  17. </div>
  18. <div class="shuoshuo-page">
  19. <?php dmeng_paginate(); ?>
  20. </div>
  21. </div>
  22. <?php get_footer('colophon'); ?>
  23. <?php get_footer(); ?>

3,创建 shuoshuo.css 文件

.shuoshuo {
    width: 100%;
    overflow: hidden;
    margin-top: 20px;
}
.shuoshuo-item {
    width: 100%;
    padding: 15px 15px;
    display: block;
    box-shadow: 0 0 3px RGBA(0,0,0,.15);
    background-color: #f8f8f8;
    border:1px #eee solid;
    border-radius: 4px;
    font-size: 14px;
    letter-spacing: 1px;
    color: #666;
    min-height:60px;
    position: relative;
}
.shuoshuo-item:not(:first-child) {
    margin-top: 20px;
}
.shuoshuo-date {
}
.shuoshuo-content {
    margin: 15px 0 0 0;
}
.shuoshuo-content img {
    max-width: 100%;
    height: auto;
}

4,创建说说

在后台新建一个页面,页面模板选择说说。
在说说模块中添加测试说说。本文说说页面中显示的是内容,当然你也可以修改代码显示标题等。


这里是演示页面:https://blog.lh1010.com/shuoshuo/
可通过修改 shuoshuo.css 文件,来设计不同的说说页面样式。