ACF Relationship PHP Wordrpess

86 views Asked by At

I am trying to create a custom author block on my wordpress website. The website is build on elementor. With the use of ACF I created a relationship (our_people_author) between the blog posts and custom post type (our people) - which contains posts presenting people.

I would like to add this code to functions.php or turn it into a shortcode but I am getting an error. Would anyone be able to help me with it?

$authors = get_field('our_people_author');
<?php foreach($authors as $author):?>

    <img src="<?php echo get_the_post_thumbnail_url($author->ID, 'thumbnail');?>">
<h3>
    <a href="<?php echo get_page_link($author->ID);?>">
        <?php echo $author->post_title;?>
    </a>
</h3>

<?php endforeach;?>
2

There are 2 answers

0
Parthavi Patel On
    <?php 

    $authors = get_field( 'our_people_author );
    foreach( $authors as $author ):
          setup_postdata( $author );

          // Your data here

        wp_reset_postdata();

    endforeach;

You are missing this setup_postdata( $author );

0
Rizwan Ahmad On

Assuming that you are using the shortcode on the same page/post/custom_post where you have the custom field "our_people_author" saved.

Just add the following code in your function file. Make sure to replace YOUR_SHORTCODE_NAME with your shortcode name.

add_shortode("YOUR_SHORTCODE_NAME",function(){
    if(is_admin()){
        return;
    }
    ob_start();
    $authors = get_field('our_people_author');
    foreach($authors as $author):
        // code here

    endforeach; 
    return ob_get_clean(); 
});

Also I have noticed that you have added <?php tag in wrong place in your code line no 2.