【问题标题】:Save array from drop-down menu in Wordpress从 Wordpress 的下拉菜单中保存数组
【发布时间】:2013-01-15 00:26:07
【问题描述】:

我已成功将自定义帖子类型拉入自定义元框中的下拉列表中。但是,当在前端显示它时,我还想提供指向实际帖子的链接,而不仅仅是帖子的名称。所以我猜我需要将它保存为一个数组?这可以通过下拉菜单实现吗?对我应该如何处理这个感到困惑。非常感谢任何帮助。

这是我目前所拥有的:

// Add Meta Box To Select Overseeing Pastor
add_action('admin_init', 'ministry_select_add_meta');
function ministry_select_add_meta(){
    add_meta_box('ministry_select_post', __('Overseeing Pastor'), 'ministry_select_meta', 'ministry', 'side');
}

function ministry_select_meta( $post ) {
    $values = get_post_custom( $post->ID );
    $selected = isset( $values['pastor_select'] ) ? esc_attr( $values['pastor_select'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
        <select name="pastor_select">
            <?php
            $args = array(
                'post_type' => 'employee',
                'position' => 'pastor'
            );
            $pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
                $is_selected = (get_the_title() == $selected) ? 'selected="selected"' : '';
                echo '<option value="'.get_the_title().'" '.$is_selected.'>'.get_the_title().'</option>';
            endwhile; wp_reset_postdata();
            ?>
        </select>
    <?php   
}

add_action( 'save_post', 'ministry_select_save' );
function ministry_select_save( $post_id )
{
    // Stop If Autosaving
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // Stop If Nonce Can't Be Verified
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // Stop If Unauthorized User
    if( !current_user_can( 'edit_post' ) ) return;

    // Make Sure Data Is Set Then Save      
    if( isset( $_POST['pastor_select'] ) )
        update_post_meta( $post_id, 'pastor_select', esc_attr( $_POST['pastor_select'] ) );
}

【问题讨论】:

  • 为什么要保存链接?您的下拉菜单是如何工作的?
  • 我希望能够在前端指定链接,以便将您带到牧师的个人资料。
  • 当在下拉列表中选择帖子标题时,您想要什么是转到帖子 URL?
  • 我想在另一个职位类型(事工)中选择一个职位类型(牧师)。一旦选择了它,我想在前端填充牧师的名字以及该帖子后面的链接(牧师的个人资料)。我的第一部分工作正常,我只需要能够拉入该帖子的链接,以便我可以抓住它并将其显示在前端。
  • 在下面的回复中,我提到了为您提供帖子 URL 的功能

标签: arrays wordpress


【解决方案1】:

要获取帖子的链接,您可以使用get_permalink function

<?php $permalink = get_permalink( ); ?>

如果你在循环之外,也可以这样

<?php $permalink = get_permalink( $post->ID ); ?>

您可以使用它在 HTML 代码的任何位置打印它。

如果您想要在下拉列表中选择帖子标题时转到帖子 URL,您可以使用 JavaScript 代码执行此操作,执行以下操作:

<select name="pastor_select" onchange='location=this.options[this.selectedIndex].value;'>
            <?php
            $args = array(
                'post_type' => 'employee',
                'position' => 'pastor'
            );
            $pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
                $is_selected = (get_the_title() == $selected) ? 'selected="selected"' : '';
                echo '<option value="'.get_permalink( ).'" '.$is_selected.'>'.get_the_title().'</option>';
            endwhile; wp_reset_postdata();
            ?>
        </select>

如果你想要保存一些 POST 信息,建议保存 POST 的 ID,以便稍后你可以检索该 POST 的任何数据,如果你想存储永久链接和标题,你可以结合函数 get_permalink() ;和 get_the_title();在选择“值”属性中。

【讨论】:

    【解决方案2】:

    所以我想出了一个不同的解决方案。我没有尝试保存数组,而是保存了帖子 ID,这样我就可以访问帖子的标题以及永久链接。

    这是我修改后的代码

    <select name="pastor_select">
        <?php
        $args = array(
            'post_type' => 'employee',
            'position' => 'pastor'
        );
        $pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
            $employeeID = get_the_ID(); // THIS FIXED THE PROBLEM
            $is_selected = ($employeeID == $selected) ? 'selected="selected"' : '';
            echo '<option value="'.$employeeID.'" '.$is_selected.'>'.get_the_title().'</option>';
        endwhile; wp_reset_postdata();
        ?>
    </select>
    

    这就是我在前端的调用方式

    <?php 
    $id = $post_meta_data['pastor_select'][0];
    echo '<a href="'.get_permalink($id).'">';
    echo get_the_title($id);
    echo '</a>';
    ?>
    

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 1970-01-01
      • 2011-07-20
      • 2013-06-26
      • 2017-02-17
      • 1970-01-01
      • 2019-10-20
      • 2011-10-31
      • 2014-08-05
      相关资源
      最近更新 更多