【问题标题】:Using custom taxonomy as page navigation使用自定义分类作为页面导航
【发布时间】:2016-11-11 00:26:52
【问题描述】:

** 更新(有一点障碍)...

我通过呼应分类术语使代码正常工作。有一个小障碍。您不能选择超过一个术语。否则它不知道该怎么做。这是代码。

为回声做好准备:

<?php $my_terms = get_the_terms( $post->ID, 'YOUR_TERM_HERE' );
if( $my_terms && !is_wp_error( $my_terms ) ) {
    foreach( $my_terms as $term ) {}
} ;?>

设置您的页面导航:

<?php // get_posts in same custom taxonomy
                  $postlist_args = array(
                     'posts_per_page'  => -1,
                     'orderby'         => '',
                     'order'           => 'ASC',
                     'post_type'       => 'YOUR_POST_TYPE_HERE',
                     'YOUR_TERM_HERE'         => array( $term->slug )  // here is your echo

                  ); 
                  $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($nextid) ) {
                     echo '<a rel="next" href="' . get_permalink($nextid). '"><i class="fa fa-chevron-circle-left" aria-hidden="true"></i></a>';
                  }
                  if ( !empty($previd) ) {
                     echo '<a rel="prev" href="' . get_permalink($previd). '"><i class="fa fa-chevron-circle-right" aria-hidden="true"></i></a>';
                  } ;?>

如果有人知道如何使用多个分类术语来做到这一点。我真的很想知道。

嗯,这可能很难描述,但我会尽力而为。

我有一个自定义帖子类型about us,并在该 CPT 中创建了一个分层自定义分类法our team。我在该自定义分类中添加了几个项目。

  • 团队成员 1
  • 团队成员 2

我想要做的是,当您单击团队成员 1 时,将有一个字段 (div) 显示团队成员 2 的名称并且是可点击的,因此您可以转到下一个团队成员,或返回.. .等等。

这种方式会做一些事情,但它不会通过 post_type 的自定义分类:

 <?php // get_posts in same custom taxonomy
$postlist_args = array(
   'posts_per_page'  => -1,
   'orderby'         => 'menu_order title',
   'order'           => 'ASC',
   'post_type'       => 'over-ons',
   'taxonomy' => 'ons_team'
); 
$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>';
} ;?>

原代码说:

// get_posts in same custom taxonomy
$postlist_args = array(
   'posts_per_page'  => -1,
   'orderby'         => 'menu_order title',
   'order'           => 'ASC',
   'post_type'       => 'your_custom_post_type',
   'your_custom_taxonomy' => 'your_custom_taxonomy_term'
); 
$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>';
}

我的your_custom_taxonomy_term 是什么?因为我认为我的分类法没有一个?

正常导航不起作用:

<?php posts_nav_link('separator','prelabel','nextlabel'); ?>

有没有人做过类似的东西或者知道从哪里开始......

【问题讨论】:

    标签: wordpress navigation custom-taxonomy


    【解决方案1】:

    使用这个

    <?php previous_post_link('<div class="prev">%link</div>'); ?> <?php next_post_link('<div class="next">%link</div>'); ?>

    %link 将显示下一个团队成员的姓名

    【讨论】:

    • 谢谢。但这不适用于自定义分类法。它只是转到下一篇文章,不提交自定义分类。
    【解决方案2】:

    希望了解的朋友可以查看我在 OP 中的更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多