0

カスタム メタ ボックスにあるドロップダウンにカスタム投稿タイプを正常にプルしました。ただし、フロント エンドに表示するときは、投稿の名前だけでなく、実際の投稿へのリンクも提供したいと考えています。これを配列として保存する必要があると思いますか?これはドロップダウンで可能ですか?私はこれにどのようにアプローチすべきかについて混乱しています。どんな助けでも大歓迎です。

これが私がこれまでに持っているものです:

// 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'] ) );
}
4

2 に答える 2

1

投稿のリンクを取得するには、get_permalink 関数を使用できます

<?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(); 「値」属性を選択します。

于 2013-01-15T05:20:16.480 に答える
0

そこで、別の解決策を思いつきました。配列を保存しようとする代わりに、投稿 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>';
?>
于 2013-01-15T21:45:13.493 に答える