Reputation: 972
I currently have this code in my page template that displays 3 'Portfolio' Items and then 3 'News' Items...
<?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();
?>
Would it be possible to alternates how these display? so instead of displaying like this:
Portfolio Portfolio Portfolio
News News News
It displays like this:
Portfolio News Portfolio
News Portfolio News
UPDATE>>>>>>>
I have tried to implement a solution from another post here:
$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);
But it doesn't seem to be working and I don't really understand PHP well enough to know whats wrong...
Many thanks
Upvotes: 1
Views: 112
Reputation: 2733
OK so, without knowing whether $post->display()
function relies on the global post object/query its worth trying this:
<?php
global $post;
$portfolio_args = array(
'post_type' => 'portfolio',
'posts_per_page' => 3
);
$portfolio_posts = get_posts($portfolio_args);
foreach ($portfolio_posts as $portfolio_post) {
$posts_list['portfolio'][] = $post;
}
$news_args = array(
'post_type' => 'post',
'posts_per_page' => 3
);
$news_posts = get_posts($news_args);
foreach ($news_posts as $news_post) {
$posts_list['news'][] = $post;
}
for ($i = 0; $i < count($posts_list['portfolio']); $i++) {
$post = $posts_list['portfolio'][$i];
setup_postdata($post);
$post = new SeedPost(get_the_ID());
$post->display();
wp_reset_postdata();
if (isset($posts_list['news'][$i])) {
$post = $posts_list['news'][$i];
setup_postdata($post);
$post = new SeedPost(get_the_ID());
$post->display();
wp_reset_postdata();
unset($posts_list['news'][$i]);
}
}
/* If there are any left news. */
foreach($posts_list['news'] as $post) {
setup_postdata($post);
$post = new SeedPost(get_the_ID());
$post->display();
wp_reset_postdata();
}
?>
Upvotes: 0
Reputation: 26065
You're missing just the actual "getting the posts". In a case like this, get_posts
is indicated instead of WP_Query as it returns a simple array that we have to iterate to extract the information.
$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();
}
Printed:
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
...
)
)
Upvotes: 1