Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:24:55+00:00 2026-06-14T08:24:55+00:00

I am building a WordPress theme and am onto displaying the custom fields of

  • 0

I am building a WordPress theme and am onto displaying the custom fields of a custom post type. Instead of writing this a bunch of times:

<?php if(get_post_meta($post->ID, 'custom_item', true)){ echo $post_meta_data['custom_item'][0]; } ?>

Can’t I write a function to shorten this process? Something where I would pass it a variable and it can get plugged into the function. This is what I have so far with no luck:

<?php 
    function display_meta($custom_meta) {
    if(get_post_meta($post->ID, $custom_meta, true)){ echo $post_meta_data[$custom_meta][0]; }} 
?>

And then I want to be able to do something like this for each field:

<?php echo display_meta('custom_item') ?>

I am still relatively new to PHP and this is my first time trying to write a function like this. Any help is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:24:56+00:00Added an answer on June 14, 2026 at 8:24 am

    looks like you need to pass $post and $post_meta_data to the function as well.

    <?php 
    function display_meta($custom_meta, $post, $post_meta_data) {
        if(get_post_meta($post->ID, $custom_meta, true)){ 
            echo $post_meta_data[$custom_meta][0]; 
        }
    } 
    ?>
    

    and you’re attempting (incorrectly) to do an echo twice. Something like this would work better

    <?php display_meta('custom_item', $post, $post_meta_data) ?>
    

    You should do some simple php tutorials and learn about functions and scoping tho.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a new WordPress theme using register_post_type to create a custom post
I am building a custom wordpress theme and have a slideshow on the front
I'm building a custom WordPress theme and have a quick question regarding WordPress and
I am building a custom WordPress theme and need to add descriptions to my
I am building a child theme in Wordpress that needs some custom functionality (for
i'm building a wordpress webpage based on the Skeleton Wordpress theme. I have 2
This is fairly simple but I cant work it out. I'm building a wordpress
I am building a WordPress theme for a small startup. They want to have
I am building a wordpress theme I want to have the next and previous
i am building a wordpress site, twenty eleven theme. I want a widget area

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.