显示与自定义帖子类型和分类相关的帖子时出现问题。

时间:2014-12-14 作者:user5288

基于自定义帖子类型的自定义分类法显示帖子时出现问题。我可以将所有帖子显示在自定义帖子类型中,但不仅仅是分类法。

以下是我如何注册帖子类型

function reg_forms() {
  $labels = array(
    \'name\'               => _x( \'Forms\', \'post type general name\' ),
    \'singular_name\'      => _x( \'Form\', \'post type singular name\' ),
    \'add_new\'            => _x( \'Add New\', \'Form\' ),
    \'add_new_item\'       => __( \'Add New Form\' ),
    \'edit_item\'          => __( \'Edit Form\' ),
    \'new_item\'           => __( \'New Form\' ),
    \'all_items\'          => __( \'All Forms\' ),
    \'view_item\'          => __( \'View Forms\' ),
    \'search_items\'       => __( \'Search Forms\' ),
    \'not_found\'          => __( \'No Forms found\' ),
    \'not_found_in_trash\' => __( \'No Forms found in the Trash\' ), 
    \'parent_item_colon\'  => \'\',
    \'menu_name\'          => \'Forms\'
  );
  $args = array(
    \'labels\'        => $labels,
    \'description\'   => \'Forms for download\',
    \'public\'        => true,
    \'menu_position\' => 5,
    \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
    \'has_archive\'   => true,
  );
  register_post_type( \'forms\', $args ); 
}
add_action( \'init\', \'reg_forms\' );

function my_taxonomies_product() {
  $labels = array(
    \'name\'              => _x( \'Form Categories\', \'taxonomy general name\' ),
    \'singular_name\'     => _x( \'Form Category\', \'taxonomy singular name\' ),
    \'search_items\'      => __( \'Search Form Categories\' ),
    \'all_items\'         => __( \'All Form Categories\' ),
    \'parent_item\'       => __( \'Parent Form Category\' ),
    \'parent_item_colon\' => __( \'Parent Form Category:\' ),
    \'edit_item\'         => __( \'Edit Form Category\' ), 
    \'update_item\'       => __( \'Update Form Category\' ),
    \'add_new_item\'      => __( \'Add New Form Category\' ),
    \'new_item_name\'     => __( \'New Form Category\' ),
    \'menu_name\'         => __( \'Form Categories\' ),
  );
  $args = array(
    \'labels\' => $labels,
    \'hierarchical\' => true,
  );
  register_taxonomy( \'form_category\', \'forms\', $args );
}
add_action( \'init\', \'my_taxonomies_product\', 0 );
这里是查询。

$the_query = new WP_Query($args = array(
      \'post_type\' => \'forms\',
      \'custom_cat\' => \'form_category\',

      ) );
          // The Loop
    if ( $the_query->have_posts() ) 
    {

            while ( $the_query->have_posts() ) {
              $the_query->the_post();
              echo "<div>";
              echo  get_the_title();
              echo "</div>";
              ;?>

              <?php 
            }
          } 

        else {
          wp_reset_postdata();
        }

2 个回复
SO网友:shanebp

尝试以下操作:

$the_query = new WP_Query($args = array(
      \'post_type\' => \'forms\',
      \'tax_query\' => array(
        array(
            \'taxonomy\' => \'form_category\',
            \'field\'    => \'slug\'
        ),
      ),

    )
);
顺便说一句,为什么在这个add\\u操作中有一个0?删除它或将其更改为>0。

add_action( \'init\', \'my_taxonomies_product\', 0 );

SO网友:user5288

将我需要使用税务查询的目标转到类别。。。呃。

<?php
$myposts = get_posts(array(
    \'showposts\' => -1,
    \'post_type\' => \'forms\',
    \'tax_query\' => array(
        array(
        \'taxonomy\' => \'product_category\',
        \'field\' => \'slug\',
        \'terms\' => array(\'appeals\'))
    ))
);

foreach ($myposts as $mypost) {
      echo $mypost->post_title . \'<br/>\';

}
?>

结束

相关推荐