1

我正在使用 wordpress 创建一个网站,并且我有一个投资组合页面,其中显示了所有投资组合项目 - 这些项目由父页面“投资组合”的子页面组成。要在投资组合父页面上显示子页面及其内容,我需要一个特殊查询。为此,我只需为投资组合页面创建一个页面模板。在它上面我查询了数据库如下:

$parent = post->ID; //portfolio page ID
query_posts('post_type=page&post_parent='.$parent);//grab those children of portfolio

精彩的。然而,随着时间的推移,我了解到最好使用 pre_get_posts 来完成这样的工作,因为它对服务器和 wordpress 的负担更少。更难,但可能:

function portfolioloop($query){

if ($query->is_page( 'portfolio') ){//query will only effect my portfolio page

 $children = get_pages(array('child_of' => 35, 'echo' => 0) ); 
    // i dont think post->ID; works here. 
   //35 is the post ID of portfolio. Let me know if im wrong.

  // get and add the children id's to array. only id's is needed. 
  for($i = 0; $i < sizeof($children); ++$i) { 
  $portfolioitems[] = $children[$i]->ID; //$portfolioitems is a list of id's only. thats what we want
   } 

   $query->set('post__in', $portfolioitems);

 }


}
add_filter('pre_get_posts','portfolioLoop');

唉,它返回找不到页面。我做错了什么?如果您能帮助我,我将不胜感激,谢谢,

4

0 回答 0