2015-04-27 50 views
0

我需要在single.php中包含一個導航按鈕,其中包含名爲'works'的自定義帖子類型的前一個和後一個帖子。一個自定義帖子類型的WP導航文章

我包括此

<?php echo get_next_posts_link('Go to next post'); ?> 
<?php echo get_previous_posts_link('Go to prev post');?> 

或本

<?php previous_post_link($taxonomy = 'works'); ?> 

但鴕鳥政策什麼都不顯示或導航包括所有的文章和網頁。例如,只需要將該CUT的帖子像轉盤畫廊一樣分頁。

回答

0

如果使用相同的類別不起作用,那麼你應該試試這個。將標記「works」添加到自定義後期類型的帖子中。然後你就可以得到previous_和next_post_link這個標籤:

<?php previous_post_link('%link', 'Previous in works', TRUE, ' ', 'post_tag'); ?> 
<?php next_post_link('%link', 'Next in works', TRUE, ' ', 'post_tag'); ?> 
+0

不工作,不顯示任何 –

+0

對不起,沒有工作。我查了一下如何在一個較老的項目中完成這個解決方案,併爲我工作。作爲解決方法,您可以使用適用於自定義帖子類型的後貼標籤。看到我更新的答案。 –

1

試試這個

<?php 
    $term_list = wp_get_post_terms($post->ID, 'TAXONOMY', array("fields" => "slugs")); 
    if (empty($term_list[1])) { 
    print_r($term_list[0]); 
    $termo = $term_list[0]; 
    } else { 
    print_r($term_list[1]); 
    $termo = $term_list[1]; 
    } 

    // get_posts in same custom taxonomy 
    $postlist_args = array(
    'posts_per_page' => -1, 
    'orderby'   => 'menu_order title', 
    'order'   => 'ASC', 
     'post_type'  => 'CUSTOM-POST-TYPE', 
    'taxonomy'=>'TAXONOMY', 
    'term'=>$termo, 
    ); 
    $postlist = get_posts($postlist_args); 

    // get ids of posts retrieved from get_posts 
    $ids = array(); 
    foreach ($postlist as $thepost) { 
    $ids[] = $thepost->ID; 
    } 

    // get and echo previous and next post in the same taxonomy   
    $thisindex = array_search($post->ID, $ids); 
    $previd = $ids[$thisindex-1]; 
    $nextid = $ids[$thisindex+1]; 
    if (!empty($previd)) { 
    echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>'; 
    } 
    if (!empty($nextid)) { 
    echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>'; 
    } 
    ?> 

好運。

問候

+0

嗨,第一個數組($ term_list)失敗,顯示消息「致命錯誤:不能使用WP_Error類型的對象作爲數組...」。但是如果我在$ postlist_args中刪除這個數組和「'term'=> $ termo」,一切正常。非常感謝你的幫助! –

相關問題