【问题标题】:Wordpress + multi custom post + one loopWordpress + 多自定义帖子 + 一个循环
【发布时间】:2015-04-27 13:42:42
【问题描述】:

我有一个关于 worpdress 和自定义帖子类型的问题。我创建了一个具有 2 种自定义类型的工作板,一种用于注册帐户的公司,另一种用于发布任务的公司。所以一个公司可以有多个任务。

我的问题是,是否可以在一个循环中拥有多个自定义帖子类型?

更多细节,每个帖子类型都有一个像<?php the_title();?> 这样的标题,我无法在一个循环中显示每个自定义帖子的每个标题。我什么都试过了。

【问题讨论】:

  • 您可以在一个循环中使用任意数量的帖子类型。您只需将 WP_Query 的“post_type”参数用作数组。这是一个例子,'post_type' => array('post_type_one', 'post_type_two')
  • 是的,我知道,但是如何在同一个循环中显示两种自定义帖子类型的标题?
  • the_title() 函数将显示标题,无论帖子类型如何。但是,如果您需要类似....两种帖子类型的不同布局等,您可以使用循环内的“if”条件检查帖子类型。
  • 所以我可以这样做例如:如果 post-type = "a thing" 显示对应的 the_title()?

标签: php wordpress loops title custom-type


【解决方案1】:

您可以在 wp_query 的 args 列表中使用一组帖子类型。

$args = array(
/** other arguments here **/
'post_type' => array('post_type_one', 'post_type_two'
)
$results = new WP_Query($args);
if($results->have_posts()) : while($results->have_posts()): $results->the_post();
/** if you need different loops for different post types */
if(get_post_type() == 'post_type_one'){
the_title(true);
/** your rest of content code here - for post type one */
} else {
the_title(true);
/** content for the second post type */
}
endwhile; endif;

我认为你可以管理其他所有事情。

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多