2017-02-23 195 views
0

我打算創建一個側欄來檢索當前頁面的子項列表以及當前頁面本身。這裏是我一直在使用這甚至檢索當前頁面的兒童在上子頁面的代碼,但是當我嘗試包括$parent$post->post_parent它會顯示所有網頁獲取當前頁面的子頁面以及當前頁面鏈接

<?php 
    if ($post->post_parent) { 
     $ancestors=get_post_ancestors($post->ID); 
     $root=count($ancestors)-1; 
     $parent = $ancestors[$root]; 
    } else { 
     $parent = $post->ID; 
    } 
    $children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0"); 
    $parent = wp_list_pages("title_li=&child_of=". $post->post_parent ."&echo=0"); 
    if ($children) { ?> 
    <ul class="tabs"> 
     <?php echo $parent ?> 
     <?php echo $children; ?> 
    </ul> 
<?php } ?> 
+0

請添加您的代碼的_actual_結果和您的期望值。人們不喜歡猜測。 – Patru

回答

0
/*------------------------------------------------------------------- 
    Return parent page ID or child as else. Add this to functions.php 
---------------------------------------------------------------------*/ 

function get_top_parent_page_id() { 
    global $post; 

    if ($post->ancestors) { 
     return end($post->ancestors); 
    } else { 
     return $post->ID; 
    } 
} 

//使用這個在模板中

<?php while (have_posts()) : the_post(); 

    //Add parent page slug into arguments 
    $query = new WP_Query('pagename=page_slug'); 
    if ($query->have_posts()){ while ($query->have_posts()) { $query->the_post(); } } 

    //Get children by calling function in functions.php for the top parent id     
    $current_page_id = get_top_parent_page_id(); 
    $args = array('post_type' => 'safaris', 'post_parent' => $current_page_id, 'orderby' => 'title', 'order' => 'ASC'); 
    $child_query = new WP_Query($args); 

    $i = 0; 
    if ($child_query->have_posts()){ while ($child_query->have_posts()){ $child_query->the_post(); 
    $i++; if(1 == $i) { ?> 

    <li role="presentation" class="active"><?php the_title(); ?></li> 

    <?php } else { ?> 

    <li role="presentation"><?php the_title(); ?></li> 

    <?php } } } wp_reset_postdata(); endwhile; ?> 
+0

我以前用過這個。它可以幫助 – omukiguy

0

以下是有效的代碼。

<?php if (!is_page()) { ?> 
    <div class="card recentPosts"> 
     <h3>Recent posts</h3> 
     <?php 
     $args = array('numberposts' => 4, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date"); 
     $postslist = get_posts($args); 
     foreach ($postslist as $post) : setup_postdata($post); 
     $image_id = get_post_thumbnail_id(); 
     $image_url = wp_get_attachment_image_src($image_id,'thumbnail', true); 
     ?> 
      <a class="recentposts" href="<?php the_permalink(); ?>" title="<?php the_title();?>"> 
       <?php 
       if (has_post_thumbnail()) { 
       echo '<div class="aside image">'; 
       the_post_thumbnail('sidebarThumb'); 
       echo '</div>'; 
       } 
       ?> 

      <div class="title"> 
       <h3><?php the_title(); ?></h3> 
       <div class="meta"> 
        <div class="metaItem postedon"> 
         <?php echo get_the_date('F y'); ?> 
        </div> 
        <div class="metaItem author"> 
         <?php 
           global $post; 
          $fname = get_the_author_meta('first_name'); 
          $lname = get_the_author_meta('last_name'); 
          $full_name = ''; 

          if(empty($fname)){ 
           $full_name = $lname; 
          } elseif(empty($lname)){ 
           $full_name = $fname; 
          } else { 
           //both first name and last name are present 
           $full_name = "{$fname} {$lname}"; 
          } 

          echo 'By '.$full_name; 
          ?> 
        </div> 
       </div> 

      </div> 
      <div class="feature-image" style="background-image: url(<?php echo $image_url[0]; ?>);"></div> 
     </a> 
     <hr /> 
     <?php endforeach; 
     wp_reset_postdata(); 
     ?> 
    </div> 
<?php } ?>