ACF relationship field by user - php

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

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.

WordPress Advanced Custom Fields Getting field data from different post

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?

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; ?>

Wordpress: Check posts have comments allowed

I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks

Categories