在Wordpress中显示Metabox的问题

时间:2015-07-20 20:27:46

标签: php wordpress

我有以下代码,所有内容都保存得当,但我无法在网站主页上正确显示。

问题是在帖子页面上显示加倍并且在主页上的随机位置显示。但是我希望它只显示在主页上的某个地方。

function home_page_position_get_meta( $value ) {
    global $post;

    $field = get_post_meta( $post->ID, $value, true );
    if ( ! empty( $field ) ) {
        return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
    } else {
        return false;
    }
}

function home_page_position_add_meta_box() {
    add_meta_box(
        'home_page_position-home-page-position',
        __( 'Home Page Position', 'home_page_position' ),
        'home_page_position_home_page_position_html',
        'post',
        'side',
        'default'
    );
}
add_action( 'add_meta_boxes', 'home_page_position_add_meta_box' );


function home_page_position_home_page_position_html( $post) {
    wp_nonce_field( '_home_page_position_home_page_position_nonce', 'home_page_position_home_page_position_nonce' ); 
    $home_page_position_home_page_position_box_position_value =  get_post_meta($post->ID,'home_page_position_home_page_position_box_position',true);
?>

    <p>
        <label for="home_page_position_home_page_position_box_position"><?php _e( 'Box Position', 'home_page_position' ); ?></label><br>
        <select name="home_page_position_home_page_position_box_position">

            <option id='none-none' value='none' name='none_' <?php selected($home_page_position_home_page_position_box_position_value, 'none'); ?> >None</option>
            <option id='box-01' value='box01' name='box_01' <?php selected($home_page_position_home_page_position_box_position_value, 'box01'); ?> >Box 01</option>
            <option id='box-02' value='box02' name='box_02' <?php selected($home_page_position_home_page_position_box_position_value, 'box02'); ?> >Box 02</option>
            <option id='box-03' value='box03' name='box_03' <?php selected($home_page_position_home_page_position_box_position_value, 'box03'); ?> >Box 03</option>

        </select>
    </p>    <p>

        <label for="home_page_position_home_page_position_page_title"><?php _e( 'Page Title', 'home_page_position' ); ?></label><br>
        <input type="text" name="home_page_position_home_page_position_page_title" id="home_page_position_home_page_position_page_title" value="<?php echo home_page_position_get_meta( 'home_page_position_home_page_position_page_title' ); ?>">
    </p>    <p>

        <label for="home_page_position_home_page_position_introduction"><?php _e( 'Introduction', 'home_page_position' ); ?></label><br>
        <textarea name="home_page_position_home_page_position_introduction" id="home_page_position_home_page_position_introduction" ><?php echo home_page_position_get_meta( 'home_page_position_home_page_position_introduction' ); ?></textarea>

    </p>    <p>

        <label for="home_page_position_home_page_position_link_to_page"><?php _e( 'Link To Page', 'home_page_position' ); ?></label><br>
        <input type="text" name="home_page_position_home_page_position_link_to_page" id="home_page_position_home_page_position_link_to_page" value="<?php echo home_page_position_get_meta( 'home_page_position_home_page_position_link_to_page' ); ?>">
    </p><?php
}


function home_page_position_home_page_position_save( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( ! isset( $_POST['home_page_position_home_page_position_nonce'] ) || ! wp_verify_nonce( $_POST['home_page_position_home_page_position_nonce'], '_home_page_position_home_page_position_nonce' ) ) return;
    if ( ! current_user_can( 'edit_post' ) ) return;

    if ( isset( $_POST['home_page_position_home_page_position_box_position'] ) )
        update_post_meta( $post_id, 'home_page_position_home_page_position_box_position', esc_attr( $_POST['home_page_position_home_page_position_box_position'] ) );

    if ( isset( $_POST['home_page_position_home_page_position_page_title'] ) )
        update_post_meta( $post_id, 'home_page_position_home_page_position_page_title', esc_attr( $_POST['home_page_position_home_page_position_page_title'] ) );

    if ( isset( $_POST['home_page_position_home_page_position_introduction'] ) )
        update_post_meta( $post_id, 'home_page_position_home_page_position_introduction', esc_attr( $_POST['home_page_position_home_page_position_introduction'] ) );

    if ( isset( $_POST['home_page_position_home_page_position_link_to_page'] ) )
        update_post_meta( $post_id, 'home_page_position_home_page_position_link_to_page', esc_attr( $_POST['home_page_position_home_page_position_link_to_page'] ) );
}
add_action( 'save_post', 'home_page_position_home_page_position_save' );


function cd_display_quote( $value )
{
     // We're in the loop, so we can grab the $post variable
    global $post;

    $position = get_post_meta( $post->ID, 'home_page_position_home_page_position_box_position', true );
    $pTitle = get_post_meta( $post->ID, 'home_page_position_home_page_position_page_title', true );
    $intro = get_post_meta( $post->ID, 'home_page_position_home_page_position_introduction', true );
    $link = get_post_meta( $post->ID, 'home_page_position_home_page_position_link_to_page', true );  

    if( $position != 'none' ) {
        echo $position;
        echo $pTitle;
        echo $intro;
        echo $link;        
    }


    // Return the values: quote first, then the content
    return $value;
}
add_action('the_content', 'cd_display_quote');

1 个答案:

答案 0 :(得分:0)

答案

您的回显数据而不是从方法返回它意味着您的元数据将始终在输出之前输出内容。

相反,请尝试使用以下代码输出内容:

function cd_display_quote( $value )
{
     // We're in the loop, so we can grab the $post variable
    global $post;

    $position = get_post_meta( $post->ID, 'home_page_position_home_page_position_box_position', true );
    $pTitle = get_post_meta( $post->ID, 'home_page_position_home_page_position_page_title', true );
    $intro = get_post_meta( $post->ID, 'home_page_position_home_page_position_introduction', true );
    $link = get_post_meta( $post->ID, 'home_page_position_home_page_position_link_to_page', true );  

    $prepend = '';
    if( $position != 'none' ) {
        $prepend .= $position;
        $prepend .= $pTitle;
        $prepend .= $intro;
        $prepend .= $link;        
    }


    // Return the values: quote first, then the content
    return prepend  . $value;
}
add_filter('the_content', 'cd_display_quote');

说明:

您可能会注意到我已将add_action更改为add_filter the_content - 这不是至关重要,但这是一种很好的做法。你看,WordPress实际上为add_action add_filter运行相同的代码 - 唯一真正的区别是“行动”不必返回值,它们是用作钩子来运行某些代码或在特定时间执行任务。另一方面,“过滤器”就是这样 - 将值传递给回调方法,并且预计会返回过滤后的值。

本文的摘录(http://www.zell-weekeat.com/wordpress-actions-and-filters/)总结得很好:

操作

  • 必须添加某些内容
  • 使用add_action()声明。
  • 与do_action()一起使用。

过滤器

  • 当某些内容必须更改
  • 使用apply_filters()声明。
  • 与add_filters()一起使用。

希望这有帮助。