WordPress添加自定义文章类型

WordPress添加自定义文章类型,第1张

概述wordpress默认的文章类型是Post,需要增加自己个性的类型吗,如本站的说说,是一个像写心情一样的文章类型,简单记录某时的心情或感受。

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。内存溢出小编现在分享给大家,也给大家做个参考。

wordpress 默认的文章类型是 Post,需要增加自己个性的类型吗,如本站的说说,是一个像写心情一样的文章类型,简单记录某时的心情或感受。这个模块好早就弄好了,一直没有时间分享出来,今晚就分享它,让有需要的朋友们可以自己弄一个喜欢的个性类型。


在 functions.PHP 最后加入以下代码,首先要创建一个文章类型,这里的代码执行了在后台左侧菜单中创立了一个说说的文章类型入口,包括说说有列表、发表说说等。有几个参数要说明下:

1.menu_position => 9;这个是用来显示说说菜单图标出现的位置。9 是在“文章”这个菜单下。

2.menu_icon => home_url( ‘app.png’,__file__ );显示在菜单中的图标;

//说说

add_action('init','my_custom_init');

function my_custom_init(){

$labels = array( 'name' => '说说',

'singular_name' => '说说',

'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,

'exclude_from_search' =>true,

'query_var' => true,

'rewrite' => true,

'capability_type' => 'post',

'has_archive' => false,

'hIErarchical' => false,

'menu_position' => 9,

'menu_icon' => home_url( 'app.png',__file__ ),

'supports' => array('editor','author','Title','custom-fIElds','excerpt','comments') );

register_post_type('shuoshuo',$args);

}

创建一个页面:shuoshuo 在主题 page 文件夹下创建一个页面,用来显示说说的内容,这里用到的图片都是在主题配置中有相关设置的,如果你主题中没有这些设置,直接换成图片的路径就是。

<?PHP

/*

Template name: 说说

 */

get_header(); ?>

<style>

/*说样式*/

body {

background: url(<?PHP echo cs_get_option('i_shuoshuo_bg'); ?>) no-repeat;

background-size: cover;

background-position: center top;

background-attachment: fixed;

}

.ss-Title {

background: #8BBF5D;

display: inline-block;

padding: 5px 15px;

color: #fff;

Font-weight: normal;

margin: 0;

}

.ss-div {

padding: 0;

overflow: hIDden;

border-bottom: 1px solID #8BBF5D;

margin: 20px;

}

.ss-ny {

display: inline-block;

wIDth: calc(100% - 150px);

text-align: right;

padding: 0;

margin: 0;

vertical-align: mIDdle;

}

.ss-ny p {

margin: 0;

color: #fff;

Font-size: 14px;

overflow: hIDden;

white-space: nowrap;

text-overflow: ellipsis;

}

.ss-ul{List-style:none;padding:0 20px;position:relative;}

.ss-ul p{margin:0;}

.ss-ul::after {

content: '';

position: absolute;

right: 140px;

top:0;

height: 100%;

wIDth: 50px;

background: url(<?PHP echo cs_get_option('i_shuoshuo_bd'); ?>) repeat-y center top 0/50px;

}

.ss-li {

background: rgba(255,255,.2);

border-radius: 5px;

position: relative;

padding: 20px 0;

margin: 20px 0;

wIDth: calc(100% - 200px);

min-height: 150px;

Box-sizing: content-Box

}

.ss-li::before {

content: '';

wIDth: 100%;

height: 100%;

position: absolute;

left: 0;

top: 0;

-webkit-filter:blur(20px);

filter: blur(20px);

z-index: -1;

}

.ss-li::after {

content: '';

wIDth: 0px;

height: 0px;

border-style: solID;

border-wIDth: 0px 0 25px 25px;

border-color: transparent transparent transparent rgba(255,.2);

position: absolute;

right: -25px;

top: 40px;

}

.ss-li img {

wIDth: 150px;

float: left;

margin: 0 20px;

height: 150px;

border-radius: 10px;

}

.ss-li:hover .ss-time{background:rgba(0,.5);}

.ss-li:hover .ss-time:before{border-right-color:rgba(0,.5);}

.ss-lix::before {

content: "";

wIDth: 0px;

height: 0px;

border-style: solID;

border-wIDth: 0px 0 20px 22px;

border-color: transparent transparent transparent #fff;

position: absolute;

left: 730px;

top: 23px;

}

.ss-txt {

Font-size: 16px;

line-height: 1.7;

text-indent: 2em;

text-align: justify;

color: #fff;

padding: 0 20px;

position: absolute;

left: 170px;

top: 50%;

-webkit-transform: translateY(-50%);

transform: translateY(-50%);

}

.ss-time {

position: absolute;

right: -180px;

top: 0px;

text-align: center;

color: #fff;

Font-size: 14px;

Transition: all .3s linear;

padding: 10px;

Box-sizing: content-Box;

border-radius: 10px;

}

.ss-time::before {

content: "";

position: absolute;

left: -20px;

top: 35px;

border: 10px solID transparent;

border-right-color: inherit;

Transition: all .3s linear;

}

.ss-d{

display: block;

Font-size: 3em;

Font-weight: bold;

text-align: center;

}

.ss-ym{

Font-size: 12px;

text-align: center;

}

.ss-like{

position: absolute;

right: 10px;

bottom:10px;

color:#fff;

}

.ss-zz{

position: absolute;

right: 50px;

bottom: 8px;

height: 24px;

line-height: 24px;

color: #fff;

vertical-align: mIDdle;

}

.ss-zz img{

wIDth: 24px;

height: 24px;

border-radius: 50%;

margin-right: 5px;

}

@media(max-wIDth:1023px){

.ss-div{margin:20px 0;}

.ss-Title{padding:2px 5px;Font-size:16px;}

.ss-ny{wIDth: calc(100% - 80px);}

.ss-ul{padding:0;}

.ss-ul:after{display:none;}

.ss-li{wIDth:100%;min-height:0;}

.ss-li:after{display:none;}

.ss-txt{position:static;padding:0 10px 20px 10px;min-height:95px;-webkit-transform:none;transform:none;}

.ss-time{position:static;border-radius:0;Font-size:12px;float:right;margin-left:5px;margin-right:5px;margin-top:-25px;border-top:5px solID #fff;}

.ss-time::before{display:none;}

.ss-h{display:none;}

}

</style>

<main ID="main">

<div >

<h1 ><?PHP the_Title(); ?></h1>

<?PHP if (have_posts()): ?>

<?PHP while (have_posts()) : the_post(); ?>

<article >

<?PHP the_content(); ?>

</article>

<?PHP enDWhile; ?>

<?PHP endif; ?>

</div>

<?PHP

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(

'post_type' =>'shuoshuo',//显示哪些类型的文章

'post_status' =>'publish',//显示发布的

'posts_per_page' => cs_get_option('i_shuoshuo_per_page'),//每页显示几条记录,在主题后台设置

'paged' => $paged,

);

query_posts($args);

?>

<?PHP if (have_posts()) : ?>

<ul > <!--循环的样式一定要有:post-wrap-->

<?PHP while (have_posts()) : the_post(); ?>

<li >

<?PHP get_template_part('template-parts/post/diy','shuoshuo'); ?>

</li>

<?PHP enDWhile; ?>

</ul>

<?PHP get_template_part('template-parts/pagination'); ?>

<?PHP endif;?>

</main>

<?PHP get_footer(); ?>

创建显示说说列表的模板,因本站的说说要用到 AJAX 加载,所以把显示的内容弄成一个模板,方便动态加载。在 template-parts/post 中新建一个 PHP,名为:diy-shuoshuo.PHP

<P overflow:hidden;wIDth:180px;"><img src="<?PHP echo catchFirstimg(); ?>"></P>

<div ><span ><?PHP the_time('j'); ?></span><span ><?PHP the_time('Y年m月'); ?></span></div>

<P ><?PHP echo get_post_excerpt('',250); ?></P>

<a href="<?PHP echo get_author_posts_url( get_the_author_Meta( 'ID' ) ) ?>" target="_blank">

<?PHP echo get_avatar( get_the_author_Meta('email'),'' ); ?>

<?PHP echo get_the_author() ?>

</a>

<a href="JavaScript:;" data-action="ding" data-ID="<?PHP the_ID(); ?>" >

<?PHP if(isset($_cookie['bigfa_ding_'.$post->ID])): ?>

<i ></i>

<?PHP else: ?>

<i ></i>

<?PHP endif; ?>

<span >

<?PHP if(get_post_meta($post->ID,'bigfa_ding',true)): ?>

<?PHP echo get_post_meta($post->ID,true); ?>

<?PHP else: ?>

<?PHP echo '0'; ?>

<?PHP endif; ?>

</span>

</a>

后台-页面-新建页面,最后在后台页面功能下,新建一个页面,模析就选刚才创建好的“说说”,保存就行。然后添加这个页面到网站的菜单上。要写说说,就像写文章一样,发表说说,CSS 样式都在以上代码中了,可根据自己的风格修改。


整体说说显示效果:



后台说说菜单:


以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的WordPress添加自定义文章类型全部内容,希望文章能够帮你解决WordPress添加自定义文章类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zz/1009650.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存