简体   繁体   中英

if control on Wordpress

I need to insert an "if" control in my wordpress code because I want that this checkbox appears only with specific user. But I receive always an error.

<?php if ( !current_user_can('edit_users') ) {
    <?php $featured_checked = get_post_meta( $post->ID, '_elist_featured', true ) && 'on' == get_post_meta( $post->ID, '_elist_featured', true ) ? ' checked="checked"' : ''; ?><label for="_elist_featured" class="selectit"><input name="_elist_featured" type="checkbox" id="_elist_featured" <?php echo esc_html( $featured_checked ); ?>><?php esc_html_e( 'This listing is featured.' ); ?></label></div> 

    } ?>

You need to properly open and close the PHP tags surrounding your IF statement and closing curly brace. Your current code is missing ?> at the end of the first line and <?php at the beginning of the last

Incorrect:

<?php if ( !current_user_can('edit_users') ) {
<-- your HTML and PHP -->
} ?>

Correct:

<?php if ( !current_user_can('edit_users') ) { ?>
<-- your HTML and PHP -->
<?php } ?>

You are using additional <?php tags in your code.

FYI- try to use a good editor to fix these kinda bugs

<?php if ( !current_user_can('edit_users') ) {
    $featured_checked = get_post_meta( $post->ID, '_elist_featured', true ) && 'on' == get_post_meta( $post->ID, '_elist_featured', true ) ? ' checked="checked"' : ''; 
?>
<label for="_elist_featured" class="selectit">
<input name="_elist_featured" type="checkbox" id="_elist_featured" <?php echo esc_html( $featured_checked ); ?>>
<?php esc_html_e( 'This listing is featured.' ); ?>
</label>
</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM