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); ?>
Related
I am using the custom field plugin and i have made some custom fields in wordpress.
I wrote php to bring to the front the custom fields using the following code, but it doesn't stop looping and I need only 10 or 20 times to loop.
So I want to change but I don't know how.
<?php if( $banner_query2->have_posts() ): ?>
<?php while ( $banner_query2->have_posts() ) : $banner_query2->the_post(); ?>
<?php
$post_id = get_the_ID();
?>
<?php
$featured_matches = get_field('locations');
if( $featured_matches ): ?>
<ul>
<?php foreach( $featured_matches as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<!-- <?php //the_title(); ?> -->
<span><?php the_field( 'customfile' ); ?></span>
<span><?php the_field( 'customfile2' ); ?></span>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Its quite possible to terminate a foreach loop or any other early, by using break; so all you need to do is maintain a counter and when it reaches the magic number break the loop.
<?php
$cnt = 0;
$maxcnt = 10;
foreach( $featured_matches as $post ):
// do your funky stuff
$cnt++;
if ( $cnt >= $maxcnt ) :
break;
endif;
endforeach;
Seems like in wordpress if you add just break; in the end of the foreach loop works fine
I've made a custom loop which displays all posts and custom post type posts on one page.
I am looking to show under the title either the category of the post, or the post type "Case Study", depending on which section the post belongs to. There are no categories in my custom post type "Case Study".
The way the code is written here, it will show the category of the post and nothing if the post belongs to the custom post type "Case Study". I am having trouble setting up the conditional statement to show either a category name or the post type name.
<?php $all_query = new WP_Query(array('post_type'=>array( 'post', 'case-study'),
'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $all_query->have_posts() ) : ?>
<?php while ( $all_query->have_posts() ) : $all_query->the_post(); ?>
<?php the_title()?>
<?php the_category(', '); ?>
<?php the_excerpt();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
**EDIT
I have added this code (to replace the_category), but I realize it is sloppy and there is probably a better way. Any input or pointers is greatly appreciated!
<?php if (in_category('blog')) { ?>
<?php the_category(', '); ?>
<?php } elseif (in_category('pr')){ ?>
<?php the_category(', '); ?>
<?php } else { ?>
Case Study
<?php } ; ?>
What about this? I think get_post_type() will use the current post in the loop. If not you need to pass either the ID or the WP_Post object into that function.
if( get_post_type() == 'post'){
the_category(', ');
}elseif( get_post_type() == 'case-study'){
//your code
}
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();
}
?>
I would like to display custom field on product page.
I made field (Advanced Custom Fields) and add a rule to display in selected category on Posts. It works, I placed there a simple text and would display in product page. In editor I edit template and pasted the code:
<?php the_field( 'my_info' ); ?>
Unfortunately nothing appears.
I try also something like this:
<?php
query_posts('cat=195&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<?php if( get_field('my_info') ): ?>
<?php the_field('my_info'); ?>
<?php endif; ?>
<?php endwhile;
?>
After this code field display, but there is a problem with loading other page sections.
What I make wrong?
Since query_posts() is used, you have to put wp_reset_query(); after the endwhile(). Otherwise use WP_Query .
<?php
$query=WP_Query('cat=195&posts_per_page=1');
while ($query->have_posts()) : $query->the_post(); ?>
<?php if( get_field('my_info') ): ?>
<?php the_field('my_info'); ?>
<?php endif; ?>
<?php endwhile; ?>
I'm having hard time with this problems since I cant figure it out.
I'm using 2 WP_Query loops for custom post types (slider and portfolio) on the same page.
I also created a custom meta box for both custom post types.
So here is the code for index.php which Im using as Home template for displaying slider and portfolio items:
<?php
/*
Template Name: Home
*/
?>
<?php get_header(); ?>
<div id="header-container">
<div id="header">
<?php rm_slider(); ?> // This is where Im calling slider function to display the slider.
</div>
</div>
<div id="content">
<div class="container">
<?php $loop = new WP_Query(
array(
'post_type' => 'portfolio',
'posts_per_page' => -1
));
?>
<?php if ($loop->have_posts()) { ?>
<ul class="services">
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<li>
<?php if (has_post_thumbnail()) : the_post_thumbnail(); ?>
<?php else: ?>
<p>No portfolio image</p>
<?php endif; ?>
<h3><?php the_title(); ?></h3>
<p>Client: <?php echo get_post_meta($post->ID, '_project_client', true); ?></p>
<p>Client website: <?php echo get_post_meta($post->ID, '_project_client_url', true); ?></p>
</li>
<?php endwhile; } ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
And here is the code for slider.php:
<?php
// create slider markup
function rm_slider() {
$slider_loop = new WP_Query(
array(
'post_type' => 'slider',
'posts_per_page' => -1
));
if ($slider_loop->have_posts()) { ?>
<div id="slider">
<div class="slider-container">
<?php while ($slider_loop->have_posts()) : $slider_loop->the_post(); ?>
<div>
<?php if (has_post_thumbnail()) : the_post_thumbnail(); ?>
<?php else: ?>
<p>No slider image</p>
<?php endif; ?>
<div class="slide-info">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
$slide_url = get_post_meta($post->ID, '_slide_url', true);
if ($slide_url != '') { ?>
<?php echo $slide_url; ?>
<?php } else { echo 'empty?'; ?>
<?php
}
?>
</div>
<?php endwhile; ?>
</div><!-- .slider-container -->
</div><!-- #slider -->
<?php }
wp_reset_query();
}
?>
Im sure that the actual content from custom meta boxes is there, because when I only use 1 loop, it displays perfectly. But when using both loops, it only displays custom post meta only for the portfolio section. Im struggling with this problem whole day, please help me! Thanks :)
Strange, try changing this:
$slide_url = get_post_meta($post->ID, '_slide_url', true);
echo get_post_meta($post->ID, '_project_client', true);
for this:
$slide_url = get_post_meta(get_the_ID(), '_slide_url', true);
echo get_post_meta(get_the_ID(), '_project_client', true);
You could also try getting all post meta just to see if its all there.
$meta = get_post_meta( get_the_ID( ) );
print_r($meta); // prints the meta array to the screen, check your data is there.
As far as I know, after every WP_Query() you should use:
wp_reset_postdata();
NOT wp_reset_query();. Have a try with this.
wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function. As noted in the examples below, it's heavily encouraged to use the pre_get_posts filter to alter query parameters before the query is made.
and
wp_reset_postdata() is used to restore the global $post variable of the main query loop after a secondary query loop using new WP_Query. It restores the $post variable to the current post in the main query.
And I also suggest you to try changing the possible redundant variable name like $loop to something like $portfoliowLoop etc.