如何引入自定义帖子类型类别?

时间:2014-05-14 23:01:51

标签: wordpress categories custom-post-type taxonomy

最难以使用我的自定义帖子类型和重新审核类别...

到目前为止,我有两种已注册并正在运作的自定义帖子类型:RV租借和RV销售。

我只需要一个模板页面,其中包含RV销售的所有类别,但我无法使其工作......代码似乎只是拉入RV租赁的类别,即使它应该只显示RV租赁和房车销售的所有CPT类别。疯狂的试图找出如何让它只能拉动房车销售...有人可以帮忙吗?我意识到这是我发布的很多代码,但我真的不知道这里有什么问题......失去理智。

这是我登记RV销售自定义帖子类型的函数文件:

/* ------------------------------
CPT RV SALES  
------------------------------*/

add_action('init','create_rvsales_post_type'); 

function create_rvsales_post_type() {
$labels = array(
    'name' => 'RV Sales',
    'singular_name' => 'RV Sales',
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_nav_menus' => false, 
    'show_in_menu' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 20,
    'menu_icon' => null,
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array('title','thumbnail','editor'),
    'has_archive' => 'motorhomes-for-sale',
    'rewrite' => array('slug' => 'motorhomes-for-sale/%rvsales_cats%'),
    'query_var' => true,
    'can_export' => true,
); 
register_post_type('rv_sales',$args);
}

 //setup tax
 add_action( 'init', 'create_rvsales_taxonomies', 0 ); 


 function create_rvsales_taxonomies() 
 {
 $labels = array(
 'name' => _x( 'RV Sales Categories', 'taxonomy general name' ),
 'singular_name' => _x( 'RV Sales Category', 'taxonomy singular name' ),
  );

   register_taxonomy('rvsales_cats',array('rv_sales'), array(
  'hierarchical' => true,
  'labels' => $labels,
  'show_ui' => true,
  'query_var' => true,
  'rewrite' => array( 'slug' => 'motorhomes-for-sale' ),
    ));

 }

 register_taxonomy_for_object_type('category', 'rv_sales');

 //get that nice url structure
 function filter_post_type_link2($link, $post) {
 if (!in_array($post -> post_type, array(
         'rv_sales',
     )))
     return $link;


 if ($catsegors = get_the_terms($post -> ID, 'rvsales_cats')) {
    $link = str_replace('%rvsales_cats%', array_pop($catsegors) -> slug, $link); 
}

 return $link;
}

 add_filter('post_type_link2', 'filter_post_type_link2', 10, 2); 

这是我的模板,应该在所有CPT类别(来自RV销售和房车租赁)中调用,但它仍然只是从RV租赁中提取类别,即使存在虚假参数在&#; rv_salesblah' for get_categories:

<?php 
                // get all the categories from the database
                    $cats = get_categories('rv_salesblah'); 
                    // loop through the categries
                    foreach ($cats as $cat) {

                    // setup the cateogory ID
                    $cat_id= $cat->term_id;

                    // Make a header for the category
                    echo "<h2>".$cat->name."</h2>";

                    // create a custom wordpress query
                    query_posts("cat=$cat_id&posts_per_page=100");

                    // start the wordpress loop!
                    if (have_posts()) : while (have_posts()) :     the_post();     ?>

                    <?php // create our link now that the post is setup ?>
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                    <?php echo '<hr/>'; ?>

                <?php endwhile; endif; // done our wordpress loop. Will start again for    each         category ?>
                <?php } // done the foreach statement ?>

编辑:修正了这个问题,如果有一些对不起的灵魂遇到同样的问题,这就是我用来让我走上正轨的代码:

 <?php
 //for a given post type, return all
  $post_type = 'rv_sales';
$tax = 'rvsales_cats';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms  as $tax_term) {
 $args=array(
  'post_type' => $post_type,
  "$tax" => $tax_term->slug,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo 'List of  '. $tax_term->name;
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php     the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
   endwhile;
}
 wp_reset_query();
}
}
?>

1 个答案:

答案 0 :(得分:1)

编辑:修正了这个问题,如果有一些对不起的灵魂遇到同样的问题,这就是我用来让我走上正轨的代码:

 ?php
 //for a given post type, return all
 $post_type = 'rv_sales';
$tax = 'rvsales_cats';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms  as $tax_term) {
 $args=array(
 'post_type' => $post_type,
  "$tax" => $tax_term->slug,
   'post_status' => 'publish',
  'posts_per_page' => -1,
   'caller_get_posts'=> 1
);

 $my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
 echo 'List of  '. $tax_term->name;
 while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php          the_title_attribute(); ?>"><?php the_title(); ?></a></p>
   <?php
  endwhile;
}
wp_reset_query();
}
}
?>