【发布时间】: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