Wordpress Custom Query Posts Showposts number - php

Below is a script that I'm still unsure on how to get it to work, in Wordpress I have a repeater field that I can input the number of days that are in a month, so it creates calendar squares for me to highlight in a booking process.
What I want to do, is to have the field 'how_many_days' to run a loop that will then repeat the number of divs calendarPost. So Ideally I can input separate number of loops.
A live version of the output is here: http://universitycompare.com/school-bookings/
<?php if(get_field('calendar_repeater_field')): ?>
<?php while(has_sub_field('calendar_repeater_field')): ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php $wp_query->query('showposts='.$numberoffields ); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
<?php endwhile; ?>
<?php endif; ?>
FYI - I didn't know whether this would be a PHP related problem or WP only, so please advise if it this post should be elsewhere and I will remove and repost in the correct stackoverflow forum.

Your question didn't completly explain if you were in fact trying to output posts so below is a couple of suggesstions.
I'll start with what I think you're trying to do:
If you're' just wanting to output the div.calendarPost over and over (based on the number of days) then you don't need a WordPress loop for that. A standard PHP for loop will do
<?php if ( get_field('calendar_repeater_field' ) ) : ?>
<?php while ( has_sub_field('calendar_repeater_field' ) ) : ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php for ( $i=0; $i < $numberoffields; $i++ ) { ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
If however you're wanting to output posts (based on the number of days in the ACF field) then you would use the below code.
<?php if ( get_field('calendar_repeater_field' ) ) : ?>
<?php while ( has_sub_field('calendar_repeater_field' ) ) : ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php $calendar_posts = new WP_Query('posts_per_page=' . $numberoffields); ?>
<?php if ( $calendar_posts->have_posts() ) : while ( $calendar_posts->have_posts() ) : $calendar_posts->the_post(); ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Refer to "The Usage" section of the WP Codex for more info: http://codex.wordpress.org/Class_Reference/WP_Query.
Hope that helps.

Related

WordPress: Unable to read custom field that is part of a fields group

I added a new custom field to a fields group to be used only to render the homepage. When trying read and display the contents of this field with the code below, nothing happens. I am sure the way to read custom field is how it should be. I don't get any error message. Could you please help fix this? This is being done on a WordPress site.
<div class="bg-white-base relative pt-row pb-row pl pr">
<div class="wrap-x">
<div class="inside mini">
<?php if ( $headline = get_sub_field( 'slider_-_h1' ) ) : ?>
<h2 class="mb color-purple-base">
<?php echo $headline; ?>
</h2>
<?php endif; ?>
<?php if ( $subheadline = get_sub_field( 'slider_-_h2' ) ) : ?>
<h3 class="mb alt-heading h5">
<?php echo $subheadline; ?>
</h3>
<?php endif; ?>
<?php if ( $text_area = get_sub_field( 'slider-_shortcode' ) ) : ?>
<article class="body-area mt2x">
<?php echo $text_area; ?>
</article>
<?php endif; ?>
</div>
</div>
</div>
get_sub_field() is supposed to be use inside a loop. When you use a group, you may use a loop like :
while( have_rows('hero') ): the_row();
........
endwhile;
Everything is explained here : https://www.advancedcustomfields.com/resources/group/

I want to have 3 boxes across page with one specific post in each

I am trying to have 3 boxes across my WP page.
<div class="row text-center">
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
The object is to have one different specific post in each of the three boxes.
I am trying to make a page that has every block of text easily editable from the admin rather than the code. I am using a plugin that allows me to add a shortcode to a post where I want my specific post to show.
I have the boxes in place and have everything correct.
The first box populates with the correct post as it should but the other two won't display their posts.
It seems that maybe I can only use the loop once on the page, if so how can I achieve my aims?
Thanks for reading, any understandable advice will be very welcome, I have only recently started custom themes and PhP is a bit of a struggle too so I would be grateful if the help could be dumbed down a bit.
NB: I have tried replacing break with continue as suggested but the second and third post still don't show. Is it possible that there is more than just replacing the word break with continue?
Your loop breaks out of the foreach loop on the second iteration since $count = 1. You might have wanted to do continue to skip the 2nd post?
(Don't be tempted to think that break just breaks out of the conditional block. The conditional logic simply chooses which path or block of code to execute.)
EDIT
First of all, you are doing a loop for each box...The purpose of the loop is so that you don't need to repeat your code.
Second, you are restarting the loop each time..
Use the loop like this:
$query = new WP_Query( array( 'post_type' => 'post' ) );
<?php if ( $query->have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
<?php the_title();
the_content();
?>
</div>
<?php endwhile;
wp_reset_postdata();
endif;
?>
There a couple ways to do the query. I do like the get_posts better for simple queries because you don't need to reset the postdata using that way. You'd pass in the same arguments to the WP_Query.
Also I recommend you create a page template for this and assign it to a page. This way if you want to change your template you can just assign the page a different one instead of modifying your index.php code.

Manually show specific posts on homepage

To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});

Display Post with category id in 5th and 10th place - Wordpress

I need to display separate post with category id after 5th and 10th place. I searched for a query and the query shows,
<?php $postnum++;
if ($postnum == 5) { ?>
<div class="block">
<h2>Blog</h2></div>
<?php }
if ($postnum == 10) { ?>
<div class="block"><h2>References</h2></div>
<?php }
After putting the above code before endwhile, i can see the Blog and References after 5th and 10th place. But while inserting,
<?php $postnum++;
if ($postnum == 5) { ?>
<div class="block">
<?php
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title();
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php }
if ($postnum == 10) { ?>
<div class="block"><?php
query_posts( array(cat=>4, orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title();
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php } </div>
<?php }
The above code doesnot seems to work correctly. Please anyone help me..
Could be a few things. Check your server logs, but at the very least
<?php the_title();
<?php endwhile; ?>
is wrong - either you need to add ?> after the_title(); or remove <?php before endwhile;
Similarly, this:
<?php } </div>
Looks like it's missing a ?>.
I'd probably add quotes around the query_posts() parameters as well, for clarity:
query_posts( array('cat'=>3, 'orderby'=>'post_date', 'order'=>'desc') );
And I'd read this too, just so you're confident query_posts is the right approach for you.

WordPress Advanced Custom Fields repeater field -- get_field not working for me

Quasi-noob here. I just can't get any result from get_field. Here's my test page:
http://23.21.199.240/test
I can retrieve the repeater field data with a FOR loop, but the special ACF get_field function isn't doing what I expected. (It seems so simple, so I won't be surprised if I'm making a totally bonehead mistake.)
Any help would be greatly appreciated!
<?php
/*
Template Name: test
*/
?>
<?php get_header(); ?>
<?php query_posts('category_name=worldwise&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
id: <?php the_ID(); ?><br />
<!--let's try to display all the topics (an ACF repeater field) for this post....-->
<?php if(get_field('topic')): // if there are topics, get all their data in an array ?>
<?php
$topic_list_1 = ''; // set up an empty topic list
while(has_sub_field('topic')): // for every topic
$topic_title_1 = get_sub_field('topic_title'); // get the title
$topic_list_1 .= '<li>' . $topic_title_1 . '</li>'; // and add it to the topic list
endwhile; // no more topics, move on
?>
<p><strong>The FOR loop produced these topics:</strong></p>
<ul><?php echo $topic_list_1; ?></ul>
<?php else: ?>
<p style="color:red">GET_FIELD did not find any topics</p>
<?php endif; ?>
<!--or, let's try displaying those topics another way....-->
<?php
$vid_id = $post->ID;
$vid_custom = get_post_custom($vid_id);
?>
<?php if($vid_custom['topic'][0] != null): ?>
<?php
$topic_list_2 = '';
for($r=0;$r<$vid_custom['topic'][0];$r++):
$topic_title_2 = $vid_custom[topic_.$r._topic_title][0];
$topic_list_2 .= '<li>' . $topic_title_2 . '</li>';
endfor;
?>
<p><strong>The FOR loop produced these topics:</strong></p>
<ul><?php echo $topic_list_2; ?></ul>
<?php else: ?>
<p style="color:red">The FOR loop did not find any topics</p>
<?php endif; ?>
<br />
<?php endwhile; else: ?>
<p>Sorry, no posts or pages matched your criteria.</p>
<?php endif; ?>
I don't like using get_sub_field methods as it gets a bit complicated when you're nesting repeater fields.
This is what's in my opinion the easiest way to get what you're trying to do:
<ul>
<?php foreach (get_field('topic') as $row) :?>
<li><?php print $row['topic_title'] ?></li>
<?php endforeach; ?>
</ul>

Categories