【问题标题】:using multiple custom post in the same page在同一页面中使用多个自定义帖子
【发布时间】:2013-10-05 18:53:03
【问题描述】:

我对创建两个自定义帖子类型有疑问,第一个已经存在是“帖子”,第二个“产品”是我使用此代码创建的:

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'product',
array(
  'labels' => array(
    'name' => __( 'Product' ),
    'singular_name' => __( 'Product' )
  ),
  'public' => true ,  
  'supports' => array( 'title', 'editor', 'thumbnail')
)
);

 register_taxonomy( 'couleur', 'product', array( 'hierarchical' => true, 'label' =>    'Couleur', 'query_var' => true, 'rewrite' => true ) );
}
 add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {
if ( is_home() )
$query->set( 'post_type', array( 'product' ) );

return $query;
}

问题是当我从数据库中为他们俩获取帖子时,这意味着我在同一页面“index.php”中显示“帖子”和“产品”,问题是页面中只显示一个而不是它们都显示:

product show => post(default) => hide 
post(default) hide => product show 

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    将此添加到您的 functions.php 文件中

    add_filter( 'pre_get_posts', 'my_get_posts' );
    function my_get_posts( $query ) {
        if ( is_home() && $query->is_main_query() ) {
            $query->set( 'post_type', array( 'post', 'product' ) );
        }
        return $query;
    }
    

    还提到了$query->set('post_type', 'any'); here,但从未尝试过。也检查this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-30
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2015-03-19
      • 1970-01-01
      相关资源
      最近更新 更多