programing

사용자 지정 게시물 유형에 대한 읽기 설정에 정적 페이지 추가

instargram 2023. 10. 29. 18:59
반응형

사용자 지정 게시물 유형에 대한 읽기 설정에 정적 페이지 추가

사용자 지정 게시물 유형을 만들었고 사용자가 보관 페이지에 대한 정적 페이지를 선택할 수 있도록 하고 싶습니다.예를 들어, [게시] 페이지 아래에 있습니다.Projects 페이지를 추가합니다. [ Dropout ]

읽기 설정 페이지에 다른 정적 페이지 옵션을 추가하는 방법이 있습니까?그것을 수정할 수 있는 기존의 후크가 있습니까?

reading settings

또한 선택한 페이지를 페이지에 이렇게 표시하는 방법이 있습니까?

enter image description here

업데이트:

을 살피는 것/wp/wp-admin/includes/template.php:1768

/**
 * Filters the default post display states used in the posts list table.
 *
 * @since 2.8.0
 * @since 3.6.0 Added the `$post` parameter.
 *
 * @param array   $post_states An array of post display states.
 * @param WP_Post $post        The current post object.
 */
$post_states = apply_filters( 'display_post_states', $post_states, $post );

if ( ! empty($post_states) ) {
    $state_count = count($post_states);
    $i = 0;
    echo ' — ';
    foreach ( $post_states as $state ) {
        ++$i;
        ( $i == $state_count ) ? $sep = '' : $sep = ', ';
        echo "<span class='post-state'>$state$sep</span>";
    }
}

post_state에 갈고리가 있는 것 같은데 어떻게 설정합니까?

그리고./wp-admin/options-reading.php:83

실제로 파일을 수정하지 않고 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.
    ) );
} );

/**
 * Renders the custom "Projects page" field.
 *
 * @param array $args
 */
function settings_field_page_for_projects( $args ) {
    $id = 'page_for_projects';
    wp_dropdown_pages( array(
        'name'              => $id,
        'show_option_none'  => '&mdash; Select &mdash;',
        'option_none_value' => '0',
        'selected'          => get_option( $id ),
    ) );
}

/**
 * Adds page_for_projects to the white-listed options, which are automatically
 * updated by WordPress.
 *
 * @param array $options
 */
add_filter( 'whitelist_options', function ( $options ) {
    $options['reading'][] = 'page_for_projects';

    return $options;
} );

미리보기:

enter image description here

또한 선택한 페이지를 페이지에 이렇게 표시하는 방법이 있습니까?

네.

코드:

/**
 * 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 );

미리보기:

enter image description here

필요하거나 필요한 경우, JavaScript/jQuery를 사용하여 "프로젝트 페이지" 필드를 "홈페이지 표시" 열의 "게시물 페이지" 아래로 이동할 수 있습니다.

언급URL : https://stackoverflow.com/questions/48724545/add-static-page-to-reading-settings-for-custom-post-type

반응형