7

Add fields to archive?

I'm using Advanced Custom Fields plugin (but it's not relevant as I can also use native WP fields) and want to add custom field to the custom post archive page.

The problem is there is no archive page by default, where you can attach the field (though you can add it to single categories as they have presence in the admin area).

Create custom page template = no highlight in the menu

One solution would be to create the custom page template for archives. But that way you loose the ability to highlight it in the navbar while on single post/category, because this new page won't be recognized as a parent of single.

Am I correct there is no way to add fields to archive and keep the highlight in the menu?

1 Answer 1

9

Using the Advanced Custom Fields plugin you can assign options pages to you custom posttype like this:

if( function_exists('acf_add_options_page') )
{
    acf_add_options_page(array(
        'page_title'    => 'YOUR_PAGE_TILE Options',
        'menu_title'    => 'YOUR_MENU_TITLE Options',
        'menu_slug'     => 'options_YOUR_SLUG',
        'capability'    => 'edit_posts',
        'parent_slug'   => 'edit.php?post_type=YOUR_CUSTOM_POSTTYPE_SLUG',
        'position'      => false,
        'icon_url'      => 'dashicons-images-alt2',
        'redirect'      => false,
    ));
}

That way you get an options page you can assign fields to later. Keep in mind that all the fields you asign to that option page are not bound to your custom posttype. They are like 'normal" option fields but you have them in your custom posttype section in the WP Backend menu. I think it's necessary to use the pro version of the plugin - or have the addon installed to build option pages.

4
  • Thanks. It adds the option but for some reason doesn’t display it in the actual page. Do you retrieve the value as usual with the_field()?
    – Runnick
    Jul 3, 2017 at 15:02
  • I'm working with twig templates but it should work if you do it like this: $options= get_fields('option'); and then get the value by echo $options['YOUR_FIELDNAME'];
    – Luckyfella
    Jul 3, 2017 at 15:07
  • 1
    Actually it's the_field('option_name', 'option') if anyone is interested.
    – Runnick
    Jul 3, 2017 at 17:53
  • Both ways work. Your solution saves resources of course. If you have a site with not that much option values it's just nicer to assign them all to an array in my opinion. That way you don't have to repeat writing the_field(..., 'option') all the time.
    – Luckyfella
    Jul 3, 2017 at 18:16

Your Answer

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

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