1

I am trying to get a function to get three posts and print/echo 1st post with class active and rest of the post will be Normal.

I tried this code in functions.php to generate and didn't achieve the result.

function slider_news() {
        $args = array(
      'numberposts' => 3
    );
            $latest_posts = get_posts($args);   

            foreach ($latest_posts as $post) {
            $num    = 0;
            $newnum = $num + 1; 
            $num    = $newnum;

            if ($num =1) {

                echo 'Post with <div class="active">1st Post </div ';

                $newnum = $num + 1; 

            }


            else{

                echo 'Post with <div class="Normal">All other posts except 1st</div>';

                }
                                            }
                                }       

Source Code i got from this function is below

Post with <div class="active">1st Post </div>
Post with <div class="active">1st Post </div>
Post with <div class="active">1st Post </div>

closed as off-topic by Michael, Nathan Johnson, Jacob Peattie, Krzysiek Dróżdż, Pat J Apr 16 '18 at 16:15

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.

  • 1
    This is more a programming question than a WordPress question, but your code sets the variable $num as always equal to 1 prior to the IF conditional, which will therefore always be true. Without commenting on whether this is really the best way to about what you're seeking to achieve or preferred way to write the operation: Try initializing $num BEFORE/OUTSIDE the foreach, equal to 1 ( $num = 1;). You won't need any other variables. Just $num = $num + 1 (or $num++) AFTER the foreach, prior to the IF conditional. On the second trip through, $num will = 2, and the else will execute. – CK MacLeod Apr 14 '18 at 16:32
  • Thank You @CKMacLeod and can you please help me with one more thing, how could i use html double quotes along with wordpress php variables without the syntax error! .I am having a lot of Confusion on Combining the Html " quotes and the " quotes required to print the Variable. Thanks A Lot! – Nimesh Apr 14 '18 at 16:46
  • 2
    Also, I made a mistake cramming that answer into the space above. You should either initialize $num as equal to 0- or "increment" it AFTER the conditional. – CK MacLeod Apr 14 '18 at 16:53
  • Your Idea already worked! Thanks. I need a bit more help ! Can we chat in stackexchange. – Nimesh Apr 14 '18 at 17:07
  • 2
    I really have to get on with my day. These are elementary questions with answers and examples abundantly available on-line. Aside from finding the answers to your specific questions, reading through the documentation will prepare you for later ones! (That's how I learned, and how I think most of us do.) If that doesn't work for you, you might have to pay someone for tutoring. Good luck! – CK MacLeod Apr 14 '18 at 17:17
3

As I noted in the comments, this is more a programming language question than a WordPress Development question, and that goes for the second question, too. However, instead of adding to comment discussion and making more mistakes, I'll write out an answer.

function slider_news() {

    $num = 0;
    $args = array( 'numberposts' => 3 );

    $latest_posts = get_posts( $args );   

    foreach ( $latest_posts as $post ) {

        $num++; //PHP increment operator ++ (add 1 to value)

        if ( 1 === $num ) { //using a single equals as in original would set the value, === is strict equivalence

            echo 'Post with <div class="active">1st Post</div>';
            //example was missing final ">"

        } else {

            echo 'Post with <div class="Normal">All other posts except 1st</div>';

        }

    }

}

I don't see any problem in the above with alternating between single and double quotes. As long as you you are consistent with using single quotes to set off strings, you can use double quotes within them however you like, but you will need to escape single quotes meant to be printed. For more, I suggest you read up on PHP syntax. There aren't any special WordPress exceptions to be concerned with.

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