如何将WordPress meta框的页面属性->页面模板下拉菜单转换为单选按钮?

问题描述 投票:1回答:1

我希望能够将页面属性“模板”下拉菜单更改为单选按钮,以使我可以在其旁边放置相应的缩略图。

似乎这里已经问过这个问题了-Is it possible to make WordPress page attribute meta box select option display as images?-但是没有提供任何代码,而查看注释似乎表明用户回答了自己的问题?

我已经按照下面的说明删除并替换了functions.php上的页面属性元框:

add_action( 'add_meta_boxes', 'wpse44966_add_meta_box' );
function wpse44966_add_meta_box( $post_type ){ 
    remove_meta_box(
        'pageparentdiv',
        'page',
        'side');
    add_meta_box(
        'wpse44966-meta-box',
        'page' == $post_type ? __('Page Style Templates') : __('Attributes'),
        'wpse44966_meta_box_cb', 
        'page', 
        'side', 
        'low');
}

然后我可以在自己的meta框中调用“模板”下拉菜单-我还包括了page_template_dropdown函数(重命名为“ << page_template_dropdown_show_it”)。

function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) { $templates = get_page_templates( null, $post_type ); ksort( $templates ); foreach ( array_keys( $templates ) as $template ) { $selected = selected( $default, $templates[ $template ], false ); echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>'; } } function wpse44966_meta_box_cb( $post ){ echo 'Please select from the below'; if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) : $template = ! empty( $post->page_template ) ? $post->page_template : false; ?> <p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p> <select name="page_template" id="page_template"> <?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?> <option value="default"><?php echo esc_html( $default_title ); ?></option> <?php page_template_dropdown_show_it( $template, $post->post_type ); ?> </select> <?php endif; }
但是,当我同时修改

page_template_dropdown_show_it

wpse44966_meta_box_cb以显示单选按钮时,这些更改在视觉上适用,但是当您选择它们​​时什么也没有发生?function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) { $templates = get_page_templates( null, $post_type ); ksort( $templates ); foreach ( array_keys( $templates ) as $template ) { $checked = checked( $default, $templates[ $template ], false ); echo "\n\t<input type='radio' name='page_template' value='" . esc_attr( $templates[ $template ] ) . "' $checked>" . esc_html( $template ); } } function wpse44966_meta_box_cb( $post ){ echo 'Please select from the below'; if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) : $template = ! empty( $post->page_template ) ? $post->page_template : false; ?> <p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p> <?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?> <input type='radio' name='page_template' value="default"><?php echo esc_html( $default_title ); ?> <?php page_template_dropdown_show_it( $template, $post->post_type ); ?> <?php endif; }
显然,它不像直接交换一样简单(因为它不起作用),但是我能看到的唯一缺失是,现在没有任何东西携带id =“ page_template”,而之前是包含在以下内容中:

<select name="page_template" id="page_template">

php wordpress meta-boxes
1个回答
0
投票
因此,看来我是对的,问题是不再传递ID“ page_template”。

[为解决这个问题,我所做的全部工作都是应用一些Javascript(使用admin_enqueue_scripts函数),该Javascript将id“ page_template”添加到已选择的单选按钮中。

© www.soinside.com 2019 - 2024. All rights reserved.