2013-01-23 11 views
6

मेरे पास मेरी वर्डप्रेस साइट में एक कस्टम पोस्ट प्रकार "समाचार" है। मैं प्रत्येक पोस्ट में मेटा डेटा जोड़ने के लिए उन्नत कस्टम फ़ील्ड प्लगइन का उपयोग कर रहा हूं।कस्टम पोस्ट प्रकार वार्षिक/मासिक संग्रह

मैं एक संग्रह के रूप समाचार आइटम की एक सरणी बनाना चाहते हैं:

[2013] 
    [January] => 5 
[2012] 
    [January] => 20 
    [February] => 10 
[2011] 
    [April] => 30 

मैं इस काम कर रहा का उपयोग कर पाने में कामयाब रहे:

global $wpdb; 
    $news = $wpdb->get_results(
     "SELECT wp_posts.post_date, COUNT(wp_posts.ID) as count 
     FROM $wpdb->posts 
     WHERE 
     wp_posts.post_type = 'news' AND 
     wp_posts.post_status = 'publish' AND 
     wp_posts.post_date <= CURDATE() AND 
     wp_posts.post_date >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR) 
     GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date) 
     ORDER BY wp_posts.post_date DESC", 
     ARRAY_A); 

    $archive = array(); 
    foreach ($news as $post): 
     $year = date('Y', strtotime($post['post_date']));  
     $month = date('m', strtotime($post['post_date']));  
     $month_name = date('F', strtotime($post['post_date'])); 
     $post['url'] = 'NOT SURE ABOUT URL'; 
     $archive[$year][$month_name] = $post; 
    endforeach; 

मैं विशिष्ट साल से जोड़ने के लिए सक्षम होना चाहिए और http://example.com/2012/ और http://example.com/2012/10/ का उपयोग कर महीनों।

चूंकि इसमें एक कस्टम पोस्ट प्रकार "समाचार" शामिल है, मुझे यकीन है कि यह कैसे किया जाए?

+0

मैं भी उल्लेख करना चाहिए कि जब मैं कोशिश करते हैं और करने के लिए ब्राउज़ करें: 'http://example.com/index.php? post_type = news & year = 2012' मुझे 'http: // example.com/news' – dclawson

+1

पर रीडायरेक्ट किया गया है, आप यहां भी प्रयास करना चाहेंगे: http://wordpress.stackexchange.com/ – tillinberlin

उत्तर

26

आपको जो चाहिए वह करने के लिए, आपको कस्टम पोस्ट प्रकार के लिए वर्ष/माह/आदि पोस्ट डेटा कैप्चर करने में सक्षम होने के लिए वर्डप्रेस रीराइट्स को संशोधित करने की आवश्यकता है।

आप नीचे दिए गए कोड के साथ ऐसा हो सकता है:

/** 
* Custom post type date archives 
*/ 

/** 
* Custom post type specific rewrite rules 
* @return wp_rewrite    Rewrite rules handled by Wordpress 
*/ 
function cpt_rewrite_rules($wp_rewrite) { 
    $rules = cpt_generate_date_archives('news', $wp_rewrite); 
    $wp_rewrite->rules = $rules + $wp_rewrite->rules; 
    return $wp_rewrite; 
} 
add_action('generate_rewrite_rules', 'cpt_rewrite_rules'); 

/** 
* Generate date archive rewrite rules for a given custom post type 
* @param string $cpt  slug of the custom post type 
* @return rules    returns a set of rewrite rules for Wordpress to handle 
*/ 
function cpt_generate_date_archives($cpt, $wp_rewrite) { 
    $rules = array(); 

    $post_type = get_post_type_object($cpt); 
    $slug_archive = $post_type->has_archive; 
    if ($slug_archive === false) return $rules; 
    if ($slug_archive === true) { 
     $slug_archive = $post_type->name; 
    } 

    $dates = array(
     array(
      'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})", 
      'vars' => array('year', 'monthnum', 'day')), 
     array(
      'rule' => "([0-9]{4})/([0-9]{1,2})", 
      'vars' => array('year', 'monthnum')), 
     array(
      'rule' => "([0-9]{4})", 
      'vars' => array('year')) 
     ); 

    foreach ($dates as $data) { 
     $query = 'index.php?post_type='.$cpt; 
     $rule = $slug_archive.'/'.$data['rule']; 

     $i = 1; 
     foreach ($data['vars'] as $var) { 
      $query.= '&'.$var.'='.$wp_rewrite->preg_index($i); 
      $i++; 
     } 

     $rules[$rule."/?$"] = $query; 
     $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i); 
     $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i); 
     $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i); 
    } 
    return $rules; 
} 

आप देखेंगे कि मैं कोड की $rules = cpt_generate_date_archives('news', $wp_rewrite); भाग में हार्ड-कोडेड है news। आप इसे आवश्यकतानुसार बदल सकते हैं।

इस कोड के साथ, आप http://yoursite.com/news/2013/02/ पर नेविगेट करने और उस विशिष्ट समय के लिए उस विशिष्ट पोस्ट प्रकार के लिए संग्रह सूची प्राप्त करने में सक्षम होना चाहिए।

पूर्णता के लिए, मैं इसका उपयोग करने के लिए मासिक संग्रह विजेट कैसे उत्पन्न करना शामिल करूंगा।

/** 
* Get a montlhy archive list for a custom post type 
* @param string $cpt Slug of the custom post type 
* @param boolean $echo Whether to echo the output 
* @return array   Return the output as an array to be parsed on the template level 
*/ 
function get_cpt_archives($cpt, $echo = false) 
{ 
    global $wpdb; 
    $sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' GROUP BY YEAR($wpdb->posts.post_date), MONTH($wpdb->posts.post_date) ORDER BY $wpdb->posts.post_date DESC", $cpt); 
    $results = $wpdb->get_results($sql); 

    if ($results) 
    { 
     $archive = array(); 
     foreach ($results as $r) 
     { 
      $year = date('Y', strtotime($r->post_date)); 
      $month = date('F', strtotime($r->post_date)); 
      $month_num = date('m', strtotime($r->post_date)); 
      $link = get_bloginfo('siteurl') . '/' . $cpt . '/' . $year . '/' . $month_num; 
      $this_archive = array('month' => $month, 'year' => $year, 'link' => $link); 
      array_push($archive, $this_archive); 
     } 

     if(!$echo) 
      return $archive; 
     foreach($archive as $a) 
     { 
      echo '<li><a href="' . $a['link'] . '">' . $a['month'] . ' ' . $a['year'] . '</a></li>'; 
     } 
    } 
    return false; 
} 

इस सुविधा का उपयोग करने के लिए बस कस्टम पोस्ट प्रकार के स्लग, यानी की आपूर्ति: get_cpt_archives('news')

Array 
(
    [0] => Array 
     (
      [month] => February 
      [year] => 2013 
      [link] => http://yoursite.com/news/2013/02 
     ) 

    [1] => Array 
     (
      [month] => January 
      [year] => 2013 
      [link] => http://yoursite.com/news/2013/01 
     ) 

) 

आप एक foreach और आउटपुट लेकिन उन्हें आप चाहते हैं के साथ इन के माध्यम से लूप कर सकते हैं: इस यानी अद्वितीय वर्ष/दिनांक/लिंक की एक सरणी, वापस आ जाएगी।

वैकल्पिक रूप से, आप get_cpt_archives('news', true) का उपयोग कर सकते हैं जो <li> में अपने विशिष्ट संग्रह से लिंक किए गए प्रत्येक आइटम को स्वचालित रूप से प्रतिबिंबित करेगा।

उत्पादन के स्वरूपण बिल्कुल जैसे आप चाहते हैं नहीं है, तो आप इसे यह

Year 
    Month 
    Month 
Year 
    Month 

प्रारूप जिनके लिए आपको प्रदर्शित करने के लिए प्राप्त करने के लिए एक सा tweak करना होगा।

मुझे उम्मीद है कि इससे मदद मिलती है।

+0

महान जवाब, धन्यवाद। – dclawson

+1

यह पहली बार काम नहीं किया। आपको शायद 'सेटिंग्स-> परमालिंक' पर जाना होगा, कस्टम का चयन करें और सहेजें।फिर इसे पिछले एक पर वापस सेट करें और फिर से सहेजें। कुछ लोगों ने कहा है कि यह बचत के साथ पर्याप्त है लेकिन मेरे लिए नहीं। –

+0

इन कार्यों के लिए बहुत बहुत धन्यवाद। यह पूरी तरह से काम किया! इस मुद्दे का सबसे अच्छा जवाब मुझे ऑनलाइन मिला। –

3

मैं जानता हूँ कि यह एक पुरानी पोस्ट है, लेकिन वहाँ करने का एक और अधिक संक्षिप्त तरीका क्या ओपी पूछ रहा है है:

function cpt_add_rewrite_rules() 
{ 
    $cpt = 'news'; 

    add_rewrite_rule($cpt.'/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})?$', 
     'index.php?post_type='.$cpt.'&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]', 
     'top'); 

    add_rewrite_rule($cpt.'/([0-9]{4})/([0-9]{1,2})?$', 
     'index.php?post_type='.$cpt.'&year=$matches[1]&monthnum=$matches[2]', 
     'top'); 

    add_rewrite_rule($cpt.'/([0-9]{4})?$', 
     'index.php?post_type='.$cpt.'&year=$matches[1]', 
     'top'); 
} 

add_filter('init', 'cpt_add_rewrite_rules'); 
संबंधित मुद्दे