Using Custom Fields

If you want to add more data for your post or page such as Subtitle, Publish Date, Language… You can refer this article to use the WordPress feature or Advanced Custom Fields plugin to do this.

1. Using default Custom Fields of WordPress

By default, Custom Fields section is hidden so you don’t see it on post edit screen, you need to check here to show it

enable-custom-fields

How to add a new custom field

Easily, You only need to enter the Name and the value of the new fields.

custom-fields

How to display on Front End:

Assuming you added 2 fields to a post: u_subtitle and u_lang.

If you want to display all fields associated with that post, put the code below to single.php file

<?php the_meta(); ?>

It will display something like this:

post-meta

But you only want to show just one field, so you need to use get_post_meta 

As an example, you want to show the subtitle, you can use this code:

<?php 
 $u_subtitle = get_post_meta( $post->ID, 'u_subtitle', true);
 if( $u_subtitle) {
 echo 'Subtitle: ' . $u_subtitle . '</br>';
 }
?>
The result:

subtitle

It’s easy so you can add and show the extra information for your posts, pages. But If you want further, you can refer to use the plugin below instead.

2. Using Advanced Custom Fields plugin

By using this plugin, you can add the custom field to somewhere, not only post, page. Such as you can assign the “End Date” field for the U-Course…

How to add a new Custom field

Just an example, I am going to add a Course group with End Date field with the basic settings.

end-date-cf

Save it and you can see a new field “End Date” in U-Course edit screen, like this:

new-end-date-f

How to display on Front End

Besides using the native WordPress functions above, with Advanced Custom Fields, you can use their functions or shortcodes. Read details here: httpss://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

Continue the example about adding the End Date.

  • Using the functions:

After picking a date for End Date field, To show it, You need to put the code below to themes\university\u-course\single-course-meta.php, after line 33.

<?php $enddate= get_field('end_date'); ?>
  <?php if($enddate){ ?>
      <?php _e('END:','cactusthemes');?>
      <div class="course-start"><?php echo date_i18n( get_option('date_format'), strtotime($enddate));?></div>
 <?php }?>

Then, you can get the result like this:

result-acf

 

  • Using shortcode,  (To learn more about ACF shortcodes, please click here)
[acf field="field_name" post_id=""]