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

Hello there, I'm trying to make this work but I don't get it.

If user is logged in I get a form dropdown of the posts the user has published. If it has no posts I get the message "You have no posts". And if user is not logged in a link to log in prints.

I don't get it to work with the second part "You have no posts"

What am I doing wrong? Thanks.

closed as off-topic by fuxia Feb 12 at 0:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    This would be much easier to see why this happens if the code was indented correctly – Tom J Nowell Feb 11 at 21:41
1

Let's start with proper indenting your code:

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

As you can see there clearly is something wrong with that code, because you can't indent it.

So how should that look? It's hard to be certain, but... I'm pretty sure it should be something like this:

<?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; ?>
  • Not working, but I'm kind of getting things – Lucas Feb 11 at 22:00
  • One more problem found - you've used $count_user_posts, but it's a function, not a variable... – Krzysiek Dróżdż Feb 11 at 22:02
  • 1
    too much... i'm trying to read without knowing the alphabet... continue tomorrow. Thanks – Lucas Feb 11 at 22:10
0

You have incorrectly placed your endwhile instruction. You code is entering the while-loop and encountering the elseif outside the if block.

Current code outline (pseudocode):

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

Syntactically correct code outline:

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

You would have to move this line:

<?php endwhile;

just below:

<option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>

HOWEVER this will simply not work! The while loop has no else case. I think you want to do something like:

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

and place the while inside an if-else checking whether the user has posts. However this won't fully work, because echoing inside a <select> tag won't do nothing. Instead, wrap the select just around the while,open it only if there are posts.

  • Sorry, but this answer is simply wrong. You can't use endwhile: – Krzysiek Dróżdż Feb 11 at 21:44
  • yeah sorry, typo. editing – Fabrizio Mele Feb 11 at 21:44
  • Trying to make it work with your indications... hard for me :) – Lucas Feb 11 at 21:52
  • try copy-pasting the code I just added at the bottom of the answer, I've fixed the indentation so you can better understand the code flow – Fabrizio Mele Feb 11 at 21:54
  • 1
    Sorry, but it won't work. You're printing 'You have no posts'; inside of select - it doesn't make much sense, I'm afraid... – Krzysiek Dróżdż Feb 11 at 21:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.