Wordpress模板中的备用帖子类型

时间:2017-05-10 15:09:48

标签: php wordpress

我目前在我的页面模板中有这个代码,显示3' Portfolio'项目然后3'新闻'项...

<?php
        $portfolio_args = array(
            'post_type' => 'portfolio',
            'posts_per_page' => 3
        );

        $portfolio = new WP_Query($portfolio_args);

        while($portfolio->have_posts()) {
            $portfolio->the_post();
            $post = new SeedPost(get_the_ID());
            $post->display();
        }
        wp_reset_query();

        $news_args = array(
            'post_type' => 'post',
            'posts_per_page' => 3
        );

        $news = new WP_Query($news_args);

        if($news->have_posts()) {
            while($news->have_posts()) {
                $news->the_post();
                $post = new SeedPost(get_the_ID());
                $post->display();
            }
        }
        wp_reset_query();
        ?>

是否有可能交替显示这些?所以不要像这样显示:

Portfolio    Portfolio    Portfolio 

News         News         News

它显示如下:

Portfolio     News         Portfolio

News          Portfolio    News

UPDATE&GT;&GT;&GT;&GT;&GT;&GT;&GT;

我试图从另一篇文章中实现解决方案:

$portfolio = array(
            'post_type' => 'portfolio'
        );

        $news = array(
            'post_type' => 'post'
        );

        $new = array();
        for ($i=0; $i<count($portfolio); $i++) {
           $new[] = $portfolio[$i];
           $new[] = $news[$i];
        }
        var_dump($new);

但它似乎没有起作用,我不能很好地理解PHP,知道什么是错的......

非常感谢

1 个答案:

答案 0 :(得分:1)

你只是错过了实际的“获取帖子”。在这种情况下,get_posts被指示而不是WP_Query,因为它返回一个我们必须迭代以提取信息的简单数组。

$portfolio_args = array(
     'post_type' => 'portfolio',
     'posts_per_page' => 3
 );
 $portfolio = get_posts($portfolio_args);

 $news_args = array(
     'post_type' => 'post',
     'posts_per_page' => 3
 );
 $news = get_posts($news_args);

 $all = array();
 for ($i=0; $i<count($portfolio); $i++) {
    $all[] = $portfolio[$i];
    $all[] = $news[$i];
 }

# Debugging, uncomment to check the variable
// printf('<pre>%s</pre>', print_r($all,true));

 for ($i=0; $i<count($all); $i++) {
    // to get the post title use $all[$i]->post_title and so on
    $post = new SeedPost($all[$i]->ID);
    $post->display();
 }

印刷:

Array
(
    [0] => WP_Post Object
        (
            [ID] => 25
            [post_author] => 1
            [post_date] => 2017-05-11 13:38:57
            [post_content] => 
            [post_title] => a portfolio item
            ...
            [post_type] => portfolio
            ...
        )

    [1] => WP_Post Object
        (
            [ID] => 17
            [post_author] => 1
            [post_date] => 2017-05-07 12:32:52
            [post_content] => 
            [post_title] => a post
            ...
            [post_type] => post
            ...
        )

    [2] => WP_Post Object
        (
            [ID] => 24
            [post_author] => 1
            [post_date] => 2017-05-11 13:38:40
            [post_content] => 
            [post_title] => 
            ...
            [post_type] => portfolio
        )

    [3] => WP_Post Object
        (
            [ID] => 15
            ...
            [post_type] => post
            ...
        )

    [4] => WP_Post Object
        (
            [ID] => 23
            ...
            [post_type] => portfolio
            ...
        )

    [5] => WP_Post Object
        (
            [ID] => 13
            ...
            [post_type] => post
            ...
        )

)