0

I am creating a field that needs to add a custom field to every post on a site. I am using the ACF plugin in my plugin to do so. I have followed this tutorial and am close. In the tutorial they add the custom fields to a settings back, but I want to add the custom field to every post. Here is the ACF-related code that I have:

<?php
// 1. customize ACF path
add_filter('acf/settings/path', 'my_acf_settings_path');
function my_acf_settings_path( $path ) {
    // update path
    $path = get_stylesheet_directory() . '/vendor/advanced-custom-fields/';
    // return
    return $path;
}

// 2. customize ACF dir
add_filter('acf/settings/dir', 'my_acf_settings_dir');
function my_acf_settings_dir( $dir ) {
    // update path
    $dir = get_stylesheet_directory_uri() . '/vendor/advanced-custom-fields/';
    // return
    return $dir;
}

// 3. Hide ACF field group menu item
add_filter('acf/settings/show_admin', '__return_false');

// 4. Include ACF
include_once( get_stylesheet_directory() . '/vendor/advanced-custom-fields/acf.php' );

// 5. Setup Custom Fields on post pages
$this->setup_options();
public function setup_options() {
  if(function_exists("register_field_group")) {
    register_field_group(array (
      'id' => 'acf_field-id',
      'title' => 'Field Name',
      'fields' => array (
        array (
          'key' => 'field_5ab8f6946d890',
          'label' => 'Label',
          'name' => 'name',
          'type' => 'wysiwyg',
          'instructions' => '',
          'default_value' => '',
          'toolbar' => 'full',
          'media_upload' => 'yes',
        ),
      ),
      'location' => array (
        array (
          array (
            'param' => 'post_type',
            'operator' => '==',
            'value' => 'post',
            'order_no' => 0,
            'group_no' => 0,
          ),
        ),
      ),
      'options' => array (
        'position' => 'normal',
        'layout' => 'no_box',
        'hide_on_screen' => array (
        ),
      ),
      'menu_order' => 0,
    ));
  }
}

I know my issue is in #5 above. I am not clear how to run the function to load that field for every post. Any insight?

Also, how would I then automatically output this field code in the single.php template before the post content. Can I use a WordPress function to do that as well?

2 Answers 2

0

You can use wordpress plugin - Advanced Custom Fields to set up your desired fields. https://wordpress.org/plugins/advanced-custom-fields/

To print custom field value, please refer plugin documentation.

0

Your #5 looks fine to me. The 'location' section states that it should attach to every post_type equal to type post. That custom field 'Label' should now appear on the post edit page.

To get your field data in single.php you would then use

$data = function_exists( 'get_field' ) ? get_field( 'name', get_the_ID() ) : null;

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.