2015-03-25 55 views
2

我在這裏有一個問題。WordPress的 - 自定義字段的分類單導航訂單

我喜歡展示在single.php中 2個導航按鈕,設在一個分類(含稅名:temporada

現在我google一下。我發現了一個簡單的代碼來顯示這個按鈕,但,這是爲了通過日期,我想訂購,但自定義字段(自定義字段:numeroepisodio

這是我的代碼:

的functions.php

add_filter('get_next_post_join', 'navigate_in_same_taxonomy_join', 20); 
add_filter('get_previous_post_join', 'navigate_in_same_taxonomy_join', 20); 

function navigate_in_same_taxonomy_join() { 
    global $wpdb; 
return " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr. term_taxonomy_id = tt.term_taxonomy_id"; 
} 

add_filter('get_next_post_where' , 'navigate_in_same_taxonomy_where'); 
add_filter('get_previous_post_where' , 'navigate_in_same_taxonomy_where'); 

function navigate_in_same_taxonomy_where($original) { 
    global $wpdb, $post; 
    $where = ''; 
    $taxonomy = 'temporada'; 
    $op = ('get_previous_post_where' == current_filter()) ? '<' : '>'; 
    $where = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy); 
     if (! is_object_in_taxonomy($post->post_type, $taxonomy)) 
    return $original ; 

    $term_array = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids')); 
    $term_array = array_map('intval', $term_array); 

     if (! $term_array || is_wp_error($term_array)) 
    return $original ; 
    $where = " AND tt.term_id IN (" . implode(',', $term_array) . ")"; 
return $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post-> post_date, $post->post_type); 
} 

的single.php

<?php if (get_previous_post() != null) : ?> 
<?php 
$singular_nav_previous_text = apply_filters('tc_singular_nav_previous_text', _x('&larr;' , 'Previous post link' , ' customizr')); 
    previous_post_link('%link' , '<div id="temporadas-dropdown"><i class="fa fa-caret-left"></i> ANTERIOR</div>'); 
?> 
<?php endif; ?> 

<?php if (get_next_post() != null) : ?> 
<?php 
$singular_nav_next_text = apply_filters('tc_singular_nav_next_text', _x('&rarr;' , 'Next post link' , 'customizr' )); 
    next_post_link('%link' , '<div id="temporadas-dropdown">SIGUIENTE <i class="fa fa-caret-right"></i></div>'); 
?> 
<?php endif; ?> 

如何顯示基於taxonomy = 'temporada'和秩序由meta_value = 'numeroepisodio'的按鈕?

感謝您的時間。

+0

讓我知道如果你需要更多的幫助.. – LumberHack 2015-03-28 22:22:16

回答

1

爲什麼不使用WP_Query對象爲這個像下面

$posts = get_posts(array(
    'post_type'   => 'post', 
    'posts_per_page' => -1, 
    'orderby'   => 'numeroepisodio', 
    'order'    => 'DESC', 
    'tax_query' => array(
        array(
          'taxonomy' => 'temporada' 
          ) 
)); 

if($posts): ?> 

// Do something here 

<? endif; 

您可能需要稍微修改,並檢查語法..

+0

嗨,@ JudeRosario並感謝您的回覆。如何使用它檢查下一個和上一個鏈接? – Ferrrmolina 2015-03-28 22:40:52

+0

上面的代碼調用循環,所以'get_previous_post(true)'和'get_next_post(true)'應該可以工作 – LumberHack 2015-03-28 22:48:55

+0

感謝您的幫助。 – Ferrrmolina 2015-03-28 22:54:07

相關問題