count - What is the best way to do this?

admin2025-06-03  2

Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 6 years ago.

Improve this question
<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=".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. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 6 years ago.

Improve this question
<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/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.

Share Improve this question edited Feb 12, 2019 at 17:45 Lucas asked Feb 11, 2019 at 21:32 LucasLucas 133 bronze badges 1
  • 2 This would be much easier to see why this happens if the code was indented correctly – Tom J Nowell Commented Feb 11, 2019 at 21:41
Add a comment  | 

2 Answers 2

Reset to default 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/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/wp-login.php" >Log in</a>
<?php endif; ?>

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/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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748915224a314779.html

最新回复(0)