如何在我的前端打印所选下拉元框(wordpress)的结果?

How do I print on my frontend the result from a selected dropdown metabox (wordpress)?

因此,我创建了一个具有此元数据框的自定义 post 类型。 metabox 没问题,但我不确定它是否保存正确(我猜是)。假设此代码一切正常(请,如果有任何问题,请告诉我)我想知道... 如何在我的前端显示从我的 metabox 中选择的日期和月份?

(我对后端不太了解,我正在尝试解决这个问题,对于任何误解,我们深表歉意)

自定义Post类型代码:

function ccr_agenda() {

    $labels = array(
        'name'                => __( 'Agenda', 'codexcoder' ),
        'singular_name'       => __( 'Agenda', 'codexcoder' ),
        'add_new'             => _x( 'Add New Item', 'codexcoder', 'codexcoder' ),
        'add_new_item'        => __( 'Add New Item', 'codexcoder' ),
        'edit_item'           => __( 'Edit item', 'codexcoder' ),
        'new_item'            => __( 'New item', 'codexcoder' ),
        'view_item'           => __( 'View Agenda', 'codexcoder' ),
        'search_items'        => __( 'Search in Agenda', 'codexcoder' ),
        'not_found'           => __( 'Not found in Agenda', 'codexcoder' ),
        'not_found_in_trash'  => __( 'Not found in Agenda Trash', 'codexcoder' ),
        'parent_item_colon'   => __( 'parent-item Agenda:', 'codexcoder' ),
        'menu_name'           => __( 'Agenda', 'codexcoder' ),
        );

    $args = array(
        'labels'              => $labels,
        'hierarchical'        => false,
        'description'         => 'descrição',
        'taxonomies'          => array(),
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => null,
        'menu_icon'           => null,
        'show_in_nav_menus'   => true,
        'publicly_queryable'  => true,
        'exclude_from_search' => false,
        'has_archive'         => true,
        'query_var'           => true,
        'can_export'          => true,
        'rewrite'             => true,
        'capability_type'     => 'post',
        'supports'            => array(
            'title', 'editor', 'thumbnail'
            )
        );

    register_post_type( 'agenda', $args );
    flush_rewrite_rules( false );
}

add_action( 'init', 'ccr_agenda' );

metabox代码:

// Agenda meta box register
add_action( 'add_meta_boxes', 'agenda_metabox' );

function agenda_metabox($post){
    add_meta_box('agenda_metabox_id', 'Date event', 'show_agenda_metabox', '$post->post_type', 'side' , 'high');
}

// Saving Agenda data
add_action('save_post', 'ccr_agenda_save');

function ccr_agenda_save(){ 
    global $post;
    if(isset($_POST["dia_agenda"])){
         //UPDATE: 
        $meta_agenda_dia = $_POST['dia_agenda'];
        update_post_meta($post->ID, 'show_agenda_metabox', $meta_agenda_dia);
        //END OF UPDATE
    }

    if(isset($_POST["mes_agenda"])){
         //UPDATE: 
        $meta_agenda_mes = $_POST['mes_agenda'];
        update_post_meta($post->ID, 'show_agenda_metabox', $meta_agenda_mes);
        //END OF UPDATE

    }
}

function show_agenda_metabox($post){
    $meta_agenda_dia = get_post_meta($post->ID, 'show_agenda_metabox', true); 

    $meta_agenda_mes = get_post_meta($post->ID, 'show_agenda_metabox', true); 
    ?>   

    <label>Select a day:  </label>

    <select name="dia_agenda" id="dia_agenda">
      <option value="01" <?php selected( $meta_agenda_dia, '01' ); ?>>01</option>
      <option value="02" <?php selected( $meta_agenda_dia, '02' ); ?>>02</option>
// [ ... ]
      <option value="30" <?php selected( $meta_agenda_dia, '30' ); ?>>30</option>
      <option value="31" <?php selected( $meta_agenda_dia, '31' ); ?>>31</option>
    </select>

    <br />

    <label>Select a month:  </label>

    <select name="mes_agenda" id="mes_agenda">
      <option value="01" <?php selected( $meta_agenda_dia, '01' ); ?>>jan</option>
// [... ]
      <option value="12" <?php selected( $meta_agenda_dia, '12' ); ?>>dec</option>
    </select>


    <?php
}

我的前端正在尝试像这样打印选定的日期和月份:

<ul id="event-list">
    <?php

        $args = array(
            'post_type'     => 'agenda',
            'post_status'   => 'publish',
            'posts_per_page'=> 4,
        );

        $getPosts = new WP_Query($args);
        add_filter( 'the_title', 'max_title_length');

        while($getPosts->have_posts()) : $getPosts->the_post(); ?>

        <li>
            <span class="event">
                <h4 id="day"><?php echo get_post_meta(get_the_id(), 'dia_agenda', true ); ?></h4>
                <h5 id="month"><?php echo get_post_meta(get_the_id(), 'mes_agenda', true ); ?></h5>
            </span>

            <span class="sidebar-post-title"><a href="<?php echo the_permalink(); ?>"><?php the_title(); ?></a></span>
        </li>

        <?php endwhile;
        remove_filter( 'the_title', 'max_title_length');

    ?>                  
</ul>

但是上面的这段代码没有在屏幕上打印任何内容。 所以,我有两个问题:

  1. 我做错了什么?我怎样才能做到这一点?

  2. 一旦我 get/printed 得到正确的结果,我怎样才能按这些结果在 list/loop 顺序中显示它? (例如,最接近我当前日期的日期和月份将首先显示在列表中,依此类推)。

I've searched about this but I didn't get any good results. Maybe it's because I don't know much about back-end (like I said before), I'm just trying to learn. (Consider this fact when you read this, pls :D)


第一次更新-(@yivi 提出的更改)

我进行了更改,metabox 不再显示在我的自定义 post 类型页面上。我在这里遗漏了什么吗?

// Agenda meta box register
add_action( 'add_meta_boxes', 'agenda_metabox' );

function agenda_metabox($post){
    add_meta_box('agenda_metabox_id', 'Date Event', 'show_agenda_metabox', '$post->post_type', 'side' , 'high');
}

// Saving Agenda data
add_action('save_post', 'ccr_agenda_save');

function ccr_agenda_save(){ 
    global $post;
    if(isset($_POST["dia_agenda"])){
         //UPDATE: 
        $meta_agenda_dia = $_POST['dia_agenda'];
        update_post_meta($post->ID, 'dia_agenda', $meta_agenda_dia);
        //END OF UPDATE
    }

    if(isset($_POST["mes_agenda"])){
         //UPDATE: 
        $meta_agenda_mes = $_POST['mes_agenda'];
        update_post_meta($post->ID, 'mes_agenda', $meta_agenda_mes);
        //END OF UPDATE
    }
}

function show_agenda_metabox($post){
    $meta_agenda_dia = get_post_meta($post->ID, 'dia_agenda', true); 

    $meta_agenda_mes = get_post_meta($post->ID, 'mes_agenda', true); 

 [...]

您正在将元数据保存在 show_agenda_metabox 下,但您正在尝试检索 dia_agendames_agenda 键。

你有这个:

update_post_meta($post->ID, 'show_agenda_metabox', $meta_agenda_dia);
update_post_meta($post->ID, 'show_agenda_metabox', $meta_agenda_mes);

然后是这个:

echo get_post_meta(get_the_id(), 'mes_agenda', true );

对于这两个 "update_post_meta",您实际上是在用 "month" 元覆盖您的 "day" 元。所以你应该把它改成这样的:

update_post_meta($post->ID, 'agenda_dia', $meta_agenda_dia);
update_post_meta($post->ID, 'agenda_mes', $meta_agenda_mes);

然后相应地使用这些调用。

echo get_post_meta(get_the_ID(), 'agenda_mes', true );
echo get_post_meta(get_the_ID(), 'agenda_dia', true );

应该可以。

(还要检查你的 "month" select 你实际上指的是你的 "day" 变量:

<select name="mes_agenda" id="mes_agenda">
  <option value="01" <?php selected( $meta_agenda_dia, '01' ); ?>>jan</option>

(记得把 $meta_agenda_dia 改成 $meta_agenda_mes

要按这些元进行排序,请选中 WP_Query sorting docs,这样您就可以按其中一个元进行排序;但 WP 不允许本机排序超过元。您可以使用 posts_orderby 过滤器来完成,但它可能有点超出您当前的技能范围,至少现在是这样。