按标签列出同级页面

时间:2013-02-20 07:24:18

标签: php wordpress tagging

我正在尝试列出父页面的所有兄弟(子)页面。这很容易。 但是我已经标记了每个兄弟的子页面,我想像我这样组织我的列表:

Term 1
  - Child/Sibling Page 1
  - Child/Sibling Page 2
  - Child/Sibling Page 4
Term 2
  - Child/Sibling Page 3
Term 4
  - Child/Sibling Page 5
  - Child/Sibling Page 6
  - Child/Sibling Page 7

我需要此列表显示在父页面和每个兄弟子页面上。这是我到目前为止列出所有兄弟页面的内容:

<?php if($post->post_parent): ?>
<?php $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); ?>
<?php else: ?>
<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); ?>
<?php endif; ?>
<?php if ($children) { ?>
<ul class="subpage-list">
<?php echo $children; ?>
</ul>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

我相信我可能会想出一个答案。这段代码可能有点粗糙,但据我所知,它可以根据需要运行:

<?php 
if($post->post_parent):
  $postparent = $post->post_parent;
else: 
  $postparent = $post->ID;
endif; 

$nextTagThumb='-1';
$tags = wp_get_post_tags($postparent);
foreach ($tags as $tag) :

  if ($tags) {
    $what_tag = $tags[($nextTagThumb+'1')]->term_id;
    $tag_title = $tags[($nextTagThumb+'1')]->name;
    echo '<div class="Saving_sare">'. "\n";
    echo '<h4>'.$tag_title.'</h4>'. "\n";
    echo '<ul>'. "\n";
    $args=array(
      'tag__in' => array($what_tag),
      'post__not_in' => array($postparent),
      'showposts'=>100,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;
    }
    echo '</ul>'. "\n";
    echo '</div>'. "\n";
    wp_reset_query();
    $nextTagThumb = ($nextTagThumb+1);
  }
endforeach; 
?>