做这件事最好的方法是什么?

时间:2019-02-11 作者:Lucas

<select name="select-city" onchange="location = this.value;">
<option value="">Select a post to read</option>
<?php
    if ( is_user_logged_in() ):
        global $current_user;
        wp_get_current_user();
        $author_query = array(\'posts_per_page\' => \'-1\',\'author\' => $current_user->ID);
        $author_posts = new WP_Query($author_query);
    $numposts = $count_user_posts( $user_id );
        while($author_posts->have_posts()) : $author_posts->the_post();
    ?>
<option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>
<?php  elseif ($numposts == 0): 
    echo \'You have no posts\';
    ?>
<?php endwhile;
    else :

        echo \'<a href="https://www.mysiteeeee.com/wp-login.php" >Log in</a>\';

    endif; ?>

你好,我正在努力让这一切顺利进行,但我不明白。

如果用户登录,我会得到用户发布帖子的下拉列表。如果没有帖子,我会收到消息“你没有帖子”。如果用户未登录,则会打印登录链接。

我不能用第二部分“你没有帖子”

我做错了什么?谢谢

2 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

让我们从正确缩进代码开始:

<select name="select-city" onchange="location = this.value;">
    <option value="">Select a post to read</option>
    <?php
        if ( is_user_logged_in() ):
            global $current_user;
            wp_get_current_user();
            $author_query = array(\'posts_per_page\' => \'-1\',\'author\' => $current_user->ID);
            $author_posts = new WP_Query($author_query);
            $numposts = count_user_posts( $user_id ); // <- you can\'t use $count_user_posts also - it\'s a function, not a variable
            while ( $author_posts->have_posts() ) :
                $author_posts->the_post();
    ?>
                <option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>
            <?php
                elseif ($numposts == 0) :  // <- here\'s the problem - there is an elseif, but there was no if earlier
                    echo \'You have no posts\';
            ?>
                    <?php endwhile;  // <- and here is another problem, because you use endwhile in elseif, but there was no while in that elseif...
                else :
                    echo \'<a href="https://www.mysiteeeee.com/wp-login.php" >Log in</a>\';
                endif; ?>
正如您所看到的,该代码显然存在一些错误,因为您无法缩进它。

那应该是什么样子?很难确定,但是。。。我很确定应该是这样的:

<?php if ( is_user_logged_in() ): ?>
    <?php
        global $current_user;
        wp_get_current_user();
        $author_query = array(\'posts_per_page\' => \'-1\',\'author\' => $current_user->ID);
        $author_posts = new WP_Query($author_query);
        $numposts = count_user_posts( $user_id );
        if ( $numposts ) :  // if there are posts, then show <select>
    ?>
        <select name="select-city" onchange="location = this.value;">
            <option value="">Select a post to read</option>
            <?php while ( $author_posts->have_posts() ) : $author_posts->the_post(); ?>
                <option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>
            <?php endwhile; ?>
        </select>
     <?php else : // if there are no posts ?>
         You have no posts
     <?php endif; ?>
<?php else : // you can\'t show links inside of select, so this is elseif for ( is_user_logged_in() ?>
    <a href="https://www.mysiteeeee.com/wp-login.php" >Log in</a>
<?php endif; ?>

SO网友:Fabrizio Mele

您错误地放置了endwhile 指示您的代码正在进入while循环并遇到elseif 外面if

当前代码大纲(伪代码):

if :
  while:
    elseif:
  endwhile;
else:
endif;

语法正确的代码大纲:

if :
  while:
    ...
  endwhile;
elseif:
else:
endif;

您必须移动此行:

<?php endwhile;
就在下面:

<option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>
HOWEVER 这根本行不通!while循环没有其他情况。我想你应该做一些类似的事情:

<select name="select-city" onchange="location = this.value;">
<option value="">Select a post to read</option>
<?php
    if ( is_user_logged_in() ):
        global $current_user;
        wp_get_current_user();
        $author_query = array(\'posts_per_page\' => \'-1\',\'author\' => $current_user->ID);
        $author_posts = new WP_Query($author_query);
        $numposts = $count_user_posts( $user_id );
        if ($numposts == 0) {
            echo \'You have no posts\';
        } else {

            while($author_posts->have_posts()) : $author_posts->the_post(); ?>
<option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>
<?php endwhile;
        }

    else :

        echo \'<a href="https://www.mysiteeeee.com/wp-login.php" >Log in</a>\';

    endif; ?>
并将while放在if-else中,检查用户是否有帖子。However 这不会完全奏效,因为echoa内ing<select> tag什么都做不了。取而代之的是,将select环绕在while周围,仅当有帖子时才打开它。