• Resolved kenbowen

    (@kenbowen)


    On the site esl-voices.com (WP 5.2.4; PHP 7.3.11), we use category ‘Guest Plan’ (‘cat’=> 487) for contributed posts, which are displayed separately from the standard Blog, using the menu entry ‘Guest Plans’. Currently, pressing menu entry ‘Guest Plans’ successfullly activates the following Loop[2]:

    <?php
        $temp_post = $post; // Storing the object temporarily
        $args = array('cat'=> 487);
        $the_query = new WP_Query($args);
    
        // The Loop[2]
        if($the_query->have_posts()) :
        while($the_que1Gry->have_posts()) :
            $the_query->the_post();
            get_template_part('content', get_post_format()); 
        endwhile;
        $post = $temp_post; // Restore the value of $post to the original
    ?>

    We would like to create a table of contents at the beginning of the display.
    I’ve attempted to begin building this by prefixing a preliminary Loop[1] (copied from the Standard Loop on
    https://developer.www.ads-software.com/reference/classes/wp_query/)
    which would extract the titles of the posts, as follows:

    <?php
        $temp_post = $post; // Storing the object temporarily
        $args = array('cat'=> 487);
        $the_query = new WP_Query($args);
    
        // The Loop[1]
        if($the_query->have_posts()) :
        echo '<ul>';
        while ( $the_query->have_posts()) :
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        endwhile;
        echo '</ul>';
    
        // The Loop[2]
        if($the_query->have_posts()) :
        while($the_query->have_posts()) :
            $the_query->the_post();
            get_template_part('content', get_post_format()); 
        endwhile;
        $post = $temp_post; // Restore the value of $post to the original
    ?>

    However, when I do this, pressing ‘Guest Plans’ produces the output:
    The site is experiencing technical difficulties.
    If I comment out Loop[2], I get the same result. This is surprising since Loop[1] is copied from https://developer.www.ads-software.com/reference/classes/wp_query/.
    Can anyone give me hand here?
    Thanks in advance.

    • This topic was modified 5 years ago by bcworkz.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to call wp_reset_postdata(); in between the loops so that the second loop can start from the beginning. You also need a corresponding endif; for every if ({some condition}) : statement. You didn’t copy everything from that example ??

    When you post code in these forums, please demarcate with backticks or use the code button. If don’t do this the forum’s parser corrupts your code.

    Thread Starter kenbowen

    (@kenbowen)

    Sadly, examining https://developer.www.ads-software.com/reference/classes/wp_query/ carefully, there is no ‘endif;’ to be found, because it is written in the ‘old’ PHP syntax: if { } else { }. I translated that to the “new” syntax following the model of Loop[2]. Notice that Loop[2] runs correctly, but does not contain the element ‘endif’. But clearly, I did something wrong: Copying the lines 6-18 from Standard Loop in Usage in https://developer.www.ads-software.com/reference/classes/wp_query/ gave me the behavior I desired — an unordered list of titles at the beginning.
    Thanks!
    Additionally, thanks for the backticks tip, and for fixing my original post.
    Where on this page should I see the Code button? (I’m on FF right now, but can’t see it on Chrome either.)

    Moderator bcworkz

    (@bcworkz)

    FWIW, the WP_Query doc’s second example (standard loop (alternate)) does have endif.

    I’ve no explanation how your code could work without endif. I just tried it to double check and it throws white screen errors. In any case it’s still incorrect even if it does work for you.

    Code button is in the topic/reply editor’s toolbar, second from the right. It doesn’t look like a conventional “button”, but it acts like one.

    Thread Starter kenbowen

    (@kenbowen)

    Not being argumentative, just confused. Here’s the select of what I see on the screen for WP_Query doc:

    
    #Usage
    #Standard Loop
    
    <?php
     
    // The Query
    $the_query = new WP_Query( $args );
     
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    Collapse full source code
    

    I think it works because of the if { } else { } structure.
    Cheers,
    –Ken

    Moderator bcworkz

    (@bcworkz)

    Argument? I don’t see any argument. I’m happy to contribute to any learning experience ??

    There are two different loop examples. You used the first one in your last reply, but in your OP you apparently copied the second example.

    There are two kinds of block delimiters in PHP. The curly brace:

    if ( $condition ) {
      //do something
    }

    and the colon end*; syntax:

    if ( $condition ) :
      //do something
    endif;

    There are some limitations to nesting the colon style, but otherwise the choice of which to use is mostly stylistic. I generally prefer the curly brace style, but the colon style is good for very large blocks of code where the corresponding curly brace would be hard to find when reading the code.

    I almost didn’t bother pointing out the endif when I replied “FWIW…”. I wasn’t sure if it was even worth being pedantic about. I’m glad I did if it leads to your better understanding of PHP.

    Thread Starter kenbowen

    (@kenbowen)

    Thanks! Hope you’re having a great day.

    Moderator bcworkz

    (@bcworkz)

    No problem, same to you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Trying to modify THE LOOP to create a TOC for a category’ is closed to new replies.