Wordpress looping odd even pages in two categories - php

I am building a website with ACF and I have two different categories of pages: articles and stories.
I would like build my page such that it is a list alternating articles in stories, but currently I'm getting all the articles first, then all the stories instead of them being interlaced.
You can see the website here http://www.ndstudio.no/wp-mindthegap/
I want the loop to be like this:
POST 1 (ARTICLE)
POST 2 (STORY)
POST 3 (ARTICLE)
POST 4 (STORY)
But at the moment it´s like this:
POST 1 (ARTICLE)
POST 2 (ARTICLE)
POST 3 (STORY)
POST 4 (STORY)
This is my code
<?php
$args = array(
'cat' => 5,
'post_type' => array( 'page' ),
'order' => 'ASC'
);
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
?>
<?php $colour = get_field('colour'); ?>
<div class="<?php print $colour; ?>-wrap">
<div class="first-text-content">
<h1 class="title-page"><?php the_title(); ?></h1>
<?php
// check if the repeater field has rows of data
if( have_rows('block') ):
// loop through the rows of data
while ( have_rows('block') ) : the_row();
?>
<!-- <p><?php the_sub_field('type'); ?><p> -->
<?php
$type = get_sub_field('type');
$quote = get_sub_field('quote');
$text = get_sub_field('text');
if ($type == "text") {
?>
<?php the_sub_field('text'); ?>
<?php
} else if ($type == "quote") {
?>
<div class="quote"><?php the_sub_field('quote'); ?></div>
<?php
}
?>
<?php
endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
<!-- STORIES categories -->
<?php
$args = array(
'cat' => 4,
'post_type' => array( 'page' ),
'order' => 'ASC'
);
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
?>
<div class="stories">
<div class="stories-length">
<div class="scroll-right"><img src="http://www.ndstudio.no/wp-mindthegap/wp-content/uploads/2015/05/scroll-right.png" /></div>
<img class="intro-image" src="<?php the_field('intro-image'); ?>" alt="" />
<div class="intro-column">
<h1 class="title-page"><?php the_title(); ?></h1>
<?php the_field('intro-text'); ?>
</div>
<?php
// check if the repeater field has rows of data
if( have_rows('content_block') ):
//consoleLog(get_field('block'));
// loop through the rows of data
while ( have_rows('content_block') ) : the_row();
?>
<!-- <p><?php the_sub_field('type'); ?><p> -->
<?php
// the_sub_field is printing the value, which is output into HTML
// get_sub_field is returning the value, which you'll store in a variable
$select = get_sub_field('select');
$textfield = get_sub_field('textfield');
$video = get_sub_field('video');
$quote = get_sub_field('quote');
$bgcolor = get_sub_field('bgcolor');
$image1 = get_sub_field('image1');
$image2 = get_sub_field('image2');
if ($select == "textfield") {
?>
<div class="text-column"><?php the_sub_field('textfield'); ?></div>
<?php
} else if ($select == "video") {
?>
<div class="video-row-2"><div class="article-video-2"><?php the_sub_field('video'); ?></div></div>
<?php
} else if ($select == "quote") {
?>
<div class="nautral-row"><div class="quotes"><?php the_sub_field('quote'); ?></div></div>
<?php
} else if ($select == "image1") {
?>
<div class="<?php print $bgcolor; ?>-row">
<img class="article-image" src="<?php the_sub_field('image1'); ?>" alt="" />
<?php
} else if ($select == "image2") {
?>
<img class="article-image" src="<?php the_sub_field('image2'); ?>" alt="" /></div>
<?php
}
?>
<?php
endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
Thank you so much for having a look.

Your current structure is:
Get Posts (A)
Render (A)
Get Posts (B)
Render (B)
Which is why it's following that pattern.
You need to either fetch and cache all the results before rendering:
Get Posts (A) -> Cache1
Get Posts (B) -> Cache2
Interleave/Merge Caches to make alternating Array
Render Merged Cache
Alternatively, cache the output.
Get Posts (A)
Render -> Cache
Get Posts (B)
Render -> Cache
Output Render Cache
Unless there is something Wordpress specific I don't know - those seem the two easiest approaches.

Related

Multiple loops: exclude a post returned in loop 1 from loop 2

I have two wp_query loops.
The first loop looks for a featured post and formats it differently than the rest of the posts.
Then the second loop displays the rest of the posts.
I want to exclude this first, featured post from the rest of the loop.
Right now, the post ID of the first post is saved as a variable.
I want to use that variable in the second loop, with the wp_query exclude argument.
But as far as I understand, that variable dies when my first loop ends. It's then a null variable in the second loop and so nothing is excluded.
<!-- Here's the query for the featured post -->
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
'meta_key' => 'featured_post',
'meta_value' => '1'
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- If there is no featured post, pull in the most recent post and make it big -->
<?php else: ?>
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<!-- End of the nested loop (most recent posts) -->
<?php endwhile; ?>
<?php endif; ?>
<!-- End of the featred post query loop-->
<?php endif; ?>
And here's the main loop:
<?php get_template_part('parts/content', 'featured-post'); ?>
<!-- This ends the logic for the featured post. Now we show the rest of the posts, excluding the first one we showed -->
<?php get_template_part('parts/content', 'category-filter'); ?>
<?php
// the arguments
$args=array(
'paged' => $paged,
'posts_per_page' => 9,
'post__not_in' => array($postid)
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- Start row that holds blocks -->
<div class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="column entry" >
<?php if( get_the_post_thumbnail() ): ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('blog'); ?>
</a>
<?php else : ?>
<?php endif; ?>
<h5><?php the_title(); ?></h5>
<?php
$excerpt = get_the_excerpt();
echo wp_trim_words( $excerpt , '10', '');
?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>Sorry, no more posts.</p>
<?php endif; ?>
How exactly do I set up a variable to use in the second loop?

Creating a loop within a loop in Wordpress to display 12 posts in groups of two contained in separate divs

I was hoping one of you Wordpress gurus can help me out here.
I'm trying to create a loop within a loop that presents 12 posts, in 6 rows. In each row you have 2 divs. Each div is meant to display a post title. And the loop runs through all of the 12 posts and groups them correctly - 6 divs, with 2 posts. Each post has it's own unique title.
I've managed to get the loop to breakdown the 12 posts into 6 divs, each with two inner divs in there. But I can't get the inner divs to loop through all the posts. Instead they are just looping through the first two.
So what I end up with is 6 rows, each with two divs. But only the first two posts keep recurring throughout all the rows. What am I doing wrong here?
<!--TESTER -->
<!--TESTER -->
<!--TESTER -->
<div class="section section-testimonials">
<?php
$args=array(
'post_type' => 'testimonial'
);
$query = null;
$query = new WP_Query($args);
if( $query -> have_posts() ) {
echo '';
$i = 0;
while ($query -> have_posts()) : $query->the_post();
if($i % 2 == 0) { ?>
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'testimonial', 'posts_per_page' => 2 ) ); ?>
<?php while ( $loop -> have_posts() ) : $loop -> the_post(); ?>
<!-- Two posts appear here -->
<div class="col-md-6">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
</div>
<?php } ?>
<?php
$i++;
endwhile;
}
wp_reset_query();
?>
</div>
<!--TESTER -->
<!--TESTER -->
<!--TESTER -->
Any help would be incredibly appreciated!
Cheers,
Sanny
Here, I break the problem into pieces, so you can see the loops working on their own.
<?php
$slides = [0,1,2,3,4,5];
foreach($slides as $slide):
?>
<!-- I need to get 6 of these divs which contain 2 cards inside them -->
<div class="item active">
<p>Example Carousel Slide <?php echo $slide;?></p>
</div>
<?php endforeach?>
Then, instead of just echoing a constant <p> tag, you instead put a loop there.
<?php
$posts = ["post1", "post2"];
foreach($posts as $post):?>
<div class="col-md-6">
<?php echo $post;?>
</div>
<?php endforeach?>
I will jump ahead a few steps, but you end up with something like this.
<?php
$posts = [
0=>"post0",
1=>"post1",
2=>"post2",
3=>"post3",
4=>"post4",
5=>"post5",
6=>"post6",
7=>"post7",
8=>"post8",
9=>"post9",
10=>"post10",
11=>"post11"];
for($slideNumber=0; $slideNumber < 6; $slideNumber++):
?>
<!-- I need to get 6 of these divs which contain 2 cards inside them -->
<div class="item active">
<?php
echo("<p>This is slide number " . $slideNumber . "</p>");
for($i=0; $i<2; $i++):?>
<div class="col-md-6">
<?php
$actualPostNumber= ($slideNumber * 2) + $i ;
echo("<p>" . $posts[$actualPostNumber] . "</p>");
?>
</div>
<?php endfor; ?>
</div>
<?php endfor; ?>
Read that code, and try to come up with an expectation for what it will produce. In the end, you should end up with 6 slides, each containing two posts.
The code I posted is a skeleton relative to your eventual solution. However, hopefully this "counters" approach will assist you with assigning the correct number of posts to each carousel slide.
Thanks guys for all the help. I've managed to solve this!!! Here's the solution:
If anyone has any thoughts on making this a better solution lemme know! Cheers
<?php
$args=array(
'post_type' => 'testimonial',
);
$query = new WP_Query($args);
if( $query -> have_posts() ) {
echo '';
$i = 0;
$ids = array();
while ($query -> have_posts()) : $query->the_post();
if($i % 2 == 0) { ?>
<div class="item <?php if($i == 0) :?>active<?php endif; ?>">
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'testimonial', 'posts_per_page' => 2, 'orderby' => 'rand', 'post__not_in' => $ids ) ); ?>
<?php while ( $loop -> have_posts() ) : $loop -> the_post(); ?>
<!-- A single testimonial -->
<div class="col-md-6">
<div class="card card-plain">
<div class="content">
<h4 class="title"><?php the_content(); ?></h4>
<div class="footer">
<div class="author">
<span><?php the_title(); ?></span>
</div>
</div>
</div>
</div>
</div>
<?php $ids[] = get_the_ID(); ?>
<?php endwhile; ?>
</div>
</div>
<?php } ?>
<?php
$i++;
endwhile;
}
wp_reset_query();
?>

ACF repeater showing just 1 row

So when I try to use ACF repeater field instead of showing me all the fields I get just the first one. The code is as follows.
<?php if( have_rows('vsi_projekti') ): ?>
<ul class="posts-grid">
<?php while( have_rows('vsi_projekti') ): the_row();
// vars
$image = get_sub_field('vsi_projekti_image');
$content = get_sub_field('project_name');
$link = get_sub_field('link_to_post');
?>
<li class="post-grid">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<div class="post-title-hover"><?php echo $content; ?></div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Any advice on what I'm doing wrong to be getting out just 1 row instead of multiple?
I don't know if this has anything to do with my problem or not, but I'm adding just 1 row in every post. But in the end I should get out more then just row I think?
I think that you are confused what a ACF repeater field does. If you enter just one row in every post witrh a repeater, it's normal that you get only one row... because your code works perfectly fine ... for a repeater within a post... when you add 15 rowds in your post you will get all 15rows as output...
But if you want to output every row of every repeater of every post, yopur code doesn't work. You should try this instead:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$posts = get_posts($args);
if( $posts ): ?>
<ul class="posts-grid">
<?php foreach( $posts as $post ): setup_postdata( $post ); ?>
<?php if( have_rows('vsi_projekti') ): ?>
<?php while( have_rows('vsi_projekti') ): the_row();
// vars
$image = get_sub_field('vsi_projekti_image');
$content = get_sub_field('project_name');
$link = get_sub_field('link_to_post');
?>
<li class="post-grid">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<div class="post-title-hover"><?php echo $content; ?></div>
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; //foreach( $posts as $post ) ?>
<?php wp_reset_postdata(); ?>
</ul>
<?php endif; // if( $posts ) ?>
This code gets all posts and loops throug them... In every loop the repeater field is put out....

My code causing infinite loop, can't see the bug

I am working on a WordPress project, using the Advanced Custom Fields plugin.
However I am getting a crazy infinite loop from my code, so I've obviously messed things up somewhere, however after a good two hours looking at it I can't see the problem.
<?php get_header(); ?>
<?php
// If the loop has posts
if (have_posts()) :
// For each post this page has
while (have_posts()) : the_post();
// Display the Flexible Content
while(has_sub_field('add_row')):
// If the content type is Tagline
if(get_row_layout() == 'tagline'): ?>
<div class="row paddingBottom paddingTop">
<div class="col-xs-12">
<h3 class="tagLine"><?php the_sub_field('tagline'); ?></h3>
</div>
</div>
<?php
// If the content type is a Featured Image
/*elseif ( get_row_layout() == 'featured_image' ): ?>
<div class="col-xs-12 textCenter">
<?php
$attachment_id = get_sub_field('featured_image');
$size = "full-width"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
</div>
<?php*/
// If the content type is a horizontal rule
elseif ( get_row_layout() == 'horizontal_rule' ) : ?>
<div class="row">
<div class="col-xs-12">
<hr />
</div>
</div>
<?php
// If the content type is Columns
elseif ( get_row_layout() == 'columns' ):
// If there is at least one column section
if(get_sub_field('column')):
// For each column section
while(has_sub_field('column')): ?>
<div class="row paddingTop paddingBottom">
<?php
if(get_sub_field('column_content_type')):
$count_rows = 0;
// Counting
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
// Loop
while(has_sub_field('column_content_type')): ?>
<div class="
<?php /*
if ( $count_rows == 1 ) :
echo "col-xs-12";
elseif ( $count_rows == 2 ) :
echo "col-xs-6";
elseif ( $count_rows == 3 ) :
echo "col-xs-6 col-sm-4";
elseif ( $count_rows == 4 ) :
echo "col-xs-6 col-sm-6 col-md-4";
endif; */
?>
"> <?php
// Title
if ( get_sub_field('title') ) : ?>
<h2 class="smallTitle"><?php the_sub_field('title');?></h2><?php
endif;
// Text Area
if ( get_sub_field('text') ) :
the_sub_field('text');
endif;
// Link
if ( get_sub_field('link') ) : ?>
<?php // eventually need to make it so you can change the link text ?>
<a class="textCenter" href="<?php the_sub_field('link'); ?>">
More info
</a> <?php
endif; ?>
</div> <?php
endwhile;
endif;
?>
</div>
<?php
endwhile; // end for each column section
endif; // end checking for at least one column section
endif; // end checking content type is columns
endwhile; // end flexible content
endwhile; // end for each post
endif; // end checking for at least one post
?>
<?php get_footer(); ?>
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
That's your problem... But it could be one of many based on what I'm seeing. It looks like you think while loops work like if statements... All you are doing in that loop is incrementing it over and over. It will never end.
I think the mistake is
// Display the Flexible Content
while(has_sub_field('add_row')):
i think you need to use if(){
// Code
}
instead of "while"

How to put two queries into an array within the following code?

I use a slider for my Wordpress featured articles. It selects a category and returns a custom amount of posts.
How do I make the first post that is displayed to be a custom one? Can I add an ID of a specific post directly in the slider code to make that post appear first, followed by the other ones that are returned by the original query?
For instance, on the page, the first post would be ID 6 (written in the source code manually) and the second, third and fourth posts are the ones returned by the query in the original code. How is this possible?
Someone suggested that this is very simple, all I need to do is to make another query before this to get the custom post, then close the query, reopen it to get the next query (that is in the slider code) and finally push the items into an array together. My very limited knowledge of PHP restricts me from understanding how to do this.
I know how to get a custom post, this code works:
<?php
$post_id = 6;
$queried_post = get_post($post_id);
?>
However, I do not know how I add this and the original query into an array. Do you?
Here is the full code for the slider:
<?php
$responsive = 'on' != get_option('henrik_responsive_layout') ? false : true;
$featured_auto_class = '';
if ( 'on' == get_option('henrik_slider_auto') ) $featured_auto_class .= ' et_slider_auto et_slider_speed_' . get_option('henrik_slider_autospeed');
?>
<div id="featured" class="<?php if ( $responsive ) echo 'flexslider' . $featured_auto_class; else echo 'et_cycle'; ?>">
<a id="left-arrow" href="#"><?php esc_html_e('Previous','henrik'); ?></a>
<a id="right-arrow" href="#"><?php esc_html_e('Next','henrik'); ?></a>
<?php if ( $responsive ) { ?>
<ul class="slides">
<?php } else { ?>
<div id="slides">
<?php } ?>
<?php global $ids;
$ids = array();
$arr = array();
$i=0;
$featured_cat = get_option('henrik_feat_cat');
$featured_num = (int) get_option('henrik_featured_num');
if (get_option('henrik_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));
else {
global $pages_number;
if (get_option('henrik_feat_pages') <> '') $featured_num = count(get_option('henrik_feat_pages'));
else $featured_num = $pages_number;
query_posts(array
('post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => (array) get_option('henrik_feat_pages'),
'showposts' => (int) $featured_num
));
} ?>
<?php if (have_posts()) : while (have_posts()) : the_post();
global $post; ?>
<?php if ( $responsive ) { ?>
<li class="slide">
<?php } else { ?>
<div class="slide">
<?php } ?>
<?php
$width = $responsive ? 960 : 958;
$height = 340;
$small_width = 95;
$small_height = 54;
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,'',$titletext,$titletext,false,'Featured');
$arr[$i]['thumbnail'] = get_thumbnail($small_width,$small_height,'',$titletext,$titletext,false,'Small');
$arr[$i]['titletext'] = $titletext;
$thumb = $thumbnail["thumb"];
print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, ''); ?>
<div class="featured-top-shadow"></div>
<div class="featured-bottom-shadow"></div>
<div class="featured-description">
<div class="feat_desc">
<p class="meta-info"><?php esc_html_e('Posted','henrik'); ?> <?php esc_html_e('by','henrik'); ?> <?php the_author_posts_link(); ?> <?php esc_html_e('on','henrik'); ?> <?php the_time(esc_attr(get_option('henrik_date_format'))) ?></p>
<h2 class="featured-title"><?php the_title(); ?></h2>
<p><?php truncate_post(410); ?></p>
</div>
<?php esc_html_e('Read More', 'henrik'); ?>
</div> <!-- end .description -->
<?php if ( $responsive ) { ?>
</li> <!-- end .slide -->
<?php } else { ?>
</div> <!-- end .slide -->
<?php } ?>
<?php $ids[] = $post->ID; $i++; endwhile; endif; wp_reset_query(); ?>
<?php if ( $responsive ) { ?>
</ul> <!-- end .slides -->
<?php } else { ?>
</div> <!-- end #slides -->
<?php } ?>
</div> <!-- end #featured -->
<div id="controllers" class="clearfix">
<ul>
<?php for ($i = 0; $i < $featured_num; $i++) { ?>
<li>
<div class="controller">
<a href="#"<?php if ( $i == 0 ) echo ' class="active"'; ?>>
<?php print_thumbnail( $arr[$i]['thumbnail']['thumb'], $arr[$i]['thumbnail']["use_timthumb"], $arr[$i]['titletext'], $small_width, $small_height ); ?>
<span class="overlay"></span>
</a>
</div>
</li>
<?php } ?>
</ul>
<div id="active_item"></div>
</div> <!-- end #controllers -->
If you choose to reply, please be detailed with code example, thank you.
line 39: replace that line with this one code below:
<?php if (have_posts()) : while (have_posts()) :
global $post;
if (!$first_time)
{
$post_id = 6;
$post = get_post($post_id);
$first_time = 1;
}
else the_post();
?>
Solution idea is simple:
Check for first-time loop
:firt loop - simply get needed post with "get_post()" function
:other loops - get posts from original query by "the_post()" function.

Categories