Wordpress ACF display relationship issue - php

I'm having trouble finding documentation for displaying the relationship fields between two custom post types.
Essentially I have a stores post type that holds store names and images. Then I have a product post type where each product will have fields where you can select what stores it is available at and a section for a link to the product within that store.
Setup:
I have two custom post types 1: Stores, 2: Products
The fields for "products" are
[repeater] product_stores
[sub field] store_name (RELATIONSHIP)
[sub field] store_link (URL)
The fields for "stores" are
title (WP DEFAULT TITLE)
image (IMAGE)
I'm able to display the custom field type for the retailer link but having trouble pulling the retailer name and image into the page.
What I have so far
<?php
if( have_rows('product_stores') ): ?>
<?php while( have_rows('product_stores') ): the_row(); ?>
<?php the_sub_field('store_link'); ?>
<?php endwhile; ?>
<?php endif; ?>

You don't need to use a Repeater field with a Relationship field inside it - just use a single Relationship field. The field will return an array of post objects which you can pull the title and link from.
<?php
$stores = get_field('product_stores'); // your Relationship field
if( $stores ) {
foreach( $stores as $post) {
setup_postdata($post);
the_title();
the_permalink(); // pull whatever you need from the post.
}
wp_reset_postdata();
}
?>

Related

Wordpress ACF Repeater sub fields

I am having an issue in Wordpress with ACF Repeater sub-fields link object not returning href on front-end. Basically the link in the front-end does not output any links set in WP admin. Text output is working.
Repeater fields:
Field label = Link to doc
Field name = link_to_doc
Field type = Repeater
Repeater sub-fields 1:
Field label = Link
Field name = link
Field type = Link
Repeater sub-fields 2:
Field label = Text of link
Field name = text_of_link
Field type = Text
PHP code:
<?php if ($section['link_to_doc']) : ?>
<?php foreach ($section['link_to_doc'] as $link) : ?>
<div>
<a href="<?= $link['link'] ?>" class="btn-txt">
<?= $link['text_of_link'] ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
Could anyone check the php code and let me know what's wrong?
This topic has no answer for a while, you've probably found a solution. But now ACF is explaining how to access the data for nested repeaters directly in the documentation.
<?php
// Loop over the first repeater
if( have_rows('type') ):
while( have_rows('type') ) : the_row();
// Loop over sub repeater rows.
if( have_rows('type') ):
while( have_rows('type') ) : the_row();
// Get sub value.
$child_title = get_sub_field('name');
endwhile;
endif;
endwhile;
endif;
There is also other ways to access this kind of data, here's the list:
https://www.advancedcustomfields.com/resources/repeater/

How to get parent post fields in child pages? WP Types

I made two custom post types with WP types plugin
Property (child)
Agent (Parent)
In Agent(Parent), i have created custom post fields with WP types Plugin:
Email
Phone
Fax No.
Now I want to show the fields in Property(Child) Posts.
I am using this code in single-property.php.
$parent_id = wpcf_pr_post_get_belongs($post->ID, 'agent');
$parent = get_post($parent_id);
$agentPhoto = wp_get_attachment_image_src(get_post_thumbnail_id($parent->ID), 'agentPhoto');
//successfully get the parent photo
<img src="<?php echo $agentPhoto[0];?>" alt="<?php echo $parent->post_title;?>">
//successfully get the title of the parent
<?php echo $parent->post_title;?>
I am able to get the Title and Featured Image of Agent(Parent) but I can't get the custom fields which is (1. EMail, 2. Phone, and 3. Fax No.).
Below is the code for custom field through which i can get the fields but only in assign post types which is Agent(Parent).
$agentPhone = types_render_field( "agent-phone" );
$agentEmail = types_render_field( "agent-email" );
$agentFax = types_render_field( "agent-fax" );
Any help appreciated thanks.
use this sortcode to get value of agentPhone , email etc...:and in place of id pass the parent post id:
<?php echo(do_shortcode('[types field="adresse" id="746" arg1="val1" arg2="val2"]')); ?>
<?php echo(do_shortcode('[types field="agent-email" id="746" arg1="val1" arg2="val2"]')); ?>
<?php echo(do_shortcode('[types field="agent-fax" id="746" arg1="val1" arg2="val2"]')); ?>

Retrieve a post with its ACF repeater fields in wordpress

In a wordpress theme I am programming, I created a custom post type and its template. This custom post type displays a list thanks to advance custom fields that I attached to the post template. In fact, I used the "repeater" field of Advance custom fields pro. The user only has to insert items of the list when editing the post.
I am trying to do the following: I want to display in two specific pages templates the post with all its custom fields (the list created through the repeater). I am not able to do that, I only can retrieve the "normal" fields of the post (the title, the content...). I created a snippet so you can look at my code.
<?php //the single of the post: ?>
<ul class='slider-partners'>
<?php
//slider partners
if( have_rows('slider_partenaires_hp') ): //"slider_partenaires_hp" is the repeater field
// loop through the rows of data
while ( have_rows('slider_partenaires_hp') ) : the_row();
// display a sub field value
echo "<li class='partenaire-slide'><img src='" . get_sub_field('logo_partner') . "'></li>"; //"logo_partner" is the item inside the repeater field
endwhile;
else :
// no rows found
endif;
?>
</ul>
<?php //the template of the page where I try to retrieve the above post:
$the_query = new WP_Query(array(
'post_type' => 'our-partners',
'posts_per_page' => 1,
'order' => 'DESC'
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
endwhile;
wp_reset_postdata();
?>
Could you please help me in calling a post with all its custom fields in two diferent pages ?
Thanks

Hide a custom input element if category

I'm using the Custom Field Plugin to add a custom Field and call it to the content using the_field(); in my single.php
I just want to display this field on the all the categories except the "Articles which is 13", so I was trying somethin' like this:
<?php if !is_category( '13' ); { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
But it is not working. I'm wondering how to do this.
You should use in_category, and put the conditional statement in parentheses.
<?php if (!in_category( $cat )) { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
Make sure $cat is either ID (integer), name or slug (string) of the category.
Edit: I'm assuming you actually want to display the input on posts that are not in a particular category, hence in_category. If you do indeed want to display it on all other Category archive pages, then is_category would be correct.

Pods - display link to relationship post

I'm using Pods to set up Custom Post Types in WordPress.
I have two custom post types: Car and Garage. In Car I have set up a relationship field with Garage, so when editing a Car post I can select one of the Garage posts available from the drop-down.
When viewing a Car post, I would like to display the name and the link to the garage post selected. How would I do that?
Below is an example from http://pods.io/tutorials/get-values-from-a-custom-relationship-field
//get Pods object for current post
$pod = pods( 'pod_name', get_the_id() );
//get the value for the relationship field
$related = $pod->field( 'relationship_field' );
//loop through related field, creating links to their own pages
//only if there is anything to loop through
if ( ! empty( $related ) ) {
foreach ( $related as $rel ) {
//get id for related post and put in ID
//for advanced content types use $id = $rel[ 'id' ];
$id = $rel[ 'ID' ];
//show the related post name as link
echo ''.get_the_title( $id ).'';
//get the value for some_field in related post and echo it
} //end of foreach
} //endif ! empty ( $related )
Instead of a permalink to a Garage post, I'm getting several permalinks to the Car post I'm viewing.
Any Ideas?

Categories