0

I'm making a website which has a page that contains all members. So till now I've made a custom post type using the following code:

function midas_team_post() {
  register_post_type( 'midas-team',
    array(
      'labels' => array(
          'name' => 'تیم میداس',
          'singular_name' => 'تیم میداس',
          'add_new' => 'افزودن ورزشکار',
          'add_new_item' => 'افزودن ورزشکار',
          'edit_item' => 'ویرایش ورزشکار',
      ),
      'rewrite' => array ( 'slug' => __( 'members' ) ) ,
      'public' => true,
      'has_archive' => false,
      'show_ui' => true,
      'menu_icon' => 'dashicons-groups',
      'supports'  => array( 'title', 'thumbnail' )
    )
  );
}
add_action( 'init', 'midas_team_post' );

and I've made a template file for it which is called single-midas-team.php and all posts in it have a URL like this: http://localhost/members/{posts}.

So I want to make option page (let's call it all-members) which is under this custom post type and I'm trying to figure out how to make a different template file for 'all-members' option page and the URL would be something like this: http://localhost/members/all/

and I using this code to add option page for my custom post type:

if( function_exists('acf_add_options_page') ) {

    acf_add_options_sub_page(array(
        'page_title'     => 'تمام اعضا',
        'menu_title'    => 'تمام اعضا',
        'parent_slug'    => 'edit.php?post_type=midas-team',
        'menu_slug' => 'all',
    ));

}

*also I'm using ACF Pro.

1 Answer 1

0

In order to tackle this, there are few ways in which it could be achieved.

Option One

Using the CPT on it's own.

When you create a Custom Post Type it comes with the ability to have an archive page. This archive page which is generated by WP automatically pulls in the posts, provided that the standard WP loop is defined in the template. (See this image in order to understand how the template heirarchy in themes works).

For example:

1) Create a new PHP file called archive-cpt-name-here.php

2) Place all HTML, CSS etc for this to display in this file.

3) Then navigate to http://localhost/members/ to see the new template in effect

You can write code to access the CPT in a WP Query and customise the way you want it to display. Take a look at the codex located here in order to work out how to achieve a custom loop. (Note: The custom loop must be created on the archive template to work correctly). Archive templates work similar to page templates in the fact they both require a WP Loop to run.

Option Two

Use the ACF plugin to create an option page.

ACF Option pages aren't tied into Custom Post Types. They are deisgned to be an easy way to store custom fields in the backend of WP. They are easily accesible by passing the string of options into the function. E.g the_sub_field('field_name', 'options');

For example:

1) Create a new options page using the following:

if( function_exists('acf_add_options_page') ) {

acf_add_options_page(array(
    'page_title'    => 'Theme General Settings',
    'menu_title'    => 'Theme Settings',
    'menu_slug'     => 'theme-general-settings',
    'capability'    => 'edit_posts',
    'redirect'      => false
));

}

2) Add custom fields to the options page by creating them in the Custom Fields area of WP.

3) Assign the new fields to the options page for them to appear in the WP Backend.

4) Fetch the data on a page template using the_field('field_name', 'options');

A simple setup will include a repeater field, along with sub-fields to store info about the members.

Try looking at the ACF Documentation which can be found here in order to work out how to set this up.

Hope this helps you figure out a way to get this all set up correctly.

3
  • Thx for answering. I guess I would go with option two. But the things is that I don't want to make archive for my custom post type. What I'm trying to say is that there is a sub menu (which is an option page) under custom post type and it would have its own fields and a custom template and a URL like http://localhost/members/{option-slug}/... The problem is that I don't know how to maka a custom template for option page and givin it a URL to show it.
    – arata
    Commented Apr 11, 2018 at 20:57
  • The sub-menus under the custom post type aren't option pages controlled by ACF. In other words you can't create a new options page with ACF and have it appear in the menu like that (as far as I am aware). Generally speaking, /members will be the page that contains all members to display rather than having a seperate page for them. Perhaps you may need to look at Taxonomys to create the structure you want for your CPT.
    – EBennett
    Commented Apr 11, 2018 at 22:05
  • What about a custon post that is inside another custom post?
    – arata
    Commented Apr 12, 2018 at 5:40

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.