2010-04-08 34 views
3

我试图实现的是仅显示循环中的特定页面。 3页某些页面,不多,不少。在Wordpress中显示WP_Query的某些页面的数组

我已经尝试了很多东西,但我无法完成它。

<?php 
    $special_tabs = new WP_Query(array ('post_type'=>'page','post__in'=>array(100,102,104))) 
?> 

这样做,据我所知,它显示了一个页面的数组,然后在那里包含这些ID。

<?php 
    $special_tabs = new WP_Query('page_id=100'); 
?> 

而这只显示一个确定的页面,它不支持显示不同ID的数组。

我敢肯定,我不是第一个有这种特定需求的人,我确信有一个相对简单的方法来实现我想要达到的目标,但我不能拿出一个解决方案或找到一个。任何人都可以帮助我吗?

非常感谢提前!

+0

这个具体解决方案的答案很简单:我在那里有一个错字: 我有post_in,但实际上它是post_in,两个下划线。我错过了。 我最好保持睁大眼睛。 – Ragnar 2010-04-08 12:03:16

回答

1

只是想知道使用get_pages()而不是创建新的wp_query()会更好吗?

使用这小小的一段代码就可以生成你想要的页面列表..

<ul> 
<?php $special_tabs = get_pages ('sort_column=post_title&echo=0&include=2,3,4&title_li='); ?> 
<?php 
foreach ($special_tabs as $tab) { 
    $title = $tab->post_title; 
    echo "<li>".$title."</li>"; 
} 
?> 
</ul> 

如果你在$ special_tab变量的print_r你会得到下面的数组

</php 
echo"<pre>"; 
echo print_r($special_tabs); 
echo"</pre>"; 
?> 


    Array 
(
    [0] => stdClass Object 
     (
      [ID] => 2 
      [post_author] => 1 
      [post_date] => 2010-03-24 15:26:18 
      [post_date_gmt] => 2010-03-24 15:26:18 
      [post_content] => This is an example of a WordPress page. 
      [post_title] => About 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => open 
      [ping_status] => open 
      [post_password] => 
      [post_name] => about 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2010-03-24 15:26:18 
      [post_modified_gmt] => 2010-03-24 15:26:18 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://example.com/about/ 
      [menu_order] => 0 
      [post_type] => page 
      [post_mime_type] => 
      [comment_count] => 0 
      [filter] => raw 
     ) 

    [1] => stdClass Object 
     (
      [ID] => 3 
      [post_author] => 1 
      [post_date] => 2010-03-27 10:48:29 
      [post_date_gmt] => 2010-03-27 10:48:29 
      [post_content] => 
      [post_title] => testpage1 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => open 
      [ping_status] => open 
      [post_password] => 
      [post_name] => testpage1 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2010-03-27 10:48:29 
      [post_modified_gmt] => 2010-03-27 10:48:29 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://example.com/testpage1/ 
      [menu_order] => 0 
      [post_type] => page 
      [post_mime_type] => 
      [comment_count] => 0 
      [filter] => raw 
     ) 

    [2] => stdClass Object 
     (
      [ID] => 4 
      [post_author] => 1 
      [post_date] => 2010-03-27 10:56:26 
      [post_date_gmt] => 2010-03-27 10:56:26 
      [post_content] => 
      [post_title] => testpage2 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => open 
      [ping_status] => open 
      [post_password] => 
      [post_name] => testpage2 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2010-03-27 10:56:26 
      [post_modified_gmt] => 2010-03-27 10:56:26 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://example.com/testpage2/ 
      [menu_order] => 0 
      [post_type] => page 
      [post_mime_type] => 
      [comment_count] => 0 
      [filter] => raw 
     ) 

) 

希望这是你可以使用.. :) 只记得包括您的页面ID的get_pages()包括部分,而不是2,3,4多数民众赞成他们已经...

REF:http://codex.wordpress.org/Function_Reference/get_pages

+0

非常感谢,Marty。它绝对看起来工作。但我认为创建wp_query似乎比您提供的解决方案容易一些。但我仍然非常感谢您的帮助。每当我对页面进行操作时,我都会记住这个解决方案。 事情是,我需要在几个不同的地方显示几个页面。其中之一是special_tabs部分,我有2-3个其他地方。 – Ragnar 2010-04-08 12:06:37

0

这里是2+个月大的问题的答案:

你想显示某些页面,以在foreach呼吁WP_Query每个页面,这样怎么样:

$certain_pages = array(100,102,104); 
foreach($certain_pages as $a_page) { 
    $special_tabs = new WP_Query('page_id='.$a_page); 
    /* (sample) loop goes here */ 
    if ($special_tabs->have_posts()) : while ($special_tabs->have_posts()) : $special_tabs->the_post(); 
     the_content(); 
    endwhile; endif; 
    /* loop ends */ 
} 

不用担心多次调用循环,即使在这种规模下也无法察觉到这种效果。