How to Display Full Post or Just Excerpt Using Custom Fields in WordPress

Home » Snippets » How to Display Full Post or Just Excerpt Using Custom Fields in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re a WordPress user looking to customize the way your posts are displayed on your website, you may have come across the option to display either the full post or just an excerpt. While WordPress provides default settings to control this, sometimes you may want more fine-grained control over how your posts are displayed. This is where custom fields come in handy. In this article, we’ll explore how to use custom fields to determine whether to display the full post or just an excerpt on your WordPress website.

					function wpturbo_display_post_content($content) {
    global $post;
    $display_full_post = get_post_meta($post->ID, 'wpturbo_display_full_post', true);

    if ($display_full_post) {
        return $content;
    } else {
        $excerpt = strip_tags(get_the_excerpt());
        return $excerpt;
    }
}
add_filter('the_content', 'wpturbo_display_post_content');
				

The code snippet provided is used to display either the full content of a post or just the excerpt based on a custom field value. It creates a function called wpturbo_display_post_content() which will be responsible for determining whether to display the full post or just the excerpt.

Inside the function, we first use the global $post statement to access the current post object. This will allow us to retrieve the value of the custom field associated with the post.

Next, we use the get_post_meta() function to retrieve the value of the custom field named wpturbo_display_full_post. The get_post_meta() function takes three parameters – the post ID, the custom field name, and a boolean value indicating whether to return a single value or an array. In this case, we are retrieving a single value by setting the third parameter to true.

We then use an if-else statement to determine whether to display the full post content or just the excerpt. If the value of the custom field is true (meaning the post should be displayed in full), we simply return the original content using the return $content statement.

If the value of the custom field is false or empty (meaning the post should be displayed as an excerpt), we use the get_the_excerpt() function to get the excerpt of the post. The get_the_excerpt() function retrieves the post’s excerpt and filters it to remove any HTML tags that might be present. We then use the strip_tags() function to remove any remaining HTML tags.

Finally, we use the return $excerpt statement to return the excerpt instead of the full content.

To apply this functionality, we need to hook the wpturbo_display_post_content() function into the the_content filter using the add_filter() function. By doing this, the function will be applied whenever the the_content() function is called, allowing us to modify the content accordingly.

By using this code snippet, you can easily control whether to display the full content or just the excerpt of a post based on a custom field value. This can be useful in situations where you want to provide more flexibility in how your posts are displayed on your website.

Register an account to save your snippets or go Pro to get more features.