duanguochong0397 2016-08-23 02:10
浏览 48

如何使用Metabox复选框在数组中添加页面ID?

What I'm trying to do is for a dual-language website. This particular language, Papiamento, isn't supported by Wordpress. Therefore, the client had to create two separate pages, in English and Pap. What I did was to code it like that to display English or Pap menu for each page.

Like this, in header.php:

<?php

    if( is_page( array('salon-and-spa-pap', 'tocante-nos', 'testimonio', 'tuma-contacto-cu-nos', 'galeria', 'tratamentonan-di-masahe', 'tratamentonan-spa-di-curpa', 'servicionan-di-boda', 'tratamentonan-spa-di-cara', 'wowo-lip-nek', 'cuido-di-man', 'tratamento-di-huna', 'tratamento-di-huna-di-pia', 'cuido-di-pia', 'salon-p', 'spa-etiquette-pap', 'wax-p', 'reserva-un-tratamento')) ) {
        wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
    } else {
        wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
    }

?>

However, the problem is that the client will have to keep going back to header.php to add another page slug every time she create a new page. Therefore, I created a Metabox plugin for that. I made a Metabox checkbox so that everytime a page is intended for Papiamento language, the client can just check the box and either the page id or slug will be added to the code above.

I found another question (https://wordpress.stackexchange.com/questions/71043/listing-pages-with-checkboxes-in-a-metabox-and-saving-them) that might be similar to what I was looking for but it didn't work for me.

Here's my metabox function based on this article (http://themefoundation.com/wordpress-meta-boxes-guide/).

<?php
function prfx_custom_meta() {
    add_meta_box( 'prfx_meta', __( 'Papiamento Page Box', 'prfx-textdomain' ), 'prfx_meta_callback', 'page', 'normal', 'low' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );

function prfx_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
    $prfx_stored_meta = get_post_meta( $post->ID );
    $checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
    ?>


    <p>
    <span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
    <div class="prfx-row-content">
        <label for="meta-checkbox">
            <input type="checkbox" name="checkfield[]" id="page_<?php echo $page->ID; ?>" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/> <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?>
            <?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?>
        </label>
    </div>
</p>
    <?php
}

/**
 * Saves the custom meta input
 */
function prfx_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

// Checks for input and sanitizes/saves if needed

    // Checks for input and saves
    if( isset( $_POST[ 'checkfield' ] ) ) {
        update_post_meta($post_id, "checkfield", $_POST['checkfield'] );
    } 

}
add_action( 'save_post', 'prfx_meta_save' );

and calling the page id in header.php like this:

<?php

    if( in_array($page->ID, (array) $checkfield) ) {
        wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
    } else {
        wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
    }

?>

But it didn't work. Please help!

***************** UPDATE ***************** I've been trying to edit the function prfx_meta_callback( $post ) but with no success. Here's the latest code I'm trying to modify...

function prfx_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
    $checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );

    $page = get_pages(); 
    $prfx_stored_meta = get_post_meta( $post->ID );

    ?>

    <p>
    <span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
    <div class="prfx-row-content">

       <label for="page_<?php echo $page->ID; ?>">

        <input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( isset($checkfield [ ?>'<?php echo $page->ID; ?>'<?php ] ) ) checked( $checkfield[ ?>'<?php echo $page->ID; ?>'<?php ][0], '<?php echo $page->ID; ?>'); ?>/>    <?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?></label> <br>

    </div>
</p>

 <?php 

I've been trying to make it like that, if Pap, then checked, but if not Pap, unchecked in every page.

  • 写回答

1条回答 默认 最新

  • dpxyfa4718 2016-08-23 02:35
    关注

    If the metabox is saving the checkbox settings properly, you can use the following code to make the check in header.php:

    if ( in_array( get_the_ID(), get_post_meta( get_the_ID(), 'checkfield', true ) ) ) {
        wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
    } else {
        wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法