最近访客
百度未收录

WordPress 添加自定义类型(附带类型的分类)

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

WordPress 添加自定义类型(附带类型的分类)-狐狸库

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

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

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

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

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

//增加分类
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 代码。

<?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目录,接着将每个栏目的核心循环文件都放在这里,方便其他页面可以直接复用。

<?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 并没有生效,所以先转自过来留个坑,避免以后遇到的时候再去寻找。

温馨提示: 本文最后更新于2022/10/20 03:49:10。若文章内容或图片失效,请 留言联系站长反馈!
!
也想出现在这里? 联系我们
创意广告
© 版权声明
THE END
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容