Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress adding custom fields to page without plugin

Tags:

wordpress

I'm pretty new to WordPress and building a site that is using a custom template. The template includes a large banner at the top which is uploaded via the page's "featured image".

I would like to add the page title on the top of this image(which is not an issue) but be able to specify if the title is positioned left or right(maybe a dropdown menu selection) when creating the page.

I've seen many suggestions for similar functionality using the plugin Advanced Custom Fields, but I'm unable to find a solution that doesn't involve an additional plugin.

Is there a reason why I can't just add in the functionality I need? Adding a plugin just seems a little overkill to me for such a small feature.

UPDATE:
Ok, after some poking around I found that there's a "custom fields" option that was not checked in the "screen options". My understanding is that these fields work as key value pairs that can be accessed in the template using get_post_meta($post_id, $key, $single); or a more specific example get_post_meta($post->id, 'my_dropdown_key', true);. I will test it out and report results.

like image 301
MastaWymes Avatar asked Oct 19 '22 02:10

MastaWymes


2 Answers

Yes you can use the custom fields by checking from the top bar of the WordPress page admin section. However, this is not a good solution as it will likely be confusing after you have multiple custom fields which is sure.

You can use the custom meta box function for this and add the functionality to your page which is easier done through a plugin called advanced custom fields which you already seem to know.

So, if you don't want to use the plugin instead you want to do it custom then there is a function callend add_meta_box. This function allows you to add a seperate box on a specific post_type.

For Quick Tutorial: https://www.sitepoint.com/adding-custom-meta-boxes-to-wordpress/

Hope this gives an overview on meta boxes. Thanx.

like image 67
DpEN Avatar answered Jan 04 '23 05:01

DpEN


For those also learning how to do this, you must enable "custom fields" from the "screen options" in the top right corner of the admin screen. You can then create fields based on key value pairs and retrieve them using the get_post_meta($post_id, $key, $single) function.

For example, you can create a new custom field with the key "blue_text" and value " 'color: blue;' ". You can then access this value and apply the style in the markup like so:

<h1 style=<?php echo get_post_meta(get_the_ID(), 'blue_text', true); ?>> 
        This text is blue! 
</h1>

This obviously is not the dropdown that I had originally described, but it is a simple way to add styles to a specific page in a template using custom fields with no additional plugin, so this works for me.

UPDATE:
The accepted answer using meta boxes is a much better solution and should be considered before this route.

like image 20
MastaWymes Avatar answered Jan 04 '23 06:01

MastaWymes