WordPress制作微语说说页面

首先,使用 WordPress 的 register_post_type( ) 函数注册一个新的帖子类型,方便日后编辑说说。在主题的 functions.php 文件中添加以下代码。

function my_custom_shuoshuo_init() { 
    $labels = array( 
        'name' => '说说',
        'singular_name' => '说说', 
        'all_items' => '所有说说',
        'add_new' => '发表说说', 
        'add_new_item' => '撰写新说说',
        'edit_item' => '编辑说说', 
        'new_item' => '新说说', 
        'view_item' => '查看说说', 
        'search_items' => '搜索说说', 
        'not_found' => '暂无说说', 
        'not_found_in_trash' => '没有已遗弃的说说', 
        'parent_item_colon' => '',
        'menu_name' => '说说'
    ); 
    $args = array( 
        'labels' => $labels, 
        'public' => true, 
        'publicly_queryable' => true, 
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true, 
        'rewrite' => true, 
        'capability_type' => 'post', 
        'has_archive' => true, 
        'hierarchical' => false, 
        'menu_position' => null, 
        'supports' => array('title','editor','author') 
    ); 
register_post_type('shuoshuo',$args); 
}

添加完上述函数后,就会在后台出现一个“说说”的菜单,通过菜单即可编辑说说,操作基本更平时发文章一样,但是此时编辑完成后的说说并不能在前端显示出来,这时我们就需要新建一个说说的页面模板来控制其前端显示。

<?php
/**
template name: 微语说说
*/

get_header();
get_header('abstract'); 
?>
<style type="text/css">
    #shuoshuo_content {
        background-color: #fff;
        padding: 15px;
        min-height: 500px;
    }
    /* shuo */

    body.theme-dark .cbp_tmtimeline::before {
        background: RGBA(255, 255, 255, 0.06);
    }
    ul.cbp_tmtimeline {
        padding: 0;
    }
    div class.cdp_tmlabel  li .cbp_tmlabel {
        margin-bottom: 0;
    }
    .cbp_tmtimeline {
        margin: 30px 0 0 0;
        padding: 0;
        list-style: none;
        position: relative;
    }
    /* The line */

    .cbp_tmtimeline:before {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        width: 4px;
        background: RGBA(0, 0, 0, 0.02);
        left: 80px;
        margin-left: 10px;
    }
    /* The date/time */

    .cbp_tmtimeline  li .cbp_tmtime {
        display: block;
        max-width: 70px;
        position: absolute;
    }
    .cbp_tmtimeline  li .cbp_tmtime span {
        display: block;
        text-align: right;
    }
    .cbp_tmtimeline  li .cbp_tmtime span:first-child {
        font-size: 0.9em;
        color: #bdd0db;
    }
    .cbp_tmtimeline  li .cbp_tmtime span:last-child {
        font-size: 1.2em;
        color: #F5F5F5;
    }
    .cbp_tmtimeline  li:nth-child(odd) .cbp_tmtime span:last-child {
        color: RGBA(255,248,220, 0.75);
    }
   .cbp_tmlabel  p {
        margin-bottom: 0.5em ;
    }
    /* Right content */

    .cbp_tmtimeline   li .cbp_tmlabel {
        margin: 0 0 35px 65px;
        background: #F5F5F5;
        color: #fff;
        padding: .8em 1.2em .4em 1.2em;
        font-weight: 300;
        line-height: 1.4;
        position: relative;
        border-radius: 5px;
        transition: all 0.3s ease 0s;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
        cursor: pointer;
        display: block;
    }
    .cbp_tmlabel:hover {
        transform: translateY(-3px);
        z-index: 1;
        -webkit-box-shadow: 0 15px 32px rgba(0, 0, 0, 0.15) !important
    }
    .cbp_tmtimeline   li:nth-child(odd) .cbp_tmlabel {
        background: RGBA(255,248,220, 0.75);
    }
    /* The triangle */

    .cbp_tmtimeline   li .cbp_tmlabel:after {
        right: 100%;
        border: solid transparent;
        content: " ";
        height: 0;
        width: 0;
        position: absolute;
        pointer-events: none;
        border-right-color: #F5F5F5;
        border-width: 10px;
        top: 4px;
    }
    .cbp_tmtimeline   li:nth-child(odd) .cbp_tmlabel:after {
        border-right-color: RGBA(255,248,220, 0.75);
    }
    p.shuoshuo_time {
        margin-top: 1px;
         border-top: 1px dashed #fff; 
        padding-top: 5px;
        margin-bottom: 1px;
        text-align:right;
}
    }
    /* Media */

    @media screen and (max-width: 65.375em) {
        .cbp_tmtimeline   li .cbp_tmtime span:last-child {
            font-size: 1.2em;
        }
    }
    .shuoshuo_author_img img {
        border: 1px solid #ddd;
        padding: 2px;
        float: left;
        border-radius: 4px;
        transition: all 1.0s;
        width:48px;
    }
    .avatar {
        -webkit-border-radius: 15% !important;
        -moz-border-radius: 100% !important;
        box-shadow: inset 0 -1px 0 #3333sf;
        -webkit-box-shadow: inset 0 -1px 0 #3333sf;
        -webkit-transition: 0.4s;
        -webkit-transition: -webkit-transform 0.4s ease-out;
        transition: transform 0.4s ease-out;
        -moz-transition: -moz-transform 0.4s ease-out;
    }
    .zhuan {
        transform: rotateZ(720deg);
        -webkit-transform: rotateZ(720deg);
        -moz-transform: rotateZ(720deg);
    }
    /* end */

</style>
<div id="kratos-blog-post" style="background:<?php echo kratos_option('background_index_color'); ?>">
    <div class="container">
        <div class="row">
            <?php if(kratos_option('page_side_bar')=='left_side'&&!wp_is_mobile()){ ?>
                <aside id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm scrollspy">
                    <div id="sidebar">
                        <?php dynamic_sidebar('sidebar_tool'); ?>
                    </div>
                </aside>
            <?php } ?>
            <section id="main" class='<?php echo (kratos_option('page_side_bar')=='center')?'col-md-12':'col-md-8'; ?>'>
            <?php if(have_posts()){the_post(); ?>
                <article>
                    <div class="kratos-hentry kratos-post-inner clearfix">
                        <div class="kratos-post-content">
                <!--开始-->       
        <h2 style="text-align:center;font-size:18pt">闲言碎语</h2>                
        <div id="shuoshuo_content">
            <ul class="cbp_tmtimeline">
                <?php
                        $limit = get_option('posts_per_page');
                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                        query_posts('post_type=shuoshuo&post_status=publish&showposts='.$limit=64 .'&paged=' . $paged);
                        if (have_posts()) : while (have_posts()) : the_post(); 
                ?>
                <li> <span class="shuoshuo_author_img"><img src="https://tva1.sinaimg.cn/large/a1e4b7b5gy1gdxzuhrkhdj20hs0hs0tm.jpg" class="avatar"></span>
                    <a class="cbp_tmlabel" href="javascript:void(0)">
                        <?php the_content(); ?>
                        <p class="shuoshuo_time">                               
                                <span style="font-size:12px;color:#aaaaaa;" ><?php the_time('Y年n月j日 G:i'); ?></span>
                        </p>
                    </a>
                    <?php endwhile;endif; ?>
                </li>
            </ul>
        </div>
         <!--结束-->       
                        </div>
                    </div>
                         <?php if ( wp_is_mobile() ){ ?>
                                        <!--文章分页-->
                                        <?php kratos_pages(3);?>
                                <?php } else { ?>
                                       <?php kratos_pages(6);?>
                                <?php } ?>                                   		                     
                    <?php comments_template(); ?>
                </article>
            <?php } ?>
            </section>
            <?php if(kratos_option('page_side_bar')=='right_side'&&!wp_is_mobile()){ ?>
            <aside id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm scrollspy">
                <div id="sidebar">
                    <?php dynamic_sidebar('sidebar_tool'); ?>
                </div>
            </aside>
            <?php } ?>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        var oldClass = "";
        var Obj = "";
        $(".cbp_tmtimeline li").hover(function () {
            Obj = $(this).children(".shuoshuo_author_img");
            Obj = Obj.children("img");
            oldClass = Obj.attr("class");
            var newClass = oldClass + " zhuan";
            Obj.attr("class", newClass);
        }, function () {
            Obj.attr("class", oldClass);
        })
    })
</script>
<?php get_footer(); ?>

此代码是本站是根据本站模板修改的,已经包括分页功能,但有一个BUG:如果首页有12页的话,而你的自定义页面有11页的话,那么自定义页面的第12页将会显示空白,如果不在乎的话可以忽略。

分页函数是在index.php里面复制来的,即使用的是网站首页底部那个分页模块,这个不同的主题大概率不相同,如果你不知道怎么解决分页也可以调用以下代码。

<?php 
  if (function_exists('wp_pagenavi')) 
      wp_pagenavi();
  else { ?>
    <div>
        <?php previous_posts_link('【« 上一页】') ?><?php next_posts_link('【下一页 »】') ?>
    </div>
<?php } ?>

这是使用wp的内置函数上一页下一页两个按钮调用,这个方法不会出现空白页,没有翻页数字,也不够美观,但至少是通用的。

《Wordpress制作微语说说页面》
测试地址
本站这个在没有达到分页要求前是看不到分页按钮的。


2021-10-09 删了删了,花里胡哨,还不如多发两篇普通文章。

发表回复

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