0

I am working on a simple user profile archive, using the theme "Atomic Blocks" as a starter theme. I am using one custom post type and several custom post fields to create these user profiles. The Custom Post type "Profile" and the plugin Advanced Custom Fields (ACF) to display different content on each "post" to look like a profile of sorts (a profile picutre, a file download, a video embed and short description).

Now I want to add more custom fields to these custom post type excerpts (see below). Mainly the profile pictures.profile archive page,

I managed to create a custom excerpt that displays the profile description ("Profilbeschreibung" in the image above) custom field with this code:

function advanced_custom_field_excerpt() {
    global $post;
    $text = get_field('profilbeschreibung');
    if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length =30; // 30 words
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }

    return apply_filters('the_excerpt', $text);
}

Since I am not that experienced with creating custom wordpress functions I am stuck. My main question would be: Is there any way to achieve displaying a custom field containing an image in each profile excerpt?

I know that excerpts have a hard time displaying html, so I am not sure.

I looked all over and found nothing for my specific case. I appreciate any insight into what I might have to do or what went wrong. Thanks!

  • First - it will work better if you add multiple custom fields. You should have one for the file download, one for the video embed, one for the description. For the image, it's likely that you can use WP's built-in Featured Image functionality. If you can't use that, you can also add a custom field for the image. Once all the data is split up, it's fairly easy to add within your function - inside your if statement, at the end, you can get the featured image (or custom field) of the current post and append it to $text. – WebElaine Feb 18 at 18:24
  • Thanks for the feedback @WebElaine! Well thats where I am stuck. I really don't know how. I normally call my custom fields on each individual post (not the excerpt) with something like $abschluss = get_field('abschluss'); and then use them inside the single post template with <?php if( $abschluss ): ?> etc. But for this I really don't know where to start. I do have a custom field for the image already used on the main posts, now I want to display that custom field in the excerpt I quoted in my question. How do I append it to $text? – festgebaeck Feb 19 at 6:58
  • Hi @WebElaine, I now managed to display images using this function. Is this a viable solution or should I be aware of any future shortcomings? – festgebaeck Feb 19 at 8:12
  • Couple of suggestions: one, instead of echoing the image, add it to text. If you want the image last, you can just change echo to $text .= or if you want the image before the text, you can say $text = (all the image stuff) . $text. – WebElaine Feb 19 at 19:24
  • Two: instead of saving the image URL you can have ACF save the image ID. This will help you get whatever particular size of image you want (instead of whatever size may have been uploaded as an original) and you'll also get the alt text (important for accessibility). You can use $foto = wp_get_attachment_image(get_field('profilfoto'), 'thumbnail') to get all the details, then add the image tag with the src and alt text. – WebElaine Feb 19 at 19:27

Your Answer

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

Browse other questions tagged or ask your own question.