WordPress自定义文章类型分类

5
我正在为一家旅行社建设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

你在 functions.php 中为自定义文章类型启用了分类吗?

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

另外,您尝试过在调用get_categories()函数后对categories变量进行var_dump()吗?

这是我与此相关的functions.php文件: - mtuttle
实际上,我似乎无法让我的代码显示在评论中。有什么建议吗? - mtuttle
1
对于注释,你可以在代码 <?php echo("") ?> 周围使用反引号,但是直接在原始帖子中发布整个文件可能更容易。 - Dan
嗨,丹,谢谢你的建议。我已经将该函数添加到顶部帖子中了。 - mtuttle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接