最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Load info from customposttype into template page

programmeradmin5浏览0评论

So I have a Custom Post Type called members. to this custom post type, I have ACF linked with a form to make an account In this post type are users made and stored.

Now I want to retrieve all members stored inside the post-type and display them inside a template page.

the code I have inside functions.php for the custom post type is:

function init_members() {
    $labels = array(
        'name'               => 'Members',
        'singular_name'      => 'Member',
        'menu_name'          => 'Members',
        'name_admin_bar'     => 'Member',
        'add_new'            => 'New member',
        'add_new_item'       => 'New member',
        'new_item'           => 'New member',
        'edit_item'          => 'Edit member',
        'all_items'          => 'All members',
        'search_items'       => 'search member',
        'not_found'          => 'No members found',
        'not_found_in_trash' => 'No members found in trash'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'exclude_from_search' => true,
        'rewrite' => array('slug' => 'lid'),
        'has_archive' => false,
        'supports' => array('title'),
        'show_in_rest' => true,
        'menu_icon' => 'dashicons-groups'

    );
    register_post_type('members', $args);
}
add_action('init', 'init_members');

So how do I get the member names from the custom post type with ACF to display on the template page

So I have a Custom Post Type called members. to this custom post type, I have ACF linked with a form to make an account In this post type are users made and stored.

Now I want to retrieve all members stored inside the post-type and display them inside a template page.

the code I have inside functions.php for the custom post type is:

function init_members() {
    $labels = array(
        'name'               => 'Members',
        'singular_name'      => 'Member',
        'menu_name'          => 'Members',
        'name_admin_bar'     => 'Member',
        'add_new'            => 'New member',
        'add_new_item'       => 'New member',
        'new_item'           => 'New member',
        'edit_item'          => 'Edit member',
        'all_items'          => 'All members',
        'search_items'       => 'search member',
        'not_found'          => 'No members found',
        'not_found_in_trash' => 'No members found in trash'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'exclude_from_search' => true,
        'rewrite' => array('slug' => 'lid'),
        'has_archive' => false,
        'supports' => array('title'),
        'show_in_rest' => true,
        'menu_icon' => 'dashicons-groups'

    );
    register_post_type('members', $args);
}
add_action('init', 'init_members');

So how do I get the member names from the custom post type with ACF to display on the template page

Share Improve this question asked Apr 9, 2020 at 10:25 AstaAsta 254 bronze badges 1
  • I have added an example to you. If you could give me some more information such as the field name and how do you use the original post title. I could give more accurate details to meet your question. – 西門 正 Code Guy - JingCodeGuy Commented Apr 9, 2020 at 10:47
Add a comment  | 

1 Answer 1

Reset to default 0

You just need to put a query in your template. As an example, I use less conditions. You may add what is necessary later.

// prepare arguments
$args = array(
    'post_type'                 => 'members',
    'orderby'                   => 'name',
    'order'                     => 'ASC',
    'post_status'               => 'publish',
);

// create a query based on arguments
$query = new WP_Query($args);

while ($query->have_posts()): 
    $query->the_post();

    // output here
    // WP builtin: eg. echo get_the_title();

    // you can add html table here

    // ACF builtin: echo acf_get_metadata( get_the_ID(), 'meta_name' )
endwhile;
wp_reset_query(); 

In between the while loop, you can create html table. I suppose you know how to create html and add php code so I just give you the essential idea.

For query, you may refer to WP_Query class

发布评论

评论列表(0)

  1. 暂无评论