• Resolved dorich

    (@dorich)


    I have a glossary page in which the a reader can click on a letter to see a list of posts that start with the selected letter.

    The selection process works as expected but a member of the forum suggested checking for an occurence where a value was not passed.

    My code for this is

    <?php $letter = $_GET['letter'];
    if ($letter=="") echo "No Entries Found!";
    else
    $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => $letter, 'posts_per_page' => 10 ) ); ?>

    This returns the expected text on the page if I artificially create a url without a value.

    However, there is also an error message from WP, thus:

    Fatal error: Call to a member function have_posts() on a non-object in /XXXXXXXXXXXXXX/wp-content/themes/twentyten/glossaryListLetter2v0.php on line 14

    I’m looking for guidance on whether I can code this in such a way as to avoid the error message from WP.
    Sidenote: I’m less than a novice in php.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The easiest thing to do is default to the letter ‘A’ if none is supplied:

    <?php $letter = $_GET['letter'];
    if ($letter=="") $letter = 'A';
    $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => $letter, 'posts_per_page' => 10 ) ); ?>
    Thread Starter dorich

    (@dorich)

    vtxyzzy

    Thanks.

    I was assuming that this needed a “If then else” statement but apparently not.

    Your solution works perfectly.

    Much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Testing for Values Passed in URL’ is closed to new replies.