2016-04-07 44 views
0

我有自定義帖子類型'汽車',其子帖子類型是'carvariants'。如何獲取當前自定義帖子的子帖子並按自定義字段號排序?

我想要做的就是讓孩子的帖子(carvariants)電流後(汽車)。我想這樣的代碼:

<div> 
    <?php 
    $parent_id = 1064; 
    $the_query = new WP_Query(array(
'post_parent' => $parent_id, 
     'post_type'   => 'carvariants', 
     'posts_per_page' => 1, 
     'meta_key'   => 'wpcf-minimum-price', 
     'orderby'   => 'meta_value_num', 
     'order'    => 'ASC' 
    )); 

    ?> 
    <?php if($the_query->have_posts()): ?> 
     <ul> 
     <?php while($the_query->have_posts()) : $the_query->the_post(); 
       $compprd = get_the_ID(); ?> 

    <?php the_title(); ?> 
    <?php 
     endwhile; ?> 
     </ul> 
    <?php endif; ?> 
    <?php wp_reset_query(); ?> 
    </div> 

我想通過自定義字段中顯示的汽車爲了孩子的帖子WPCF最小价格 但「post_parent」是行不通的。此代碼顯示空白輸出。這有什麼不對?

+0

這是很難提供解決方案的問題時 說法是根本,「它不工作」 。請編輯您的 問題,以更全面地描述您的 預期會發生什麼,以及與實際的 結果有何不同。請參閱[問]提示什麼使得一個好的 解釋。 –

+0

我想通過自定義字段wpcf-minimum-price顯示汽車訂單的子帖子,但'post_parent'不起作用。此代碼顯示空白輸出。這有什麼不對? –

回答

0

我沒有試過這個。但我希望這會起作用。

如果它不起作用,留下我的評論,我會盡力使它工作。

另外,如果有更好的解決方案,我會很高興地看到,從專業代碼:

<div> 
    <?php 
    $parent_id = 1064; 
    $args = array('child_of' => $parent_id); 

    $children_pages = get_pages($args); 

    if (count($children_pages) != 0) : 
     foreach ($children_pages as $children_page) : 
      if ($children_page->have_posts()) : 
        $args_for_posts = array('posts_per_page' => 1, 
         'post_type' => 'carvariants', 
         'orderby' => 'meta_value_num', 
         'order' => 'ASC', 
         'post_parent' => $children_page); 
        $postlist = get_posts($args_for_posts); 
        foreach ($postlist as $post) : 
         setup_postdata($post); ?> 
         <ul> 
          <?php 
          the_post(); 
          ?> 
         </ul>  
        <?php 
        endforeach; 
        wp_reset_postdata(); 
      endif; 
     endforeach; 
    else : ?> 
     <p>No content to show.</p> 
    <?php 
    endif; ?> 
</div> 
+0

謝謝Oleg9的回覆,但它顯示我_沒有內容show._消息。 –

相關問題