Query Advanced Custom Field not displaying - php

We have ACF Pro for WP and we have created an ACF which show a location which is a select.
When trying to output we are getting this:
Notice: Trying to get property of non-object in
/home/cwplantactiveint/public_html/wp-content/themes/cwplant/loop-jobs.php
on line 66
Which is this
<?php $location = get_field('job_location'); echo $location->post_title; ?>
Now oddly, it outputs another custom field which was createdto output the date:
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
The whole code block looks like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php /* Check closing date is not past. */
$today = strtotime("now");
$closedate = strtotime(get_field('closing_date'));
if ($today < $closedate || !get_field('closing_date')) {
?>
<div class="singlepost infobox info-job content cfix">
<h2><?php the_title(); ?></h2>
<p><span class="red first">Location:</span> <?php $location = get_field('job_location'); echo $location->post_title; ?>
<span class="red">Closing Date:</span>
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
</p>
<?php if ( is_archive() || is_search() || is_home()) : // Only display excerpts for archives and search. ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>">View Details</a>
<?php else : ?>
<?php the_content( __( 'Continue reading →', 'twentyten' ) ); ?>
<?php endif; ?>
</div>
<?php $jobstrue = 'true'; ?>
<?php } else { ?>
<?php $jobsfalse = 'true'; ?>
<?php } ?>
<?php endwhile; // End the loop. Whew. ?>

I think your problem is that you haven't reset the $post object with wp_reset_postdata() so your query is returning the last thing in $post (likely createdto in your case).
Whenever I handle any object in WP, I always set it up and then reset it like this:
<?php
// YOUR CUSTOM FIELD
// 0- Reset post object from last query (this should really be part of your last query)
wp_reset_postdata();
// 1- Put custom field into post object and check for content
$post_object = get_field('location');
// 2- Check to make sure object exists and setup $post (must use $post variable name)
if ($post_object) {
$post = $post_object;
setup_postdata($post);
// 3- Do some magic
echo get_field('closing_date');
// 4- reset postdata so other parts of the page can use it
wp_reset_postdata();
}
?>

Related

ACF - page selector get custom field as well

I am using Advanced Custom Fields and have chosen 'Page Selector' that allows me to choose pages.
On the front end, I am able to code it so that the pages I've chosen are on the home page with the title and page link but I'm also trying to pull through a custom field in that page as well.
Here is that code I have so far;
<?php if( have_rows('page_selector') ):
$i=1;
$count = (count($my_fields['value']));
?>
<?php while( have_rows('page_selector') ): the_row();
// vars
$page = get_sub_field('page');
?>
<div class="lg-col-6 md-col-6">
<div class="sub_service">
<?php
// vars
$post_id = get_sub_field('page', false, false);
// check
if( $post_id ): ?>
<h2><?php echo get_the_title($post_id); ?></h2>
<?php endif; ?>
//This is the custom field
<?php get_sub_field('description'); ?>
</div>
</div>
<?php
$i++;
endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Apologies in advance if it doesn't make sense, will try and explain further if it's confusing.
Thanks
You post the question, then the answer comes to you...
<?php the_field('description', $post_id); ?>

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>

Trouble Writing an If-else conditional for a loop in wordpress

I'm trying to write an if else conditional that displays one post if its from a catergory called home, but displays the default one if its not.
I'm not sure what syntax wordpress use's for this exactly to output either a post from the category or display the post from the default category.
If someone can give me some guidance I would be very greatful.
Here is what I've got so far, I'm not sure what I need to output in the if and else excute brackets.
Currently, I've got the latest posts from the individual post types to display and need to add a bit that basically overrides that if a post is in category home is selected.
<?php
$query = new WP_Query('post_type=testimonial&catergory_name=home&showposts=1&paged=' . $paged);
$postcount = 0;
?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php $postcount++; ?>
<li>
<a href="<?php the_permalink(); ?>">
//start of my ifelse conditional
<?php if (catergory_name=home == true) {
} else { }
<?php
if (has_post_thumbnail()) {
// check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumb-casestudy');
}else {
?>
<img src="<?php bloginfo('template_url'); ?>/assets/images/default.jpg" alt="<?php the_title(); ?>"/>
<?php } ?>
</a>
<h4><?php the_title(); ?></h4>
<p class="hm_text"><?php
//the_excerpt();
echo get_post_meta($query->post->ID, 'wpld_cf_home_page_text', true)
?></p>
</li>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
The problem is probably because you have no closing php tag after your else condition, yet you start another opening php tag.
//start of my ifelse conditional
<?php if (catergory_name=home == true) {
} else { }
?> <---- NEED TO CLOSE THIS CLAUSE
<?php
if (has_post_thumbnail()) {
// check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumb-casestudy');
}else {
?>

highlighting current post in recent posts sidebar , wordpress

Im just wondering how I could get the post id of the blog post I am currently reading while in another loop (i.e. the recent posts loop in the sidebar)
In my single post php file I have this code creating a variable
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php $current_post_id = get_the_ID(); ?>
<?php endwhile; // end of the loop. ?>
Then in my default-widgets php file, in the loop for recent posts I have this...
<?php while ($r->have_posts()) : $r->the_post(); ?>
<?php $recent_post_id = get_the_ID(); ?>
<li> <?php echo $recent_post_id; if ( $recent_post_id == $current_post_id ) { echo 'pass '; } else { echo 'fail ';} ?></li>
<?php endwhile; ?>
Its giving fail every time, so obviously what Im doing doesnt make sense (Im still learning). Im just wondering is there a way to grab the post id from the first loop, and use it in the second. At the moment when I echo current_post_id in the second loop nothing shows up.
(Regarding highlighting the current post, that'll be easy once I get this working.
Thanks for the help.
There are a lot of examples online on how to get the ID outside the loop.
try this in the widget code:
<?php
global $post;
$current_post_id = $post->ID;
while ($r->have_posts()) : $r->the_post(); ?>
<li> <?php echo $recent_post_id; if ( $recent_post_id == $current_post_id ) { echo 'pass '; } else { echo 'fail ';} ?><?php if ( get_the_title() ) the_title(); else the_ID(); ?></li>
<?php endwhile; ?>
Let us know if it works.

If Else with a twist - maybe

Can i make a If Else-statement that finishes a div before doing "else"?
I want to cycle through my posts and put the posts from "specialCat" in one div.
<div id="the-div-i-want-to-close">
<?php if (in_category('specialCat')) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
Thanks a lot :)
Your best bet is to modify the query calling your special category then finishing the loop with the rest of the posts.
<?php
$args = array(
'category_name' => 'special-cat', // Use Category slug
'posts_per_page' => -1 // Get all the post in SpecialCat
);
$special_query = new WP_Query( $args );
?>
<div id="the-div-i-want-to-close">
<?php
while ( $special_query->have_posts() ) : $special_query->the_post();
$no_repeat[] = $post->ID ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; ?>
</div> <?php // Close your special div after looping through all SpecialCat posts
// Start the loop again
if (have_posts() ) : while (have_posts() ) : the_post();
if (in_array( $post->ID, $no_repeat ) ) continue; // Exclude the posts we already got above ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
You can include HTML within a for each loop, so long as you close the PHP tags (or include the HTML in an echo statement).
Would this do it for you?
<div id="the-div-i-want-to-close">
<?php $isOpen = true; ?>
<?php if (in_category('specialCat')) { ?>
<?php the_title(); ?>
<?php } else { ?>
<?php if($isOpen) { ?>
</div>
<?php $isOpen = false; ?>
<?php } ?>
<?php the_title(); ?>
<?php } ?>

Categories