查询自定义帖子类型?
3 个回答
- 投票数
-
- 2011-01-06
query_posts( array( 'post_type' => array('post', 'portfolio') ) );
同时显示普通帖子和
内的帖子portfolio
类型或
query_posts('post_type=portfolio');
仅适用于
portfolio
.用作常规WP查询-阅读法典: http://codex.wordpress.org/Function_Reference/query_posts#用法和 http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters
<?php query_posts(array( 'post_type' => 'portfolio', 'showposts' => 10 ) ); ?> <?php while (have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <p><?php echo get_the_excerpt(); ?></p> <?php endwhile;?>
query_posts( array( 'post_type' => array('post', 'portfolio') ) );
which shows both normal posts and posts inside
portfolio
typeor
query_posts('post_type=portfolio');
for only
portfolio
.Use as normal WP Query - read the Codex: http://codex.wordpress.org/Function_Reference/query_posts#Usage and http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters
<?php query_posts(array( 'post_type' => 'portfolio', 'showposts' => 10 ) ); ?> <?php while (have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <p><?php echo get_the_excerpt(); ?></p> <?php endwhile;?>
-
这是一个相当老的答案-但要明确一点,这不是您应该这样做的方法.它几乎不可避免地会导致404和许多其他问题.请参阅@kaiser的答案或[此帖子,说明为什么不应该使用`query_posts()`]](http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts和预贴帖子/50762#50762)This is a fairly old answer - but to be clear, there is not the way you should being doing this. It will almost inevitably lead to 404s and a host of other problems. Please see @kaiser's answers or [this post on why you shouldn't use `query_posts()`](http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts/50762#50762)
- 7
- 2013-05-28
- Stephen Harris
-
- 2013-05-27
主要答案使用
query_posts()
作为主要答案,应该从不完成.使用过滤器
使用
pre_get_posts
过滤器,并为主要查询设置portfolio
帖子类型.使用条件标签来确定要在哪里放置此过滤器.快速示例
<?php defined( 'ABSPATH' ) OR exit; /* Plugin Name: (#6417) "Portfolio" post type in query */ add_filter( 'pre_get_posts', 'wpse_6417_portfolio_posts' ); function wpse_6417_portfolio_posts( $query ) { if ( ! $query->is_main_query() // Here we can check for all Conditional Tags OR ! $query->is_archive() // For e.g.: Every archive will feature both post types ) return $query; $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
免责声明
上面的代码是一个插件,但是可以简单地塞入您主题的
functions.php
文件中(建议不).Late answer as the main answer uses
query_posts()
, which should never be done.Use a filter
Use the
pre_get_posts
filter and just set theportfolio
post type for the main query. Use Conditional Tags to determine where you want to have this filter.Quick Example
<?php defined( 'ABSPATH' ) OR exit; /* Plugin Name: (#6417) "Portfolio" post type in query */ add_filter( 'pre_get_posts', 'wpse_6417_portfolio_posts' ); function wpse_6417_portfolio_posts( $query ) { if ( ! $query->is_main_query() // Here we can check for all Conditional Tags OR ! $query->is_archive() // For e.g.: Every archive will feature both post types ) return $query; $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
Disclaimer
The above code is a plugin, but can simply get stuffed in the
functions.php
file of your theme (which is not recommended).-
为什么不建议将其添加到功能中?当然,如果站点管理员更改了主题,则他们将需要解决如何在带有该新主题的主页上显示投资组合.因此,我想说的是,将其添加到函数中而不是插件中也同样有效.还是我错过了什么?why is it not recommended to add it to functions? Surely, if the site admin changes the theme they would need to address how to display the portfolio on the home page with this new theme anyway. So, I would say it is just as valid to add this in functions rather than a plugin. Or am I missing something?
- 0
- 2016-11-29
- Phill Healey
-
@PhillHealey正如您所说,数据将是不可见的,您将不得不复制并粘贴代码.最好通过插件来对查询进行大量的逻辑修改,而显示和样式应保留在主题中.@PhillHealey As you said, the data would be invisible and you would have to copy and paste the code around. Heavy, logic modifications to queries are best served via plugins, while displaying and styling should be kept in themes.
- 0
- 2016-11-29
- kaiser
-
如果该代码特定于主题,则不是.Not if that code is specific to the theme.
- 0
- 2016-12-03
- Phill Healey
-
@PhillHealey帖子类型不应**绝不特定于主题.@PhillHealey A post type should **never** be specific to a theme.
- 0
- 2016-12-04
- kaiser
-
好的,如果您想对绝对值进行针锋相对,那就很好.但是,说没有针对设计的代码不应推送到插件是不正确的.很多时候不合适.Ok, if you want to get in some tit-for-tat over absolutes then fine. However, It's just not correct to say that none design specific code should be pushed out to a plugin. There are lots of times when that's not appropriate.
- 0
- 2016-12-05
- Phill Healey
-
- 2013-12-11
将此代码添加到子主题功能文件(推荐),以将单个CPT页面添加到主循环中
add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' ); function add_custom_post_types_to_loop( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
来源 http://codex.wordpress.org/Post_Types
或创建一个自定义的archive-portfolio.php页面模板,该模板只会显示您的CPT页面.仅当您尚未使用插件设置添加存档页面时,才需要执行此操作.
示例:" has_archive"=>true,
您还可以使用以下代码控制要显示的页面数以及在存档页面上显示页面的顺序:
add_action( 'pre_get_posts', 'cpt_items' ); function cpt_items( $query ) { if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) { $query->set( 'posts_per_page', '8' ); $query->set( 'order', 'ASC' ); } }
Add this code to your child themes functions file (recommended) to add your single CPT pages to your main loop
add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' ); function add_custom_post_types_to_loop( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
Source http://codex.wordpress.org/Post_Types
Or create a custom archive-portfolio.php page template which will only display your CPT pages. This only needs to be done if you haven't added a archive page using the plugin settings.
Example: 'has_archive' => true,
You can also control how many pages are displayed and the order in which they're displayed on the archive page using this code:
add_action( 'pre_get_posts', 'cpt_items' ); function cpt_items( $query ) { if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) { $query->set( 'posts_per_page', '8' ); $query->set( 'order', 'ASC' ); } }
我已经安装了自定义帖子类型UI插件.激活此插件后,我创建了一个名为
portfolio
的自定义帖子类型.现在,我想在前端的投资组合页面上使用它.如何获取自定义帖子类型为portfolio
的所有帖子?