WordPress Advanced Custom Fields Getting field data from different post - php

So I am having an issue with the following code:
<?php
$posts = get_field('story_character');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
By <?php the_title(); ?>
<span><?php the_field('story_character'); ?></span>
<?php endforeach;wp_reset_postdata(); ?>
<?php endif; ?>
<?php
$posts = get_field('story_character');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
of <?php the_field('char_tribe'); ?>
<span><?php the_field('story_character'); ?></span>
<?php endforeach; ?>
<?php endif; ?>
Everything after the first query does not show up and I cannot figure out why...What am I missing?

Related

Nested post object with Advanced Custom Field

I have a movie list to display in a page. Each movie is a post-object (which I repeat with an ACF repeater).
But in these movies, there is another post-object for the authors.
I can not see the name of the author. Can you help me ?
Here is my code
<?php while ( have_rows('sc_movies') ) : the_row(); ?>
<?php $post_object = get_sub_field('sc_movies_movie'); ?>
<?php if($post_object): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<article class="movie">
<div class="movie__content">
<h3 class="movie__title"><?= the_title(); ?></h3>
<?php $post_object = get_field('film_author'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<span class="movie__director">Par <?= the_title() ;?> </span>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</article>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
please make sure if your "sc_movies" repeater has "sc_movies_movie" post object with "Select multiple values? = false " and also in movie post "film_author" post object is "Select multiple values? = false" then your code is correct. If you still gettting issue try add "wp_reset_query()" before while loop because may be some other post object is conflict in page.

ACF relationship field by user

Trying to query posts were the current user is set in a user relationship field. Thought it would be easy but I am totally stuck.
I have tried this:
<?php
$user = new WP_User(get_current_user_id());
$posts = get_field('consultora', 'user_'.$user->ID);
if( $posts ):
?>
<div class="assistente-foto">
<?php foreach( $posts as $post):?>
<?php setup_postdata($post); ?>
<?php echo the_post_thumbnail();?>
<p><strong><?php echo get_the_title(); ?></strong></p>
<p>t. <?php the_field('telefone'); ?></p>
<p>e. <?php the_field('email'); ?></p>
<?php endforeach; ?>
</div>
<?php wp_reset_postdata();?>
<?php endif; ?>
caption

Wordpress relationship loop in <head>

I have a page which echos about 10 items. I would like them all to have a different background image on hover.
I'm guessing I would need to have my loop in my <head> tag so that I can apply a background image.
Here is my loop:
<?php
// get background images for the services page
$post = get_field('related_services');
if( $posts ): ?>
<style>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
.sector-box[style]::after {
background-image: url("<?php echo get_template_directory_uri(); ?>/images/creative-bg-hover.jpg");
color:#<?php the_field( 'colour' ); ?>;
}
<?php endforeach; ?>
</style>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I can't get it to echo a style for each..
Would I also need to apply an ID to each box? So that it knows which box to apply the background image to?
here is my html:
<?php
$posts = get_field('related_services');
if( $posts ): ?>
<div class="container">
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="sector-box reveal" style="background-color:<?php the_field( 'colour' ); ?>;">
<div class="sector-title">
<?php the_title( '' ); ?>
</div>
</div>
<!--Close Single Service box-->
<?php endforeach; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<!--Close Service Sector Boxes-->
Use the following:
<?php echo get_permalink($post->ID); ?>
<?php echo get_the_title( $post->ID ); ?>
<?php echo get_field('colour', $post->ID ); ?>
and remove function
<?php setup_postdata($post); ?>
<?php wp_reset_postdata(); ?>

Advanced Custom Fields - Relationship Field

I am using the relationship field within a custom post type, but my content is not showing up. I have doubled checked against the documentation but I cant see why it wouldn't be working.
Any help would be great!
Documentation here: http://www.advancedcustomfields.com/resources/relationship/
My html is:
<?php
// The Query
$args = array(
'post_type' => 'top-car'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$posts = get_field('top-cars');
if( $posts ): ?>
<ul id="recom">
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<div class="recom-single">
<h2><?php the_field( 'model' ); ?></h2>
</div>
<!--Close Recom Single-->
</li>
<!--Close First Car-->
<?php endforeach; ?>
</ul>
<!--Close Slider-->
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, we have no cars available at this time, please contact us.'); ?></p>
<?php endif; ?>

What's wrong with my relationship loop?

I'm having a problem with a relationship loop with Advanced Custom Fields in Wordpress.
The first post is shown perfect, but it should execute one more post, but it doesn't.
Can anyone see what's wrong with my code?
<?php $posts = get_field('produkter'); if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="produkt">
<?php the_title(); ?>
<?php the_content(); ?>
<?php $posts = get_field('fil'); if( $posts ): ?>
<div id="filer">
<div id="topsection" class="laddahem"><div class="topborder"></div>
<h2>Ladda hem</h2>
</div><!-- #top -->
<div class="filhuvud">
<div class="filtyp">Filtyp</div>
<div class="fildatum">Datum</div>
<div class="filstorlek">Filstorlek</div>
</div><!-- .filhuvud -->
<div class="filholder">
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<?php $attachment_id = get_field('filen');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
// hämta filstorleken
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize, 1);
$filetype = strtolower(pathinfo($url, PATHINFO_EXTENSION)); ?>
<div class="fil <?php echo $filetype; ?>">
<div class="filtyp"><a target="_blank" href="<?php echo $url; ?>" ><?php the_title(); ?></a></div>
<div class="fildatum"><?php the_time('Y-m-d'); ?></div>
<div class="filstorlek"><?php echo $filesize; ?></div>
</div><!-- .fil -->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div><!-- .filholder -->
</div><!-- #filer -->
<?php endif; ?>
</div><!-- .produkt -->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Your issue is that you're overwriting the $posts variable within the loop, after you've defined it outside of the loop.
The solution is to rename your second $posts variable to something else, so that your overarching loops look like this:
<?php $posts = get_field('produkter'); ?>
<?php if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php $files = get_field('fil'); //RENAMED THIS VARIABLE?>
<?php if( $files ): ?>
<?php foreach( $files as $file): ?>
<?php //Main body of markup and other fun stuff ?>
<?php endforeach; //end $files foreach ?>
<?php endif; //end $files if ?>
<?php endforeach; //end $posts foreach ?>
<?php endif; //end $posts if ?>
The solution was to change the other foreach loop to this:
<?php $files = get_field('fil'); if( $files ): ?>
<?php foreach( $files as $post): ?>
<?php setup_postdata($post); ?>
That made it work exactly how I wanted it.

Categories