WordPress 怎么添加自定义类型?

2022年10月9日 发表评论
腾讯云正在大促:点击直达 阿里云超级红包:点击领取
免费/便宜/高性价比服务器汇总入口(已更新):点击这里了解

我们知道,在WordPress中,只有一个文章类可以使用。格式也是根据固定连接来定义。

如果需要加一个发文类型应该如何做呢?

打个比方:默认的程序是有【写文章】,但是我想要在发文的下面在一个【收藏夹】的类型。

后台添加收藏夹发布这个功能(另外一个发布路径)

我们只需要将以下代码加入到主题文件 functions.php

  1. function collect_register() {$link_labels = array('name' => '收藏夹''singular_name' => '收藏夹''add_new' => '新建收藏网站''add_new_item' => '新建一个收藏网站''edit_item' => '编辑收藏网站''new_item' => '新收藏网站''all_items' => '所有收藏夹''view_item' => '查看收藏夹''search_items' => '搜索收藏夹''not_found' => '没有找到相关网站''not_found_in_trash' => '回收站里面没有相关网站''parent_item_colon' => '''menu_name' => '收藏夹');$args = array('labels' => $link_labels'description' => '''public' => true'menu_position' => 5'menu_icon' => 'dashicons-tag' //图标'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),'has_archive' => true);//collect: 文章类型名称 register_post_type( 'collect' , $args );}add_action('init', 'collect_register');

添加过后,你在WP的后台管理页面就可以看到一个收藏夹的类型功能。可以进入收藏夹单独添加文章。

而在这个收藏夹功能中发布的文章,URL格式为:xxx.com/collect/111.html

这么表现就区别了之前的固态链接。如,我原本的固态链接是xxx.com/111.html

新增关于新类型(收藏夹)的分类

有了这个功能,在这个功能下面在加上一些分类。

同样还是在 functions.php

  1. //增加分类function collect_class() {$labels = array('name' => '收藏夹分类','singular_name' => '收藏夹分类','search_items' => __( '搜索收藏夹分类' ),'all_items' => __( '所有收藏夹分类' ),'parent_item' => __( '该收藏夹分类的上级分类' ),'parent_item_colon' => __( '该收藏夹分类的上级分类:' ),'edit_item' => __( '编辑收藏夹分类' ),'update_item' => __( '更新收藏夹分类' ),'add_new_item' => __( '添加新的收藏夹分类' ),'new_item_name' => __( '新收藏夹分类' ),'menu_name' => __( '收藏夹分类' ),);$args = array('labels' => $labels,'hierarchical' => true,);//collect: 文章类型名称//collect_category: 文章分类名称register_taxonomy( 'collect_category', 'collect', $args );}add_action( 'init', 'collect_class', 0 );

新增一个分类模板

在你的模板目录下面新建一个 taxonomy-collect_category.php 的文件。

这个文件就是你新增文章类型的模板,格式为:taxonomy-文章类型名称_category.php

然后就可以愉快的写正常的文章循环了,以下是我的收藏夹循环,可根据代码自行调整:

taxonomy-collect_category.php 代码。

  1. <?php get_header(); ?> <main class="main-page"> <div class="main-page-banner"> <div class="banner-con"> <h1 class="title"><?php echo '收藏夹' ?></h1> <p><?php echo '站长的一些足迹' ?></p> </div> </div> <div class="layui-container main-collect"> <div class="layui-row"> <div class="layui-col-md2"> <div class="left-nav"> <div class="tt">收藏夹分类</div> <div class="nav-list"> <?php wp_nav_menu( array( 'theme_location' => 'collect_menu' )); ?> </div> </div> </div> <div class="layui-col-md10"> <div class="layui-row layui-col-space15"> <?php get_template_part( 'template-parts/page/collect' get_post_format() ); ?> </div> <div class="layui-row main-page-nav"> <div class="layui-col-md12"> <?php the_posts_pagination(); ?> </div> </div> </div> </div> </div> </main><?php get_footer(); ?>

template-parts/page/collect 目录下的 collect.php

注:wordpress这个模板功能很强大,可以在所有文件内任意调用文件,我这里就是在主题目录下新建了template-parts目录,接着将每个栏目的核心循环文件都放在这里,方便其他页面可以直接复用。

  1. <?php //获取页面page用于翻页 $limit = (get_query_var('posts_per_page')) ? get_query_var('posts_per_page') : get_option('posts_per_page'); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //分类 $args = array( 'limit' => $limit, 'paged' => $paged, 'post_type' => 'collect', 'order' => 'DESC', 'posts_per_page' => 48, ); query_posts($args); if( have_posts() ){while( have_posts() ){ the_post(); ?> <div class="layui-col-md3 layui-col-xs6"> <a href="<?php the_field('web_url') ?>" target="_blank"> <div class="post-card"> <div class="logo-con"> <img class="logo" src="<?php the_field('web_logo') ?>"> <div class="title"><?php the_title(); ?></div> <div class="introduction"><?php the_field('web_introduction') ?></div> </div> </div> </a> </div> <?php} } wp_reset_postdata(); ?>

该篇文章转自 天边鱼Blog ,之前好像因为想做域名分类暂时收藏。

但是实际将上面的代码放入 functions.php 并没有生效,所以先转自过来留个坑,避免以后遇到的时候再去寻找。

腾讯云正在大促:点击 https://2bcd.com/go/tx/进入最新活动页】领取无门槛代金券,附云服务器价格表,2核2G4M轻量应用服务器99元1年,新老用户都可以买,可以享受1次续费99元一年、135元15个月、三年560元,MySQL云数据库59元1年起,2核2G3M配置82元1年,2核4G5M配置188元一年、3年900元,4核8G12M轻量服务器880元15个月,8核16G配置1890元15个月,更多16核32G28M带宽和云服务器CVM标准型S5、GPU服务器、CVM标准型SA2租用优惠价格如下,可以 点此进入最新活动页 查看当前最新的优惠券和活动信息。还可以领下10元无门槛代金券:点此直达 阿里云限量超级红包:点击领取】5亿上云补贴和2088元满减代金券,阿里云服务器租用费用最新价格表【点击了解】,最便宜轻量应用服务器2核2G3M带宽82元1年,ECS云服务器2核2G3M带宽99元一年,ECS u1实例2核4G5M带宽优惠价格199元一年,香港30M带宽轻量服务器24元1个月、288元一年,4核8G服务器706元一年,ECS云服务器4核16G10M带宽30元1个月、90元3个月,云服务器8核32G10M带宽109元1个月、327元3个月。阿里云产品最高降价55%,点击 https://2bcd.com/go/aliyun/ 进入最新活动页了解。 腾讯云续费贵,一次性买3年/5年,免得续费贵。3年轻量 2核2G 4M 560元;3年轻量 2核4G 5M 3年900元,活动入口:点击前往(下拉到“爆品专区”即可看到)。 老用户享新人优惠的方法:用Qq登录、1人可注册3个新账号/用家人朋友的身份注册新号(点击注册新账号)。

发表评论

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