How to Show Custom Field Value as Excerpt in WordPress (And Some Intro)

in #wordpress6 years ago

Hi guys, you may know from my past posts that I'm a mountain biker. That's just the tip of the zoidberg though.

I'm also a software engineer. I have been developing software for the past 18 years of my life. I've become pretty good at it and it's my main source of income.

I discovered Discord after I joined STEEMit and noticed that its servers are great communities (so much better than slack's anyway).

One of the technologies that I'm good at is WordPress so I joined an unofficial server for WordPress support.

In there, I noticed that most of the chatter is some guy asking for help to some trivial (for me at least) problem. It's really simple. So I decided to make it a hobby and help other people. I've helped out a few already and some of them offered me jobs or projects but that's a different story.

Suddenly an idea hit me. One of my problems is that I'm not naturally creative nor a good writer. The idea is to help people out on their software development problems and post the solutions here on STEEMit.

Two ducks with one rubber band!

I'll call this series of posts... well, I haven't thought of it yet but if you have a suggestion, let's hear it.

So, here goes.

The Scenario

A guy had a blog with a lot of posts where each of the posts has a custom field with data in it. The data could be used as an excerpt or a short version of the blog post itself.

The Problem

WordPress automatically calculates the excerpt as the first few words of the blog post content. Alternatively, you can enable the Excerpt entry box from the Screen Options pull-down sheet and manually type the excerpt there, and WordPress will automatically use that.

The problem is that transferring the data from the custom field to the excerpt box would take some time since he had lots of blog posts. He also wants this custom field data to show as the excerpt only on search screens.

The Solution

Simple, let's just use WordPress' awesome filters feature. I never knew the name of the custom field so let's just call it MYCUSTOMFIELD.

With a little digging and messing around (hopefully not too messed up though) with your theme's code, this can be done with the following code being inserted at the end of the functions.php file.

add_filter( 'the_excerpt', function ($original_excerpt) {
    if ( !is_search() ) return $original_excerpt;
    return get_post_meta(get_the_ID(), 'MYCUSTOMFIELD', true);
} );

That should do it!

Code Explanation/Discussion

I'd like to believe that my code is self-explanatory if you're in between being a beginner and intermediate. But if you're more on the beginner side or there's something tricky, please don't hesitate to ask questions.

If you need help with software development, just drop a comment or send some SBD/STEEM with your concern in the memo like how those bots do it.

I'm also available for hire if you have software related projects.

Sort:  

Welcome to Steemit, @japalekhin! Wish you make many new friends here on this platform :)

Thanks! I already have, I'm not really that new to STEEMit, I'm just re-introducing myself.

Wonderful. Now I know where to go to for software related projects :)

No problem, just comment on any of my posts if you're having trouble with development or setting up a website, those things.

I will. Thank you!


Welcome to Steemit @japalekhin!

I wish you much success and hope you find Steemit to be as rewarding and informative as I have.

Here are some links you might find useful.
Your stats on SteemNow
Your stats on SteemWorld
Your stats on SteemD
How does Steemit actually work?

Introbot is hosted and managed with donations from @byColeman to help make your journey on Steemit be truly rewarding. Your feedback is always welcome so that we may improve this welcome message.
Oh yea, I have upvoted you and followed you. Many blessings from @introbot & @bycoleman

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly & Featured Posts are voted every 2.4hrs
Join the Curation Team Here | Vote Resteemable for Witness

Coin Marketplace

STEEM 0.35
TRX 0.12
JST 0.040
BTC 70351.33
ETH 3563.43
USDT 1.00
SBD 4.72