【问题标题】:How can I make an item show/hide based on a checkbox being checked/not checked?如何根据选中/未选中的复选框来显示/隐藏项目?
【发布时间】:2016-07-30 22:20:44
【问题描述】:

所以我在我的 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' );

我需要做的是,这样我就可以使用它在 Wordpress 中使用 Php 显示/隐藏内容,但我不完全知道如何在 php if 语句中定位它。

我是否使用名称 (featured-checkbox)、id (#featured-checkbox)、功能名称 ($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 -->

【问题讨论】:

  • 您要操作的内容在哪里?是主题的一部分还是进入了内容编辑器?
  • 它是主题的一部分。我想在选中该框时显示几行 HTML/PHP,而不是在未选中时显示。
  • 不是函数,而是您要显示和隐藏的 HTML。
  • 我添加了我想要隐藏/显示的代码块。
  • 而且我知道我会用 PHP 标记包围它以使其工作,我只是不知道 php.ini 的格式。我已经使用 PHP 大约 6 个月了,但只是在调试级别。这是我第一次从头开始创建任何东西。

标签: php wordpress if-statement checkbox


【解决方案1】:

我会使用内联样式来隐藏和显示。您不能从 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>

【讨论】:

  • 似乎没有使用我拥有的复选框。我已经用 'featured-checkbox' (在我的 php 函数中创建的复选框的 ID)替换了 'Current'(我假设您从我的 HTML 代码中获取了其中一个 ID),并且它仍然没有改变。默认情况下,它是隐藏的。
  • 有什么方法可以使用复选框中的 checked="checked" 吗?
  • 其实,让我们简化一下。如果选中复选框,我只想添加一个类怎么办。假设我有一个 div 有一个 ID,但没有一个类。如果复选框被选中,则为 class="true",如果未选中,则为 class="false"?
  • 您是否从前端的复选框中获得了正确的值?
  • 我该如何检查?
猜你喜欢
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
相关资源
最近更新 更多