Display custom post type to static page

161 views Asked by At

I have a custom post type "cs_portfolio" for my project. What I want to achieve is to be able the user to choose a specific page to display all the data of "cs_portfolio".

Example of this is the blog post when you set in Settings > Reading the static page of post. It automatically use the index.php to loop all the blog post.

In my case I already figure out how to add a dropdown field in Settings > Reading and save it.

Settings > Reading 1: https://i.stack.imgur.com/w6ybw.png

Here's the code for adding dropdown field in Settings > Reading.

/**
 * Adds a custom field: "Projects page"; on the "Settings > Reading" page.
 */
add_action( 'admin_init', function () {
    $id = 'page_for_projects';
    // add_settings_field( $id, $title, $callback, $page, $section = 'default', $args = array() )
    add_settings_field( $id, 'Projects Page:', 'settings_field_page_for_projects', 'reading', 'default', array(
        'label_for' => 'field-' . $id, // A unique ID for the field. Optional.
        'class'     => 'row-' . $id,   // A unique class for the TR. Optional.
    ) );
} );

function settings_field_page_for_projects( $args ) {
    $id = 'page_for_projects';
    wp_dropdown_pages( array(
        'name'              => $id,
        'show_option_none'  => '— Select —',
        'option_none_value' => '0',
        'selected'          => get_option( $id ),
    ) );
}

add_filter( 'whitelist_options', function ( $options ) {
    $options['reading'][] = 'page_for_projects';

    return $options;
} );

And then in list of Page I already set the display posts states.

Page List 2: https://i.stack.imgur.com/bVvWo.png

Here's the code for setting display post state.

 /**
 * Filters the post states on the "Pages" edit page. Displays "Projects Page"
 * after the post/page title, if the current page is the Projects static page.
 *
 * @param array $states
 * @param WP_Post $post
 */
add_filter( 'display_post_states', function ( $states, $post ) {
    if ( intval( get_option( 'page_for_projects' ) ) === $post->ID ) {
        $states['page_for_projects'] = __( 'Projects Page' );
    }

    return $states;
}, 10, 2 );

My problem now is what should be the file to be used by Portfolio Page to be called to loop all the data in "cs_portfolio" ? and maintain the slug of "cs_portfolio" ?

0

There are 0 answers