Hi,
This is what worked for me. My custom field was a single select. I had to use the following to display the term:
<?php $term = get_field('trailer_condition');
if( $term ): ?>
<div class="trailercondition">Condition: <?php echo $term[0]->name; ?></div>
<?php endif; ?>
Hope that helps.
Something like this? ๐
<?php if( have_rows('questions') ): ?>
<?php $counter = 1; //this sets up the counter starting at 0 ?>
<ul class="questions-answers">
<?php while( have_rows('questions') ): the_row(); ?>
<li><h3><a href="#"><?php the_sub_field('question_text'); ?></a></h3>
<p class="class_<?php echo $counter; // Prints the number of counted row ?>"><?php the_sub_field('answer_text'); ?></p>
<p><?php the_sub_field('answer_text'); ?></p></li>
<?php $counter++; // add one per row ?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Thanks @elliot for the reply.
I believe I’ve solved it. I’ve used the below to do what I need, not sure if there is a cleaner way though:
<ul>
<?php
$args = array(
'post_type' => 'job_sectors',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'name'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li><a href="<?php echo get_permalink($pageChild->ID);?>" title="<?php the_title(); ?>"><?php the_title(); ?><?php #the_ID(); ?></a>
<?php
global $post;
$args = array(
'post_type' => 'jobs',
'meta_query' => array(
array(
'key' => 'job_sector',
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
);
$myposts = get_posts( $args );
$total = 0;
foreach( $myposts as $post ) :
$total = $total + $post;
?>
<?php endforeach;
wp_reset_postdata();
echo "($total)";
?>
</li>
<?php endwhile; ?>
</ul>
Thanks
Sure! See below. Need anything else?
this.toolbars
: undefined
this.o
:
{
id: "wysiwyg-acf-field-field_52ba5d6289438_0_field_52ba64a564a1b-52fd8bc9b44f6"
toolbar: "full"
upload: "no"
__proto__: Object
}
Hello @elliot,
sorry about that, I had never used this feature before!
this is the admin-ajax.php html response:
{"next_page_exists":0,"html":"<li><a href=\"http:\/\/dev\/spa\/wp\/destaque\/destaque-1\/\" data-post_id=\"36\"><img src=\"http:\/\/dev\/spa\/wp\/wp-content\/uploads\/2014\/02\/02082_apeacefulscottishevening_2560x1440-120x60.jpg\" \/><span class=\"relationship-item-info\"><\/span>Destaque 1<span class=\"acf-button-add\"><\/span><\/a><\/li><li><a href=\"http:\/\/dev\/spa\/wp\/destaque\/destaque-2\/\" data-post_id=\"38\"><img src=\"http:\/\/dev\/spa\/wp\/wp-content\/uploads\/2014\/02\/02014_thisoldhouse_2560x1440-120x60.jpg\" \/><span class=\"relationship-item-info\"><\/span>Destaque 2<span class=\"acf-button-add\"><\/span><\/a><\/li>"}
and the attached image shows the json response (think you need some additional info about this JSON reponse, correct?)
I just fixed because I realized I asked a similar question before.
For those wondering how it’s done:
1. Go to core/fields/post_object.php
2. In line 93 You can either specify by post_status you want to include by making an array or just put ‘any’ to show all.
Hi Elliot,
Thanks a lot for always being there for answer all my questions.
Basically what I have to do is to populate a repeater field from the front end so it might be a better option to forget GF and just go directly to your front end form?
Regarding this matter, I have a couple of questions:
Sorry for bothering you this bad but I have never worked with front end form before.
I’m attaching an image to show you what I see on the page when adding the form (in addition to that I also see all other custom fields).
If you could guide me or point me to any resource to help me understand and solve this, I will be eternally appreciated.
Best!
I was able to fix it!
I changed my code to:
<?php $args = array( 'posts_per_page' => -1, 'post_type' => 'siteinfo' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$phone = get_field('phone_number'); ?>
<div class="phone">
<p><img class="phone-img" src="<?php bloginfo('template_directory'); ?>/images/phone.png" alt="<?php bloginfo('name'); ?>" /><span><?php echo $phone; ?></span></p>
</div><!--end.phone-->
<?php endforeach;
wp_reset_postdata();?>
Thanks for your help!
Hi @mccawphoto
The code:
<?php $posts = get_posts(array(
'post_type' => 'siteinfo',
'posts_per_page' => -1
));
$phone = get_field('phone_number');
if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post();
Is not valid.
Please read the get_posts
documenation to understand how to loop over the posts.
Perhaps you intended to use WP_Query
instead? Please research this as well.
Thanks
E
The above code does not ouput anything to HTML.
Please echo any PHP value to HTML.
<img src="<?php echo $image; ?>" />
Thanks
E
Hi @jarvis
You can first get all the sectors by performing a get_posts
– http://codex.wordpress.org/Template_Tags/get_posts
Loop over these results, and for each sector display the title and link.
To find the count of jobs in this sector will require another get_posts
call for each sector.
The post_object (multi-select) saves it’s data in the same format of a relationship field (serialized array of IDs), so you can follow this tutorial to perform a backwards query: http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/
Thanks
E
Hi Elliot, thank you for getting back to me.
I’m using the Relationship field with Flexible Content to create a list of selected posts which is called Featured Posts:
<?php
while( has_sub_field( 'page_builder' ) ):
if( get_row_layout() == 'featured_posts' ):
get_template_part ( 'builder/featured', 'posts' );
endif;
endwhile;
?>
The loop inside featured-posts.php looks like this:
<?php
$featured_posts = get_sub_field( 'featured_post_add' );
if( $featured_posts ):
foreach( $featured_posts as $post ): ?>
<article>
...
</article>
<?php
endforeach;
wp_reset_postdata();
endif;
?>
Everything works great.
Now what i’m trying to do, is to output the exact same posts, added in this Relationship field, in a widget. I have created a widget file featured-posts-widget.php which i drag into the sideabr. The thing is that the posts are not showing up and the widget is blank, besides the title, no matter what type of loop or a query i use. The code among others i tried is:
<?php
global $post;
$featured_posts_sidebar = get_field( 'featured_post_add' );
if( $featured_posts_sidebar ):
foreach( $featured_posts_sidebar as $post ): ?>
<article>
...
</article>
<?php
endforeach;
wp_reset_postdata();
endif;
?>
In the widget i use get_field and not get_sub_field. Maybe using WP_Query and not the foreach loop would be a solution, but what parameters do i have to use?
Can you point me to the right direction?
Hi! Just wanted to update you on this. I had a programmer look at it and apparently the issue was that the $posts global variable was being overridden.
When I searched through the code, the only places I saw that variable mentioned was in the main WordPress files, the ACF plugin, and the Event Calendar plugin (which I tried disabling and didn’t make a difference). So I’m not sure exactly what was the culprit there, but once that variable name was changed, it worked fine.
Here’s the code that ended up working in case anyone else has a similar issue:
<?php $store_type_posts = (array) get_field('store-type'); ?>
<?php foreach ($store_type_posts as $store_type_post): ?>
<?php $store_type_logo_id = get_field('store-type-logo', $store_type_post->ID) ?>
<?php if ($store_type_logo_id) : ?>
<?php $image = wp_get_attachment_image_src($store_type_logo_id, 'Store_Logo'); ?>
<?php $alt_text = get_the_title($store_type_logo_id); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo $alt_text; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" class="plain" />
<?php endif ?>
<?php endforeach ?>
Looks like you are missing the closing single quote on ‘artist’
I’ve tried that but had no luck the filed should just be gallery I believe,
this is the other gallery that shows up on the page http://dev.rgbdesignuk.com/memorial/test-test/
<h2>Gallery</h2>
<?php if(get_field('gallery')):?>
<?php foreach (get_field('gallery') as $gal ):?>
<a class="fancybox" rel="gallery1" href="<?php echo $gal['url']?>" title="<?php echo $gal['caption'] ?>"><img src="<?php echo $gal['sizes']['thumbnail']?>" alt="" /></a>
<?php endforeach ?>
<?php endif; ?>
<?php if( $the_galquery->have_posts() ): ?>
<?php while ( $the_galquery->have_posts() ) : $the_galquery->the_post(); ?>
<a class="fancybox" rel="gallery1" href="<?php echo wp_get_attachment_url( get_the_ID()); ?>" ><?php echo wp_get_attachment_image( get_the_ID(), 'thumbnail'); ?></a>
<?php endwhile; ?>
<?php endif; ?>
<?php if ( !(get_field('gallery')) && !($the_galquery->have_posts() ) ):?>
<p>No Gallery added</p>
<?php endif ?>
<?php if ( is_user_logged_in() ): ?>
<form style='clear:both' id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
<p>Add image to gallery</p>
<p id="async-upload-wrap"><label for="async-upload">upload</label>
<input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload"></p>
<p><input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>" />
<?php wp_nonce_field('client-file-upload'); ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /></p>
<p><input type="submit" value="Save all changes" name="save" style="display: none;"></p>
</form>
<form style='clear:both' id="embed-form" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
<p>Add video to gallery</p>
<label for="embed">You tube / vim url:</label>
<textarea name="embed"></textarea>
<input name='submit_video' type="submit" value="submit">
<p>
<input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>" />
<?php wp_nonce_field('embed-form'); ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /></p>
</form>
<?php endif?>
</div>
Hi Elliot,
I’ve fixed it. Interchange does indeed use ajax to switch out files and I think that’s why the acf values weren’t being loaded – because, as you correctly pointed out, wp didn’t know which post to pull them from.
I added the page ID to the repeater code and it worked:
<?php
require($_SERVER['DOCUMENT_ROOT'].'/pwp/wp-load.php');
?>
<div class="phone_email_holder">
<p>LARGE TEMPLATE</p>
<?php if( have_rows('phone_numbers', 7) ): ?>
<?php while( have_rows('phone_numbers', 7) ): the_row(); ?>
<div class="contact_field_wrapper row">
<div class="phone_email_label info_label">
<?php echo get_sub_field('number_label', 7) ?>:
</div>
<div class="phone_email_value">
<?php echo get_sub_field('tel_number', 7) ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
However, it does have to include the wp-load file to work.
Thanks for your help on this. I only recently discovered ACF and it’s bloody amazing. Good work!
Thanks,
Xav
Im not sure if i have done this right or not its on the following page surposed to be in the side bar http://dev.rgbdesignuk.com/memorial/test-test/
but its not showing up please help
<!--slider starts-->
<!-- Start Photo Slider -->
<?php
/*
* Create the Markup for a slider
* This example will create the Markup for Flexslider (http://www.woothemes.com/flexslider/)
*/
$property_images = get_field('gallery');
if( $property_images ) { ?>
<div id="slider" class="flexslider">
<ul class="slides">
<?php foreach( $property_images as $property_image ): ?>
<li>
<img src="<?php echo $gallery_image['url']; ?>" alt="<?php echo $gallery_image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
</div>
<?php
/*
* The following code creates the thumbnail navigation
*/
?>
<div id="carousel" class="flexslider">
<ul class="slides">
<?php foreach( $gallery_images as $gallery_image ): ?>
<li>
<img src="<?php echo $gallery_image['sizes']['thumbnail']; ?>" alt="<?php echo $gallery_image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
</div>
<?php
}else{
?><div id="single-gallery-image">
<?php the_post_thumbnail('full'); ?>
</div><?php
?>
<?php } ?>
<!-- End Photo Slider -->
</div>
<link rel="stylesheet" href="http://dev.rgbdesignuk.com/wp-content/themes/canvas-child/memorials/css/flexslider.css" type="text/css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://dev.rgbdesignuk.com/wp-content/themes/canvas-child/memorials/js/jquery.flexslider-min.js"></script>
<script type="text/javascript">
var $f = jQuery.noConflict(true);
$f(window).load(function() {
// The slider being synced must be initialized first
$f('#carousel').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
itemWidth: 75,
itemMargin: 5,
asNavFor: '#slider'
});
$f('#slider').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
sync: "#carousel"
});
});
</script>
<!--slider ends-->
Sorry,
The true solution
<?php
$posts = get_field('sidebar_widget');
$args = array(
'post_type' => 'sidebar-widget',
'name'=> ($posts->post_name), //get the value in array
);
$query = new WP_Query( $args );
// The Loop
?>
<?php if($query->have_posts()) : ?>
<?php while($query->have_posts()) : ?>
<?php $query->the_post(); ?>
<?php include('widget/content-sidebar-widget.php'); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hereโs the code I created.
This plugin works well and is very handy. However I’ve just run into one caveat โ when using the Reusable Group Field within a Flexible Content Field you can only use it once per page.
If you try and use a Reusable Group in more than one row it doesn’t seem to store a separate database entry for each instance of the group.
This would be such a great feature to have as it would give ACF a really nice modular approach.
Yes, the page breaks after the navigation at the top of the page.
Now I have this error showing up where the page breaks:
“Fatal error: Call to a member function have_posts() on a non-object in /home/senroctech/public_html/wp-content/themes/Senroc/banner-cat.php on line 66”
Here is my code where the page breaks:
<?php $posts = get_posts(array(
'post_type' => 'siteinfo',
'posts_per_page' => -1
));
$phone = get_field('phone_number');
if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post();
<div class="phone">
<p><img class="phone-img" src="<?php bloginfo('template_directory'); ?>/images/phone.png" alt="<?php bloginfo('name'); ?>" /><span><?php echo $phone; ?></span></p>
</div><!--end.phone-->
<?php endwhile;
endif; ?>
Hahaha I’m a total moron!
I had two test products called the same thing and I was testing the output on the wrong product… der ๐
Thanks for your help anyway ๐
a quick one, adding ‘die’ at the end of the debug stuff, what does that do exactly (and should I be adding that to all add_action functions in the future)?
Hi @QueenEve
You can remove the if
statement surrounding the the_field function, ACF api will be available within this action.
It is most likely that ACF doesn’t haeva value for the field ‘test_field’, or ACF doesn’t know which post to load from.
You can do some testing like so:
<?php
echo '<pre>';
print_r(get_the_ID());
echo '</pre>';
echo '<pre>';
print_r(get_field('test_field'));
echo '</pre>';
die; ?>
Please report the output
Thanks
E
Hi @augusto
When any AJAX request is sent, you can view the response data like in the attachment bellow. It is important to see what the relationship field is returning.
This will allow you to determine if the issue is in the PHP / JSON, or if the problem lies somewhere else such as JS.
Thanks
E
Hi @mccawphoto
When you say But that made everything below the navigation disappear.
do you mean that the page broke? This is most likely a PHP error.
You can debug this error by turning on DEBUG_MODE in your wp-config.php
file.
You are posting too much code above for me to see any potential issues. Please reduce your code to the bare skeleton to determine why any errors are happening.
Use a simple debugging technique like:
<?php
echo '<pre>';
print_r($variable);
echo '</pre>';
die; ?>
on each line to view variable data and see visually that your code made it to that line without breaking.
Thanks
E
Hi @Vasili
Can you please elaborate more on the question? I don’t understand what you have and what you are trying to do.
Perhaps some screenshots to show the data structure, and a better explanation of what you mean by:
The thing is that iโm also trying to show the same posts, added with relationship field, inside a sidebar widget. As you understand this have no affect,
Thanks
E
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.