wordpress给分类目录自定义模版或说说

2019年10月3日18:48:42 发表评论 1,245 views

之前我发布的wordpress微语说说功能(http://www.wdooc.com/archives/2649)存在很多我不喜欢的问题。

1、无法用wordpress自带的搜索功能搜索到文章。

2、需要单独的分类目录和分类标签

3、想要在网站中使用它们的分类目录标签这些,需要单独开发它们。

4、用了这么久,发现百度居然不收录。

当然它们的优缺点各有各的,只要对于会开发的大神来说这些都是小问题,所以我们老百姓就得用我们老百姓的办法来。

 

我今天研究的这个是直接有自带的文章功能改成说说的样子,这样分类目录,标签,搜索等全部受到影响。

首先在functions.php文件中添加如下代码:

// 分类选择模板
class Select_Category_Template{
	public function __construct() {
		add_filter( 'category_template', array($this,'get_custom_category_template' ));
		add_action ( 'edit_category_form_fields', array($this,'category_template_meta_box'));
		add_action( 'category_add_form_fields', array( &$this, 'category_template_meta_box') );
		add_action( 'created_category', array( &$this, 'save_category_template' ));
		add_action ( 'edited_category', array($this,'save_category_template'));
		do_action('Custom_Category_Template_constructor',$this);
	}
 
	// 添加表单到分类编辑页面
	public function category_template_meta_box( $tag ) {
		$t_id = $tag->term_id;
		$cat_meta = get_option( "category_templates");
		$template = isset($cat_meta[$t_id]) ? $cat_meta[$t_id] : false;
		?>
		<tr class="form-field">
			<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Template'); ?></label></th>
			<td>
				<select name="cat_template" id="cat_template">
					<option value='default'><?php _e('Default Template'); ?></option>
					<?php page_template_dropdown($template); ?>
				</select>
				<br />
				<span class="description"><?php _e('为此分类选择一个模板'); ?></span>
			</td>
		</tr>
		<?php
		do_action('Custom_Category_Template_ADD_FIELDS',$tag);
	}
 
	// 保存表单
	public function save_category_template( $term_id ) {
		if ( isset( $_POST['cat_template'] )) {
			$cat_meta = get_option( "category_templates");
			$cat_meta[$term_id] = $_POST['cat_template'];
			update_option( "category_templates", $cat_meta );
			do_action('Custom_Category_Template_SAVE_FIELDS',$term_id);
		}
	}
 
	// 处理选择的分类模板
	function get_custom_category_template( $category_template ) {
		$cat_ID = absint( get_query_var('cat') );
		$cat_meta = get_option('category_templates');
		if (isset($cat_meta[$cat_ID]) && $cat_meta[$cat_ID] != 'default' ){
			$temp = locate_template($cat_meta[$cat_ID]);
			if (!empty($temp))
				return apply_filters("Custom_Category_Template_found",$temp);
		}
		return $category_template;
	}
}
 
$cat_template = new Select_Category_Template();

wordpress给分类目录自定义模版或说说

这样就可在分类目录中选择相应的模版了。

下一步就需要添加一个模版文件,首先创建一个shuoshuo.php的文件,放入主题的根目录下,然后加入如下我正在使用的代码。

<?php /*
    Template Name: 说说
    author: 五道博客
    url: http://www.wdooc.com/archives/3612
    */
    get_header(); ?>
<style type="text/css">
    #shuoshuo_content {
        background-color: #fff;
        padding: 10px;
        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;
        /* 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);

        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;
    }
    /* 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>
</head>
<body>
<div id="primary" class="content-area" style="">
    <main id="main" class="site-main" role="main">
        <div id="shuoshuo_content">
            <ul class="cbp_tmtimeline">
                <?php
					if (have_posts()) : while (have_posts()) : the_post(); ?>
                <li> <span class="shuoshuo_author_img"><img src="https://secure.gravatar.com/avatar/163b1a117bfb1855388b6a53dec9e471?s=128" class="avatar avatar-48" width="48" height="48"></span>
                    <div class="cbp_tmlabel" >
                        <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'); ?>
                        </p>
						<p><span style="display:  block;width: 100px;float:  right;text-align:  center;">
						<a style="font-size: 24px;background-color: rgba(0, 0, 0, 0.1);border-radius: 10px;" href="<?php echo get_permalink();?>">评论</a></span>
						</p>
						<p style="clear:  right;"></p>
                    </div>
                    <?php endwhile;?>
					<?php endif; ?>
                </li>
            </ul>
			<?php if (function_exists('wp_pagenavi')) wp_pagenavi();else { ?><div><?php previous_posts_link('【« 上一页】') ?>     <?php next_posts_link('【下一页 »】') ?></div><?php } ?>
        </div>
    </main>
    <!-- .site-main -->
</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_sidebar(); ?>
<?php get_footer();?>

最后效果演示:http://www.wdooc.com/archives/category/suibirizhi/toutoushuoshuo/

wordpress给分类目录自定义模版或说说

本代码经过本人修改,此说说代码不仅有分页功能,还可在说说中添加链接,图片,视频,音频等均不收到显示的影响,如有觉得这个样式不好看,懂CSS的可自行修改,我这个平民老百姓还可将就用一用。

 

 

历史上的今天:


欢迎来到菜鸟头头的个人博客
本文章百度已收录,若发现本站有任何侵犯您利益的内容,请及时邮件或留言联系,我会第一时间删除所有相关内容。

  • A+
所属分类:PHP

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: