0

I'm using Advanced Custom Fields and I'm trying to show a custom field below the single post. I managed to get the code to render below the post, but when I set the field movie_actors and save, the entire page goes blank after I refresh it, it doesn't show any errors on the screen although I already have debug mode enabled. My field is set as User Object inside ACF, and the field type is user. What I'm trying to do is to show a link to the user profile in Ultimate Member. I also tried to show a link to the default wordpress user profile just in case, and it also gives me the same result.

I'm doing this inside my custom functions plugin (equivalent to functions.php).

I'm going to be leaving both codes here just in case:

This one is for Ultimate Member plugin:

function wpb_after_post_content($content){ if (is_single()) { 
global $post;

$content .= '<p>CAST</p>' .

'<div id="meta-coauthor"><span class="metacoauteur">' .

        $values = get_field('movie_actors', $post->ID); 
        if($values) { 
            foreach($values as $value)  {
                $author_ID = $value['ID'];
                                um_fetch_user( $author_ID) . '<a href="'.um_user_profile_url( ).'">' . um_user("display_name") . '</a>';
            }   
        }; 
    '   </span>
</div>';

 }
    return $content;
}
add_filter( "the_content", "wpb_after_post_content", 9999 );
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 100, 50, true );

And this one is for the default Wordpress user profile:

function wpb_after_post_content($content){ if (is_single()) { 
global $post;

$content .= '<p>CAST</p>' .

 '<div id="meta-coauthor"><span class="metacoauteur">' .

        $values = get_field('movie_actors', $post->ID); 
        if($values) { 
            foreach($values as $value)  {
                $author_ID = $value['ID'];
                $author_url = esc_url( get_author_posts_url($author_ID) ) . ' <a href="'.$author_url.'">' . $value['display_name'] . '</a>';
              
            }   
        }; 
    '   </span>
</div>';


 }
    return $content;
}
add_filter( "the_content", "wpb_after_post_content", 9999 );
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 100, 50, true );
1

I believe that your syntax is wrong in the variables you set up in your foreach loop. Because you're referencing an object and not an array, you need to use this format:

$author_ID = $value->ID

and...

$value->display_name

We also discovered that the foreach loop isn't necessary, so I'm removing it from the answer.

The other issue is how you're passing the content. Maybe it's my lack of experience, but I find it easier to refer to "$content" more often and attach more to that variable as I go. You are trying to connect it throughout the argument, and while it makes it more confusing for me, it also doesn't work (that's what's causing the blank page).

Try this:

function wpb_after_post_content($content){ if (is_single()) {
    global $post;

    $content .= '<p>CAST</p>' .

        '<div id="meta-coauthor"><span class="metacoauteur">';

        $actor_obj = get_field('movie_actors', $post->ID);

    echo '<pre>';
    var_dump($actor_obj);
    echo '</pre>';

    if($actor_obj) {
            $author_ID = $actor_obj->ID;
            $author_url = esc_url( get_author_posts_url($author_ID) );
            $content .= ' <a href="'.$author_url.'">' . $actor_obj->display_name . '</a>';
    };
    $content .='   </span>
</div>';


}
    return $content;
}
add_filter( "the_content", "wpb_after_post_content", 9999 );
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 100, 50, true );

Let me know if that works for you.

I don't have Ultimate Member so can't test that code, but if you get it working in the default WordPress screen, you're not far.

6
  • I tried your code. It's working, but it's also returning these errors: Notice: Trying to get property 'ID' of non-object in .php on line 523 Notice: Trying to get property 'display_name' of non-object in .php on line 525 – 023023 Apr 28 at 5:36
  • @023023 I believe this is because we don't actually need to be looping through the object like it's an array. Let me edit the answer to remove the foreach loop entirely and show you how to get the result without the loop. – Steck Insights Web Design Apr 28 at 12:09
  • @023023, did that fix the issue for you? – Steck Insights Web Design Apr 28 at 16:05
  • Sorry, I'm still testing, I'll get back to you ASAP. – 023023 Apr 28 at 16:55
  • Everything seems to be fine. – 023023 Apr 28 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.