无法在前端显示来自metabox wp_dropdown_ages()的选定帖子标题

时间:2021-03-17 作者:Mathieu Préaud

我在admin中使用wp_dropdown_pages() 显示自定义帖子类型中的帖子列表workshop. metabox下拉列表在admin中运行良好,我可以选择文章标题并保存它。

问题是,当我尝试在前端显示metabox的值时,它只返回所选帖子的ID,而不是标题。

如何在前端返回所选帖子的标题?

metabox的PHP代码:

add_action( \'add_meta_boxes\', \'mysite_work_add_meta_box\' );
if ( ! function_exists( \'mysite_work_add_meta_box\' ) ) {
    /**
     * Add meta box to page screen
     *
     * @since 1.0.0
     */

    function mysite_work_add_meta_box() {
        add_meta_box( \'additional-work-metabox-options\', esc_html__( \'Info\', \'mysite\' ), \'mysite_work_metabox_controls\', \'work\', \'normal\', \'low\' );
    }
}

if ( ! function_exists( \'mysite_work_metabox_controls\' ) ) {
    /**
     * Meta box render function
     *
     * @param  object $post Post object.
     * @since  1.0.0
     */

    function mysite_work_metabox_controls( $post ) {
        $meta = get_post_meta( $post->ID );
        $workshop_page_select = ( isset( $meta[\'workshop_page_select\'][0] ) && \'\' !== $meta[\'workshop_page_select\'][0] ) ? $meta[\'workshop_page_select\'][0] : \'\';
        wp_nonce_field( \'mysite_work_control_meta_box\', \'mysite_work_control_meta_box_nonce\' ); // Always add nonce to your meta boxes!
        ?>
        <div class="post_meta_extras">
            <p>
                <?php
                $args_pages = array(
                    \'post_type\'                         => \'workshop\',
                    \'orderby\'                           => \'publish_date\',
                    \'depth\'                 => 0,
                    \'child_of\'              => 0,
                    \'selected\'              => $workshop_page_select,
                    \'echo\'                  => 1,
                    \'name\'                  => \'workshop_page_select\',
                    \'id\'                    => \'workshop_page_select\',
                    \'class\'                 => null,
                    \'show_option_none\'      => null,
                    \'show_option_no_change\' => null,
                    \'option_none_value\'     => esc_html__( \'&ndash; Elegir &ndash;\', \'mysite\' ),
                );
                ?>
                <label for="workshop_page_select"><?php esc_attr_e( \'Taller relacionado\', \'mysite\' ); ?></label>
                <?php wp_dropdown_pages( $args_pages ); ?>
            </p>

            <?php
    }
}

add_action( \'save_post\', \'mysite_work_save_metaboxes\' );

if ( ! function_exists( \'mysite_work_save_metaboxes\' ) ) {
    /**
     * Save controls from the meta boxes
     *
     * @param  int $post_id Current post id.
     * @since 1.0.0
     */
    function mysite_work_save_metaboxes( $post_id ) {

        if ( ! isset( $_POST[\'mysite_work_control_meta_box_nonce\'] ) || ! wp_verify_nonce( sanitize_key( $_POST[\'mysite_work_control_meta_box_nonce\'] ), \'mysite_work_control_meta_box\' ) ) { // Input var okay.
            return $post_id;
        }

        if ( isset( $_POST[\'post_type\'] ) && \'page\' === $_POST[\'post_type\'] ) { // Input var okay.
            if ( ! current_user_can( \'edit_page\', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( \'edit_post\', $post_id ) ) {
                return $post_id;
            }
        }

        if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        if ( isset( $_POST[\'workshop_page_select\'] ) ) { // Input var okay.
            update_post_meta( $post_id, \'workshop_page_select\', sanitize_text_field( wp_unslash( $_POST[\'workshop_page_select\'] ) ) ); // Input var okay.
        }

    }
}
前端代码:

<?php 
$related_workshop = get_post_meta( get_the_ID(), \'workshop_page_select\', true ); 
if ( isset( $related_workshop ) && \'\' !== $related_workshop ) : 
  echo $related_workshop; 
endif; 
?>
谢谢你。

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

那是因为,as documented 中选项的值wp_dropdown_pages() 是ID:

\'value_field\'(字符串)用于填充选项元素的“value”属性的Post字段。接受任何有效的post字段。默认“ID”。

因此,既然您已经保存了ID,请使用它来获取标题:

echo get_the_title( $related_workshop );