Wordpress自定义帖子类型类别

时间:2012-09-27 17:21:26

标签: wordpress categories custom-post-type

我正在为旅行社建立一个wordpress网站,该旅行社向各个地点提供特价优惠,并在一个页面上显示。因此,我使用以下代码来过滤我设置的自定义帖子类型,并显示单独的帖子摘录和图像。

这就是我所拥有的:

<code>
    <?php
/**
 * Template Name: Deals 1 column
 */

get_header(); ?>

<div id="content" class="two_third <?php echo of_get_option('blog_sidebar_pos') ?>">

<div id="sort">
        <h5>SORT BY : </h5>
        <ul>
            <li><a href="<?php echo add_query_arg(array ('paged' => '1',  'orderby' => 'date', 'order' => 'DESC'));?>">Date</a></li>
            <li><a href="<?php echo add_query_arg(array ('paged' => '1',  'orderby' => 'title', 'order' => 'ASC'));?>">Deal (A to Z)</a></li>
            <li><a href="<?php echo add_query_arg(array ('paged' => '1',  'orderby' => 'meta_value', 'order' => 'ASC', 'meta_key' => 'price'));?>">price (A to Z)</a></li>
        </ul>
</div>
<div id="filter">
        <h5>FILTER BY : </h5>
        <ul>
        <?php
        $categories=  get_categories('taxonomy=types&title_li=');
        foreach ($categories as $category){ ?>
        <li><a href="<?php echo add_query_arg(array ('paged' => '1',  'filter' => $category->category_nicename));?>" title="Filter by <?php echo $category->name;?>"><?php echo $category->name;?></a></li>
        <?php }?>
        </ul>
</div>
<div id="reset-filters">
        <a href="<?php echo add_query_arg(array ('paged' => '1',  'filter' => ''));?>">reset filters</a>
</div>



<div id="gallery" class="one_column">

    <ul class="portfolio">
        <?php
        $query = 'post_type=gs_deals&types='.$_GET['filter'].'&orderby='.$_GET['orderby'].'&order='.$_GET['order'].'&meta_key='.$_GET['meta_key'].'&posts_per_page=3&paged='.$paged;
        query_posts($query);
        if (have_posts()) : while (have_posts()) : the_post();
        $custom = get_post_custom(get_the_ID()); 
        ?>
        <?php
        $categories=  get_categories('taxonomy=types&title_li=');
        foreach ($categories as $category){ ?>
        <div id="category"><h3><?php echo $category->name;?></h3>
        <?php }?>
        <li class="clearfix">
            <div class="clearfix">
                <span class="image-border"><a class="image-wrap" href="<?php the_permalink() ?>" title="<?php _e('Permanent Link to', 'theme1512');?> <?php the_title_attribute(); ?>" ><?php the_post_thumbnail( 'portfolio-post-thumbnail-xl' ); ?></a></span>
                <div class="folio-desc">
                <h6 class="project">Deal!</h6>
                <p><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php echo get_the_title(); ?>"><?php the_title(); ?></a></p>
                <h6 class="client">Price :</h6>
                <h4><?php echo $custom["price"][0];?></h4>
                <p class="short"><?php echo $custom["short_text"][0];?></p>
                <p><a href="<?php the_permalink(); ?>">View Details</a></p>
            </div>
            </div>
        </li>
        </div>
        <?php endwhile; ?>
        <?php endif;?>

    </ul>
    <div class="posts-nav">
        <div class="prev"><?php next_posts_link(__('? Older Projects')) ?></div>
        <div class="next"><?php previous_posts_link(__('Newer Projects ?')) ?></div>
    </div>
</div><!-- #content -->
</div>
<!-- end #main -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>


</code>

这是我的functions.php处理这个:

//////REGISTER A CUSTOM POST TYPE
add_action('init', 'gs_deals_register');//Always use a shortname like "gs_" not to see any 404 errors

function gs_deals_register(){
    $args = array(
        'label' => __('Deals List'),
        'singular_label' => __('Deals'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'deals'),//Use a slug like "work" or "project" that shouldnt be same with your page name
        'supports' => array('title', 'editor', 'thumbnail')//Boxes will be showed in the panel
       );

    register_post_type( 'gs_deals' , $args );
}

//////ADD CUSTOM INPUTS (Client & Short_text)
add_action("admin_init", "admin_init");
add_action('save_post', 'save_options');

function admin_init(){
    add_meta_box("gs_dealsInfo-meta", "Deals Options", "meta_options", "gs_deals", "side", "low");
}

function meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $price = $custom["price"][0];
    $short_text = $custom["short_text"][0];
    ?>
    <p><label>Price:</label><br /><input name="price" value="<?php echo $price; ?>" /></p>
    <p><label>Short Text:</label><br /><textarea name="short_text"><?php echo $short_text; ?></textarea></p>
<?php
}

function save_options(){
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
    global $post;
    update_post_meta($post->ID, "price", $_POST["price"]);
    update_post_meta($post->ID, "short_text", $_POST["short_text"]);
}

//////ADD TAXONOMY FOR FILTERING (Taxonomy name: types)
register_taxonomy("types", array("gs_deals"), array("hierarchical" => true, "label" => "Types", "singular_label" => "Types", "rewrite" => true));

//////ADD HOOKS FOR PANEL VIEW
add_filter("manage_edit-gs_deals_columns", "gs_deals_edit_columns"); 
add_action("manage_posts_custom_column",  "gs_deals_custom_columns");

function gs_deals_edit_columns($columns){
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Deal Title",
        "short_text" => "Short Text",
        "price" => "Price",
        "types" => "Types",
    );

    return $columns;
}

function gs_deals_custom_columns($column){
    global $post;
    $custom = get_post_custom();
    switch ($column)
    {
        case "short_text":
            echo $custom["short_text"][0];
            break;
        case "price":
            echo $custom["price"][0];
            break;
        case "types":
            echo get_the_term_list($post->ID, 'types', '', ', ','');
            break;
    } 
}

我想在相应的帖子上方显示类别名称,但似乎没有成功。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您是否在functions.php中为自定义帖子类型启用了类别?

 register_taxonomy_for_object_type('category', '[name of custom post type]');

另外,您是否尝试在调用get_categories()函数后执行categories变量的var_dump()?