2

I want to create some custom fields that will be able to show to a default wordpress post. I don't want to use the ACF plugin. Is there anyway to do it programmatically?

Thanks

2 Answers 2

2

You can use the custom fields included in wordpress

Add a new post and do the following steps:

1. Go to the Options page

select options page

2. Select "custom fields" and hit the reload button

select the custom field option and reload

3. you now have a custom field in your post edit page at the bottom.

custom field

now you can use this custom field in your theme inside the post-loop

<?php while ( have_posts() ) : the post(); ?>

    <?php echo get_post_meta($post->ID, 'featured', true); ?>

<?php endwhile; ?>

You have to replace 'featured' with the name of your custom field.

Once you created a custom field, you can use it in your others post as well.

Hope this is helpful.

2
0

This article walks you through creating a very basic, lightweight plugin to add your own meta fields. I'm sure it could be adapted to be used as a standalone function rather than a plugin.

https://metabox.io/how-to-create-custom-meta-boxes-custom-fields-in-wordpress/

Essentially you add your fields using add_meta_box() and setting the post type to post or whatever CPT you like. Update the fields using update_post_meta(). Then display wherever you like using get_post_meta().

add_meta_box

update_post_meta

get_post_meta

2
  • 1
    Answers need to be self contained, you can link offsite for supporting material e.g. further reading, but the link can't be the answer itself
    – Tom J Nowell
    Commented Nov 23, 2020 at 17:04
  • Noted, thank you. Commented Nov 23, 2020 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.