2013-05-17 221 views
6

我一直在嘗試幾天來創建一個自定義帖子類型與類別。到目前爲止,我有這個工作,我可以很容易地添加內容,並將其分配給一個類別。我的代碼如下。自定義帖子類型和類別

我不明白,似乎無法正常工作是創建一個存檔頁面來顯示類別的帖子。
例如:我的帖子類型被稱爲廣告。我的類別被稱爲攝影師

頁面是否可以動態調出您正在使用的類別並顯示屬於該類別的所有自定義帖子?

function my_custom_post_advert() { 
    $labels = array(
     'name'    => _x('Adverts', 'post type general name'), 
     'singular_name'  => _x('Advert', 'post type singular name'), 
     'add_new'   => _x('Add New', 'advert'), 
     'add_new_item'  => __('Add New Advert'), 
     'edit_item'   => __('Edit Advert'), 
     'new_item'   => __('New Advert'), 
     'all_items'   => __('All Adverts'), 
     'view_item'   => __('View Advert'), 
     'search_items'  => __('Search Adverts'), 
     'not_found'   => __('No adverts found'), 
     'not_found_in_trash' => __('No adverts found in the Trash'), 
     'parent_item_colon' => '', 
     'menu_name'   => 'Adverts' 
    ); 
    $args = array(
     'labels'  => $labels, 
     'description' => 'Holds our adverts and advert specific data', 
     'public'  => true, 
     'menu_position' => 5, 
     'supports'  => array('title', 'editor', 'thumbnail', 'excerpt', 'category'), 
     'has_archive' => true, 
    ); 
    register_post_type('advert', $args); 
} 
add_action('init', 'my_custom_post_advert'); 

function my_taxonomies_advert() { 
    $labels = array(
     'name'    => _x('Advert Categories', 'taxonomy general name'), 
     'singular_name'  => _x('Advert Category', 'taxonomy singular name'), 
     'search_items'  => __('Search Advert Categories'), 
     'all_items'   => __('All Advert Categories'), 
     'parent_item'  => __('Parent Advert Category'), 
     'parent_item_colon' => __('Parent Advert Category:'), 
     'edit_item'   => __('Edit Advert Category'), 
     'update_item'  => __('Update Advert Category'), 
     'add_new_item'  => __('Add New Advert Category'), 
     'new_item_name'  => __('New Advert Category'), 
     'menu_name'   => __('Advert Categories'), 
    ); 
    $args = array(
     'labels' => $labels, 
     'hierarchical' => true, 
    ); 
    register_taxonomy('advert_category', 'advert', $args); 
} 
add_action('init', 'my_taxonomies_advert', 0); 

回答

0

要保存自己很多的麻煩,什麼我已經在過去使用的是this custom post type plugin - 它就像一個魅力:

類型可以通過添加內容類型定製WordPress管理的,自定義字段和分類。您將能夠製作WordPress管理員並將其轉化爲您自己的內容管理系統。

而且與我使用這個Custom post type archive plugin

這個插件將啓用自定義後類型的檔案(也每年,每月和每日)用飼料,可定製的標題和頁面在一起。

1

您應該能夠導航到/廣告。此外,has_archive應爲您創建一個檔案頁面。

+0

爲什麼不是這樣的正確答案?我錯過了什麼嗎?我不知道這個頁面上的答案有一半是在說什麼! –

0

該解決方案基本上包含在StackOverflow其他地方的this question的答案中。

總之,建立一個自定義查詢,但$ args數組中替換:

'cat_name' => 'Photographers'

與分類查詢,像這樣:

'tax_query' => array( array( 'taxonomy' => 'advert_category', 'field' => 'slug', 'terms' => 'photographers' ) )

當然,您也應該在$ args中包含'post-type' => 'advert'。希望這可以幫助!

1

你基本上需要創建一個頁面模板包含定製wp_query這樣的WordPress可以判斷你是哪個類別上。

一旦你創建了你的頁面模板,那麼你可以在你的WordPress管理員中創建一個頁面...選擇新的頁面模板作爲你的模板。

如果您希望該類別是動態的,您可以始終設置您的頁面模板以接受$ _GET參數以確定顯示廣告的類別。像這樣:

http://example.com/adverts-listing/?mycat=photographers 

http://example.com/adverts-listing/?mycat=programmers 

這裏是什麼樣的頁面模板可能看起來像一個例子(當然這會在你使用的主題變化...這個例子是建立使用二十四個主題):

<?php 
/** 
* Template Name: Advert Listing 
* 
*/ 

get_header(); ?> 

    <section id="primary" class="content-area"> 
    <div id="content" class="site-content" role="main"> 

    <?php 
     // Set the args array for the query to happen 
     $args = array(
     'post_type' => 'adverts', 
     'post_status' => 'publish', 
     'posts_per_page' => 10 
    ); 

     // Dynamically set the mycat argument from a $_GET parameter 
     if(isset($_GET['mycat'])) { 
     $args['tax_query'] => array(
      array(
      'taxonomy' => 'advert_category', 
      'field' => 'slug', 
      'terms' => $_GET['mycat'] 
     ) 
     ); 
     } 

     // Issue the query 
     $q = null; 
     $q = new WP_Query($args); 

     // Start the loop 
     if($q->have_posts()) : ?> 
     <header class="page-header"> 
      <h1 class="page-title"><?php _e('Advert Listing:', 'twentyfourteen'); ?></h1> 
     </header> 

     <?php 

     while ($q->have_posts()) : $q->the_post(); 
      ?> 

      <article id="post-<?php the_ID(); ?>" class="post-<?php the_ID(); ?> adverts type-adverts status-publish hentry"> 

      <header class="entry-header"> 
       <a href="<?php echo get_permalink(get_the_ID()); ?>"><h3 class="entry-title"><?php the_title(); ?></h3></a> 
      </header><!-- .entry-header --> 

      <div class="entry-content"> 
       <?php the_excerpt(); ?> 
      </div> 

      </article> 

      <?php 

     endwhile; 

     // Previous/next post navigation. 
     twentyfourteen_paging_nav(); 

     else : 
     // If no content, include the "No posts found" template. 
     get_template_part('content', 'none'); 

     endif; 

     wp_reset_query(); // Restore global post data stomped by the_post(). 
     ?> 

    </div><!-- #content --> 
    </section><!-- #primary --> 

<?php 

get_sidebar('content'); 
get_sidebar(); 
get_footer(); 
0

所以我也需要自定義類別的職位類型。

下面的代碼非常簡單和乾淨。字面上複製並粘貼。然後根據您的需求進行調整。希望這有助於未來的人們。

它基本上鏈接正常的wordpress類別與您的自定義帖子類型。當他們從wordpress管理員部分工作時,使其客戶真的很容易。它也具有通過標籤的個人分類。所以您可以選擇通過所有帖子類型或發佈特定分類標準

該代碼是不言自明的。請投我的答案我需要建立我的代表。謝謝。

您必須將代碼複製到你的的functions.php文件

add_action('init', 'create_post_types'); 
    function create_post_types() { 
// Custom Post 1 
     register_post_type('companies', 
      array(
       'labels' => array(
        'name' => __('Companies'), 
        'singular_name' => __('Company') 
       ), 
      'public' => true, 
      'has_archive' => true, 
      ) 
     ); 
     // Default Wordpress Category Taxonomy 
     register_taxonomy_for_object_type('category', 'companies'); 
     // Post Specific Taxonomy 
     register_taxonomy('company_category', 'companies'); 
// Custom Post 2 
     register_post_type('events', 
      array(
       'labels' => array(
        'name' => __('Events'), 
        'singular_name' => __('Event') 
       ), 
      'public' => true, 
      'has_archive' => true, 
      ) 
     ); 
     // Default Wordpress Category Taxonomy 
     register_taxonomy_for_object_type('category', 'events'); 
     // Post Specific Taxonomy 
     register_taxonomy('events_category', 'events'); 
// Custom Post 3 
     register_post_type('deals', 
      array(
       'labels' => array(
        'name' => __('Deals'), 
        'singular_name' => __('Deal') 
       ), 
      'public' => true, 
      'has_archive' => true, 
      ) 
     ); 
     // Default Wordpress Category Taxonomy 
     register_taxonomy_for_object_type('category', 'deals'); 
     // Post Specific Taxonomy 
     register_taxonomy('deals_category', 'deals'); 
// Custom Post 4 
     register_post_type('banners', 
      array(
       'labels' => array(
        'name' => __('Banners'), 
        'singular_name' => __('Banner') 
       ), 
      'public' => true, 
      'has_archive' => true, 
      ) 
     ); 
     // Default Wordpress Category Taxonomy 
     register_taxonomy_for_object_type('category', 'banners'); 
     // Post Specific Taxonomy 
     register_taxonomy('banners_category', 'banners'); 
    } 

有4種自定義文章類型所以就像我說的代碼是不言自明

相關問題