0

我需要在我的 wordpress 代码中插入一个“if”控件,因为我希望这个复选框只出现在特定用户身上。但我总是收到一个错误。

<?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> 

    } ?>
4

2 回答 2

2

您需要正确打开和关闭IF语句周围的 PHP 标记和右花括号。?>您当前的代码在第一行的末尾和<?php最后一行的开头缺失

不正确:

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

正确的:

<?php if ( !current_user_can('edit_users') ) { ?>
<-- your HTML and PHP -->
<?php } ?>
于 2013-05-24T23:33:52.720 回答
0

您在代码中使用了其他<?php标签。

仅供参考-尝试使用好的编辑器来修复这些有点错误

<?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> 
于 2013-05-24T23:39:23.207 回答