用于识别多个自定义帖子类型的数组?(Array for recognizing multiple custom post types?)

编程入门 行业动态 更新时间:2024-10-24 14:22:48
用于识别多个自定义帖子类型的数组?(Array for recognizing multiple custom post types?)

我目前正在使用underscores.me框架来开发一个wordpress主题。 我会在主题中使用一些自定义帖子类型,随着时间的推移会添加更多,所以我想知道如何制作一个数组来获取各种模板文件。 目前在我的index.php上我使用以下 -

<?php if( array('movies','books') != get_post_type() ) { get_template_part( 'content', get_post_format() );} ?> <?php if( 'movies' == get_post_type() ) { get_template_part( 'content', 'movies' );} elseif( 'books' == get_post_type() ) { get_template_part( 'content', 'books' );} ?>

我必须让content-movies.php文件的if参数最终被识别,我想要的是

a>使非自定义帖子类型的帖子只使用content.php

b>一个if数组,用于查找各种帖子类型,例如电影,书籍,社论

c>使每个自定义帖子类型查找其适当的内容 - [post type] .php AND single- [post type] .php

到目前为止,我已经成功实现了一个>但是对于b&c我遇到的问题是我的第二个帖子类型'books',我仍然使用content.php文件而不是content-books.php。 [编辑]要明确它导致索引中出现的书帖类型重复,一个使用content-books.php,另一个使用content.php

我还必须在functions.php文件中添加一些自定义代码以使CPT正常工作,并且如果怀疑它是原因,也可以显示它。

I'm currently using the underscores.me framework to develop a wordpress theme. I'll be using a handful of custom post types in the theme, with more being added as time progresses so am wondering how to make an array to get the various template files to be recognized. Currently on my index.php I am using the following -

<?php if( array('movies','books') != get_post_type() ) { get_template_part( 'content', get_post_format() );} ?> <?php if( 'movies' == get_post_type() ) { get_template_part( 'content', 'movies' );} elseif( 'books' == get_post_type() ) { get_template_part( 'content', 'books' );} ?>

I had to make the if arguments for the content-movies.php file to be finally recognized, and what I'd like is

a> to make the non custom post type posts only use content.php

b> an if array to look for the various post types e.g. movies, books, editorials

c> to make each custom post type look for its appropriate content-[post type].php AND single-[post type].php

So far I've been succesful in achieving a> but for b & c the issue I'm having is that my second post type,'books', i still using the content.php file instead of content-books.php. [EDIT] to be clear it's causing a duplicate of book post types appearing in the index, one using content-books.php and the other using content.php

I also had to add some custom code to the functions.php files to get CPT's working, and can show that too if it's suspected to be the cause.

最满意答案

尝试

<?php if( in_array('movies','books') != get_post_type() ) { get_template_part( 'content', get_post_format() );} ?> <?php if( 'movies' == get_post_type() ) { get_template_part( 'content', 'movies' );} elseif( 'books' == get_post_type() ) { get_template_part( 'content', 'books' );} ?>

Try

<?php if( in_array('movies','books') != get_post_type() ) { get_template_part( 'content', get_post_format() );} ?> <?php if( 'movies' == get_post_type() ) { get_template_part( 'content', 'movies' );} elseif( 'books' == get_post_type() ) { get_template_part( 'content', 'books' );} ?>

更多推荐

本文发布于:2023-07-28 23:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   自定义   数组   类型   帖子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!