WordPress纯代码添加说说/微博/朋友圈功能

本文源代码来自萌小恩博客,切合自己主题做了些小改动,PC端和手机端也进行了微调优化。

Wordpress纯代码添加说说/微博功能

奥博说微语堂页面 www.oppblog.com/wechat

信息横飞的今天,很多人在公众的朋友圈微博反而不愿意发表任何意见,但是人偶尔的灵光总有记录下来的需求,本文结合萌小恩博客的源码,实现wordpress纯代码添加说说/微博/朋友圈功能。

以下为源码,直接套用有些主题是不太兼容,稍微修改即可。

 

添加说说功能

将以下代码加入主题的 functions.php 文件中。

//添加说说页面功能
add_action('init', 'my_custom_init'); 
function my_custom_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文件,放到主题主文件夹下,代码已经包含CSS,不需要引用别的CSS文件了,以下代码是以我使用主题改写,直接复制可能不能成功显示,按你使用的主题进行此小修改即可。当然为了适合你的主题,可以自己额外再写一部分CSS导入主题。

写入代码如下:

<?php /*
    Template Name: 说说页面
      */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
    get_header(); ?>
<style type="text/css">
    #shuoshuo_content {
        position: absolute;
		width: 650px;
        padding: 60px 15px 15px 0px;
        min-height: 500px;
    }
	#right-sidebar{margin-left:720px;}
#archives-2{display:none;}
	.site-footer{display: none;}
    /* 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;
        /* width: 29%; */
        /* padding-right: 110px; */
        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: #9BCD9B;
    }
    .cbp_tmtimeline > li:nth-child(odd) .cbp_tmtime span:last-child {
        color: RGBA(255, 125, 73, 0.75);
    }
    div.cbp_tmlabel > p {
        margin-bottom: 0;
    }
    /* Right content */
    .cbp_tmtimeline > li .cbp_tmlabel {
        margin: 0 0 45px 65px;
        background: #9BCD9B;
        color: #fff;
        padding: .8em 1.2em .4em 1.2em;
        /* font-size: 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:scale(1.05); */
        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, 125, 73, 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: #9BCD9B;
        border-width: 10px;
        top: 4px;
    }
    .cbp_tmtimeline > li:nth-child(odd) .cbp_tmlabel:after {
        border-right-color: RGBA(255, 125, 73, 0.75);
    }
    p.shuoshuo_time {
        margin-top: 10px;
        border-top: 1px dashed #fff;
        padding-top: 5px;
		font-size: 12px;
    }
    /* 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: 64px;
        transition: all 1.0s;
    }
    .avatar {
        -webkit-border-radius: 100% !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>

<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>
        <div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
			
    <main id="main" <?php generate_do_element_classes( 'main' ); ?>>	
        <div id="shuoshuo_content">
            <ul class="cbp_tmtimeline">
                <?php query_posts("post_type=shuoshuo&post_status=
                publish&posts_per_page=-1");if (have_posts()) : 
                while (have_posts()) : the_post(); ?>
                <li> <span class="shuoshuo_author_img"><img src=
                "https://www.oppblog.com/wp-content/uploads/2020/02/oppBlog.jpg" class="avatar avatar-48" width="48" 
                height="48"></span>
                    <a class="cbp_tmlabel" href="javascript:void(0)">
                        <p></p>
                        <p><?php the_content(); ?></p>
                        <p></p>
                        <p class="shuoshuo_time"><i class="fa 
                        fa-clock-o"></i>  <?php the_time('Y年n月j日 G:i'); ?>    
                        <?php the_author() ?>
                        </p>
                    </a>
                    <?php endwhile;endif; ?>
                </li>
            </ul>
        </div>
    </main>
    <!-- .site-main -->	
		</div>
<?php   
			do_action( 'generate_after_primary_content_area' );

	generate_construct_sidebars();?>

            	<?php
	get_footer();

保存上传到服务器,新建一个页面作为说说的展示页,

新建页面模板选择 “说说页面”

这个模板显示出的是说说文章内容,可以把156行的<?php the_content(); ?>替换为<?php the_title(); ?>以显示说说标题。

新建说说,就可以在新建页面看到发表的说说了。

5/5 - (1 vote)
奥博说 版权所有丨如未注明 均为原创
转载请注明:https://www.oppblog.com/archives/908

2 条评论

发表评论