2012-01-20 18 views
1

我正在使用wp_get_archives檢索發佈在我的自定義帖子類型「下載」中的帖子的每月日期列表。wp_get_archives自定義帖子類型不工作?

但它不工作,我做錯了什麼?

我使用workpress 3.3

感謝

CODE

<?php $args  = array(

    'post_type'  => 'download', 
    'type'   => 'monthly', 
    'show_count' => '1' 

); ?> 

<?php wp_get_archives($args); ?> 


這是怎麼我註冊了我的自定義信息類型僅供參考

// Downloads Post Type 
add_action('init', 'create_post_type'); 
function create_post_type() { 
    $args = array(
     'labels' => post_type_labels('Download'), 
     'public' => true, 
     'publicly_queryable' => true, 
     'show_ui' => true, 
     'show_in_menu' => true, 
     'query_var' => true, 
     'rewrite' => true, 
     'has_archive' => true, 
     'capability_type' => 'post', 
     'hierarchical' => false, 
     'menu_position' => null, 
     'taxonomies' => array('group'), 
     'supports' => array('title', 
      'editor', 
      'author', 
      'thumbnail', 
      'excerpt', 
      'comments' 
     ) 
    ); 

    register_post_type('download', $args); 

} 

回答

1

wp_get_archives();不支持'post_type'作爲參數。

有關於黑客攻擊的討論[這裏] [1]:http://wordpress.org/support/topic/archive-list-and-page-for-custom-post-types-mysql?replies=9。這篇文章有幾個鏈接指向另一個解決方案,它使用了一個應用於sql查詢的過濾器,用於選擇要包含在歸檔結果中的帖子。看看如何在general-template.php中應用過濾器,它也應該起作用。

+1

嗯,你有沒有試過這個嗎?我已經嘗試了很多querys和本文中的查詢,但沒有太多的運氣。 – Joshc

0

一些提示:

提示1個使用自定義類型後,檔案插件(任何更改後更新固定鏈接)

提示2 WPML插件+定製後型檔案:

function CPT1_join($join) { 
    global $wpdb; 
    $join = " JOIN pav_icl_translations t ON t.element_id = pav_posts.ID AND t.element_type='CUSTOM_POST_TYPE1' "; 
    return $join ; 
} 

function CPT2_join($join) { 
    global $wpdb; 
    return $join = " JOIN pav_icl_translations t ON t.element_id = pav_posts.ID AND t.element_type='CUSTOM_POST_TYPE2' "; 
} 

function wp_get_archives_filter($where, $options) { 
    global $wpdb; // get the DB engine 

    if(!isset($options['post_type'])) return $where; // OK - this is regular wp_get_archives call - don't do anything 

    $post_type = $wpdb->escape($options['post_type']); // escape the passed value to be SQL safe 

    if($post_type == 'all') 
      $post_type = ''; // if we want to have archives for all post types 
    else $post_type = "post_type = '$post_type' AND"; // otherwise just for specific one 

    if($options['post_type'] == 'CUSTOM_POST_TYPE1'){ 
     add_filter('getarchives_join', 'CPT1_join');  
     }elseif($options['post_type'] == 'CUSTOM_POST_TYPE2'){ 
     add_filter('getarchives_join', 'CPT2_join'); 
     } 

    $where = str_replace('post_type = \'post\' AND', $post_type, $where); 
    return $where; 
} 
add_filter('getarchives_where', 'wp_get_archives_filter', 10, 2); 
相關問題