如何根据选中/未选中的复选框显示/隐藏项目


How can I make an item show/hide based on a checkbox being checked/not checked?

所以我在wordpress主题上创建了一个复选框。所有东西都保存得很好,所有这些:

    function member_page_featured_meta() {
        add_meta_box( 'member_page_meta', __( 'Is this a Member Page?', 'member_page-textdomain' ), 'member_page_meta_callback', 'page', 'side', 'low' );
    }
    add_action( 'add_meta_boxes', 'member_page_featured_meta' );
    /**
     * Outputs the content of the meta box
     */
    function member_page_meta_callback( $post ) {
        wp_nonce_field( basename( __FILE__ ), 'member_page_nonce' );
        $member_page_stored_meta = get_post_meta( $post->ID );
        ?>
     <p>
        <div class="member_page-row-content">
            <label for="featured-checkbox">
                <input type="checkbox" name="featured-checkbox" id="featured-checkbox" value="yes" <?php if ( isset ( $member_page_stored_meta['featured-checkbox'] ) ) checked( $member_page_stored_meta['featured-checkbox'][0], 'yes' ); ?> />
                <?php _e( 'Yes', 'member_page-textdomain' )?>
            </label>
        </div>
    </p>   
        <?php
    }
    /**
     * Saves the custom meta input
     */
    function member_page_meta_save( $post_id ) {

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

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }
// Checks for input and saves - save checked as yes and unchecked at no
if( isset( $_POST[ 'featured-checkbox' ] ) ) {
    update_post_meta( $post_id, 'featured-checkbox', 'yes' );
} else {
    update_post_meta( $post_id, 'featured-checkbox', 'no' );
}
}
add_action( 'save_post', 'member_page_meta_save' );

我需要的是制作它,这样我就可以使用Php在Wordpress中显示/隐藏内容,但我不知道如何在Php-if语句中针对它。

我是否使用名称(featured checkbox)、id(#featured check)、功能名称($member_page_featured_meta)等

然后,为了使它成为一个特定的HTML,我想怎么做呢?

当复选框被选中时,我试图隐藏的HTML/PHP。

<header class="entry-header">
    <div style="float:right;">
        <div id="membership" "class='false' if not member"></div>
        <div id="phone-answering" "class='false' if not phone client"></div>
        <div id="location-icon" "class='false' if only one location"></div>
    </div>      
    <?php the_title( '<h1 class="entry-title" style="clear:none; font-size: 3em; text-decoration: underline;">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
    <div id="tabs">
        <ul class="tabs">
            <li><a href="#key-intake" role="tab" data-toggle="tab">Key Intake</a></li>
            <li><a href="#business-facts" role="tab" data-toggle="tab">Business Facts</a></li>
            <li><a href="#common-answers" role="tab" data-toggle="tab">Common Answers</a></li>
            <li><a href="#understanding" role="tab" data-toggle="tab">Understanding</a></li>
            <li><a href="#current" role="tab" data-toggle="tab">Current</a></li>
            <li><a href="#location" role="tab" data-toggle="tab">Locations</a></li>
            <li><a href="#notify" role="tab" data-toggle="tab">Notify</a></li>
        </ul>
        <div id="key-intake" class="">
            <h1  class="tabtitle">Key Intake</h1>
            <?php 
                // Retrieves the stored value from the database
                $key_intake_meta_value = get_post_meta( get_the_ID(), 'Key Intake', true );
                // Checks and displays the retrieved value
                if( !empty( $key_intake_meta_value ) ) {
                    echo $key_intake_meta_value;
                } else {
                    echo 'Value Not Fount or Empty';
                }
            ?>
        </div>
        <div id="business-facts">
        <h1 class="tabtitle">Business Facts</h1>
            <?php 
                // Retrieves the stored value from the database
                $business_facts_meta_value = get_post_meta( get_the_ID(), 'Business Facts', true );
                // Checks and displays the retrieved value
                if( !empty( $business_facts_meta_value ) ) {
                    echo $business_facts_meta_value;
                } else {
                    echo 'Value Not Fount or Empty';
                }
            ?>
        </div>
        <div id="common-answers">
            <h1 class="tabtitle">Common Answers</h1>
            <?php 
                // Retrieves the stored value from the database
                $common_answers_meta_value = get_post_meta( get_the_ID(), 'Common Answers', true );
                // Checks and displays the retrieved value
                if( !empty( $common_answers_meta_value ) ) {
                    echo $common_answers_meta_value;
                } else {
                    echo 'Value Not Fount or Empty';
                }
            ?>
        </div>
        <div id="understanding">
            <h1 class="tabtitle">Understanding</h1>
            <?php 
                // Retrieves the stored value from the database
                $understanding_meta_value = get_post_meta( get_the_ID(), 'Understanding', true );
                // Checks and displays the retrieved value
                if( !empty( $understanding_meta_value ) ) {
                    echo $understanding_meta_value;
                } else {
                    echo 'Value Not Fount or Empty';
                }
            ?>
        </div>
        <div id="current">
            <h1 class="tabtitle">Current</h1>
            <?php 
                // Retrieves the stored value from the database
                $current_meta_value = get_post_meta( get_the_ID(), 'Current', true );
                // Checks and displays the retrieved value
                if( !empty( $current_meta_value ) ) {
                    echo $current_meta_value;
                } else {
                    echo 'Value Not Fount or Empty';
                }
            ?>
        </div>
        <div id="location">
            <h1 class="tabtitle">Locations</h1>
            <?php 
                // Retrieves the stored value from the database
                $locations_meta_value = get_post_meta( get_the_ID(), 'Locations', true );
                // Checks and displays the retrieved value
                if( !empty( $locations_meta_value ) ) {
                    echo $locations_meta_value;
                } else {
                    echo 'Value Not Fount or Empty';
                }
            ?>
        </div>
        <div id="notify">
            <h1 class="tabtitle">Notify</h1>
            <?php 
                // Retrieves the stored value from the database
                $notify_meta_value = get_post_meta( get_the_ID(), 'Notify', true );
                // Checks and displays the retrieved value
                if( !empty( $notify_meta_value ) ) {
                    echo $notify_meta_value;
                } else {
                    echo 'Value Not Fount or Empty';
                }
            ?>
        </div>
    </div>
    <?php
        the_content();
        wp_link_pages( array(
            'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'odextranet' ),
            'after'  => '</div>',
        ) );
    ?>
</div><!-- .entry-content -->

我会使用内联样式来隐藏和显示。你不能从PHP中瞄准HTML,所以主题方面的基本过程是:

  1. 决定是否应该显示
  2. 如果应该隐藏,请使用一个变量来存储用于隐藏节的内联CSS
  3. 在包含要隐藏或显示的数据的节的容器标记中输出该变量

下面是一个使用部分代码的示例:

<?php 
// Retrieves the stored value from the database
$current_meta_value = get_post_meta( get_the_ID(), 'Current', true );
//Will either be empty or contain the inline CSS to hide the content.
$hideOrShow = "";
// Checks and displays the retrieved value
if( !empty( $current_meta_value ) ) {
    $hideOrShow = "display: none;";
}
?>
<div id="content_to_hide_or_show" style="<?php echo $hideOrShow; ?>">
    <!--Content that will be hidden or shown -->
</div>