Question

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 );
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top