Home › Forums › Front-end Issues › 3 Show fields from advancedcustomfields-plugin in single post template
I am using the CPT UI plugin and the advancedcustomfields plugin.
First, I generated the Post Type “Matratze” with the CPT UI plugin. Here is the export code out of this plugin:
{"matratze":{"name":"matratze","label":"Matratzen","singular_label":"Matratze","description":"","public":"true","show_ui":"true","show_in_nav_menus":"true","show_in_rest":"false","rest_base":"","has_archive":"false","has_archive_string":"","exclude_from_search":"false","capability_type":"post","hierarchical":"false","rewrite":"true","rewrite_slug":"","rewrite_withfront":"true","query_var":"true","query_var_slug":"","menu_position":"","show_in_menu":"true","show_in_menu_string":"","menu_icon":"","supports":["title","editor","thumbnail","excerpt","trackbacks","custom-fields","comments","revisions","author","page-attributes","post-formats"],"taxonomies":["category","post_tag"],"labels":{"menu_name":"","all_items":"","add_new":"","add_new_item":"","edit_item":"","new_item":"","view_item":"","search_items":"","not_found":"","not_found_in_trash":"","parent":"","featured_image":"","set_featured_image":"","remove_featured_image":"","use_featured_image":"","archives":"","insert_into_item":"","uploaded_to_this_item":"","filter_items_list":"","items_list_navigation":"","items_list":""},"custom_supports":""}}
Further, I generated three custom fields with the advancedcustomfields plugin. As followed you can find the export codes:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_5759b908ddfb5',
'title' => 'Test',
'fields' => array (
array (
'key' => 'field_5759b93fd444a',
'label' => 'Test',
'name' => 'test',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
array (
'key' => 'field_5759ba77d444b',
'label' => 'Test 2',
'name' => 'test_2',
'type' => 'textarea',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'rows' => '',
'new_lines' => 'wpautop',
'readonly' => 0,
'disabled' => 0,
),
array (
'key' => 'field_5759ba90d444c',
'label' => 'Lol',
'name' => 'lol',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'min' => '',
'max' => '',
'step' => '',
'readonly' => 0,
'disabled' => 0,
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'matratze',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
I finally, created a child theme for the standard twenty sixteen theme:
My single-matratzen.pho file (I already tried the file name single-matratze) looks like the following:
Test Test Test Test Test 12345
<?php if(have_posts()) : while(have_posts()) : the_post();
/* The following code displays the Advanced Custom Fields meta boxes */
echo '<div class="intro-tekst"> ' . get_field('test') . ' </div>';
echo '<div class="images"> ' . get_field('test_2') . ' </div>';
echo '<div class="trailer"> ' . get_field('lol') . ' </div>';
endwhile; endif;
?>
I would like to display the filled advanced custom fields within the post type of matratzen.
Any suggestions what I am doing wrong?
Your post type name is “matratze”
The file name of your template should be “single-matratze.php” for the single post and “archive-matratze.php” for the archive page.
The topic ‘3 Show fields from advancedcustomfields-plugin in single post template’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.