Make WordPress Core

Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#14355 closed defect (bug) (invalid)

is_front_page returns false - if used after the loop starts

Reported by: christian_gnoth's profile christian_gnoth Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: General Keywords: reporter-feedback
Focuses: Cc:

Description

hello,
I am using is_front_page() on different places in my theme template files.

It returns for the front page and home page true if I call it inside the loop - if I call it before the loop starts ( if(have_posts()) ) it returns false for the front page. it also returns false if I call it in my functions.php file in a customized excerpt action hook.

Change History (9)

#1 @scribu
14 years ago

  • Keywords reporter-feedback added; is_front_page loop return false removed

Can't reproduce. Are you using query_posts() by any chance?

#2 @christian_gnoth
14 years ago

sorry, it was written wrong - it returns outside true and inside false. and I am using query_posts.

Problem is, that I am showing on my front page an excerpt and I have a customized excerpt function - in that customized excerpt function I am testing if it is front page or not and return different content. (http://wp-theme-t1.it-gnoth.de)

if query posts is teh reason - it should be possible to show different posts on the front page and still the function returns true.

#3 @christian_gnoth
14 years ago

  • Severity changed from normal to major

#4 @nacin
14 years ago

  • Severity changed from major to normal

Please post a snippet of code that reproduces this on any install.

#5 @filosofo
14 years ago

  • Summary changed from is_front_page returns fasle - if used before the loop starts to is_front_page returns false - if used before the loop starts

#6 @christian_gnoth
14 years ago

<?php

$biq_theme_options = biq_get_theme_options();

get_header('xajax');

global $xajax;

//$xajax = new xajax();
$xajax->registerFunction('load_latest_post');

$xajax->processRequest();

echo
'<div id="main_content_container">' . "\n" .
'  <div id="content_container">' . "\n";

if ( is_home() )
{
}

var_dump(is_front_page());

if ( is_front_page() )
{
  // check for referer and show two latest posts excerpts
  //biq_check_referer();
  query_posts(array( 'posts_per_page' => '2', 'category_name' => 'Blog Post' ));

  echo '  <div id="latest_post_container">' . "\n";
  echo '    <div id="latest_post">' . "\n";

  if ( have_posts() )
  {
    while(have_posts()) 
    { 
      the_post();
      $biq_id = get_the_ID();
      echo '    <div ';
      post_class();
      echo ' id="co-post-' . get_the_ID() . '">' . "\n";
      $result = is_front_page();
      var_dump($result);
      echo 'index.php:' . is_front_page() . ':<br />' . "\n";
      echo '    <h3><a href="' . get_permalink() . '" rel="bookmark" onclick="xajax_load_latest_post(\'' . get_the_ID() . '\'); return false;" title="Permalink: ' . the_title( '', '', false) . '">' . the_title( '', '', false) . '</a></h3>' . "\n";
//      echo '    <h3><a href="javascript:xajax_load_latest_post(' . get_the_ID() . ')" rel="bookmark" onclick="return !xajax_load_latest_post(' . get_the_ID() . ');" title="Permalink: ' . the_title( '', '', false) . '">' . the_title( '', '', false) . '</a></h3>' . "\n";
//      echo '    <h3><a href="#" rel="bookmark" onclick="xajax_load_latest_post(\'' . get_the_ID() . '\');" title="Permalink: ' . the_title( '', '', false) . '">' . the_title( '', '', false) . '</a></h3>' . "\n";
//      echo '    <h3><a href="javascript:xajax_load_latest_post(\'' . $biq_id . '\')" rel="bookmark" title="Permalink: ' . the_title( '', '', false) . '">' . the_title( '', '', false) . '</a></h3>' . "\n";
      the_excerpt();
      echo '    </div>' . "\n";
    }
  }
  wp_reset_query();

  echo '    </div>' . "\n";
  echo '  </div>' . "\n";
}


$biq_amazon_item_list_search = NULL;
$biq_amazon_items_per_page   = NULL;
$biq_amazon_item_list_page   = NULL;

//  process submitted data
//  SORT Order 
if ( $_POST['biq_amazon_item_list_search'] )
{
  $biq_amazon_item_list_search = $_POST['biq_amazon_item_list_search'];
}

//  number of items to display
if ( $_POST['amazon_items_ppage'] )
{
  $biq_amazon_items_per_page = intval($_POST['amazon_items_ppage']);
  $_SESSION['amazon_items_ppage'] = $biq_amazon_items_per_page;
}
if ( !$biq_amazon_items_per_page )
{
  if ( $_SESSION['amazon_items_ppage'] )
    $biq_amazon_items_per_page = $_SESSION['amazon_items_ppage'];
  else
    $biq_amazon_items_per_page = 9;
}


//  page number
if ( $_POST['biq_amazon_item_list_page'] )
{
  $biq_amazon_item_list_page = intval($_POST['biq_amazon_item_list_page']);
}
if ( !$biq_amazon_item_list_page OR isset($_POST['biq_amazon_item_list_page_first']))
  $biq_amazon_item_list_page = 1;

//  call to get product items
$query = 'SELECT * FROM amazon_attributes;';
$biq_amazon_product_list = $wpdb->get_results($query);

if ( $biq_amazon_product_list )
{
  $biq_amazon_product_list_count = count( $biq_amazon_product_list );

  echo '  <div id="content">' . "\n";
  echo '    <div id="amazon_item_list_header">' . "\n";
  echo '      <div id="amazon_items_search">' . "\n";
  echo '        <b>SORT BY</b> ' . "\n";
  echo '        <form class="form_items_search" action="' . $_SERVER['PHP_SELF'] . '" method="post">' . "\n";
  echo '          <select name="biq_amazon_item_list_search" size="1">' . "\n";
  echo '            <option><a href="">LOW PRICE</a></option>' . "\n";
  echo '            <option><a href="">HIGH PRICE</a></option>' . "\n";
  echo '            <option><a href="">DISCOUNT</a></option>' . "\n";
  echo '          </select>' . "\n";
  echo '        </form>' . "\n";
  echo '      </div>' . "\n";
  echo '      <div id="amazon_items_count">' . "\n";
  echo '        ' . $biq_amazon_items_per_page . ' of ' . $biq_amazon_product_list_count . ' SHOES' . "\n";
  echo '      </div>' . "\n";
  echo '      <div id="amazon_items_ppage">' . "\n";
  echo '        <form class="form_items_ppage" method="post" action="">Items per Page <a href=""><input type="submit" class="input_items_ppage" name="amazon_items_ppage" value="9" /></a> | </form>' . "\n";
  echo '        <form class="form_items_ppage" method="post" action=""><a href=""><input type="submit" class="input_items_ppage" name="amazon_items_ppage" value="18" /></a> | </form>' . "\n";
  echo '        <form class="form_items_ppage" method="post" action=""><a href=""><input type="submit" class="input_items_ppage" name="amazon_items_ppage" value="27" /></a> | </form>' . "\n";
  echo '      </div>' . "\n";
  echo '    </div>' . "\n";

  $biq_amazon_page_count = intval($biq_amazon_product_list_count / $biq_amazon_items_per_page);
  if ( ($biq_amazon_page_count * $biq_amazon_items_per_page) < $biq_amazon_product_list_count )
    $biq_amazon_page_count++;
  if ( isset($_POST['biq_amazon_item_list_page_last']) )
    $biq_amazon_item_list_page = $biq_amazon_page_count;
  $biq_item_counter = $biq_amazon_items_per_page * ($biq_amazon_item_list_page - 1) + 1;
  $biq_amazon_product_list_page_rest = $biq_amazon_product_list_count % $biq_amazon_items_per_page;
  $biq_amazon_page_row_count = $biq_amazon_items_per_page / 3;

  for ( $index = 0; (($index < $biq_amazon_page_row_count)); $index++)
  {
    echo '  <div id="amazon_item_row">' . "\n";
    for ( $i = 0; (($i < 3) AND ( ($biq_item_counter <= $biq_amazon_product_list_count) )); $i++)
    {
      echo '    <div id="amazon_item_field">' . "\n";
      echo '      <div id="amazon_item_img">' . "\n";
      echo '        <a href="' . $biq_amazon_product_list[$biq_item_counter]->detail_page_url . '" title="' . $biq_amazon_product_list[$biq_item_counter]->title . '" target="_blank">';
      echo '<img src="' . $biq_amazon_product_list[$biq_item_counter]->medium_image_url . '" alt="' . htmlentities($biq_amazon_product_list[$biq_item_counter]->prod_desc) . '" />';
      echo '</a>' . "\n";
      echo '      </div>' . "\n";
//      echo '      <div class="item_seperator"></div>' . "\n";
      echo '      <div id="amazon_item_description">' . "\n";
      echo '        <a href="' . $biq_amazon_product_list[$biq_item_counter]->detail_page_url . '" title="' . $biq_amazon_product_list[$biq_item_counter]->title . '" target="_blank">' . $biq_amazon_product_list[$biq_item_counter]->title . '</a><br />' . "\n";
      echo $biq_amazon_product_list[$biq_item_counter]->brand . '<br />' . "\n";
      if ( ($biq_amazon_product_list[$biq_item_counter]->pct_off != NULL) AND ($biq_amazon_product_list[$biq_item_counter]->pct_off != 0))
      {
        echo $biq_amazon_product_list[$biq_item_counter]->formatted_price . '<br />' . "\n";
        echo $biq_amazon_product_list[$biq_item_counter]->display_price . ' (' . ($biq_amazon_product_list[$biq_item_counter]->pct_off * 100) . '% off)' . "\n";
      }
      else
      {
        echo $biq_amazon_product_list[$biq_item_counter]->display_price . '<br />' . "\n";
      }
      echo '      </div>' . "\n";
      echo '    </div>' . "\n";
      $biq_item_counter++;
    }
    if ( $index < ($biq_amazon_page_row_count - 1))
      echo '    <div class="seperator"></div>' . "\n";
    echo '  </div>' . "\n";
  }

  echo '    <div id="amazon_item_list_footer">' . "\n";
  echo '      <div id="amazon_item_list_footer_link">' . "\n";
  echo '        <a href="#top">Back To Top</a>' . "\n";
  echo '      </div>' . "\n";
  echo '      <div id="amazon_item_list_footer_nav">' . "\n";
  echo '        <form class="form_item_list_page" method="post" action=""><a href=""><input type="submit" class="input_item_list_page_first" name="biq_amazon_item_list_page_first" value="" /></a> </form>' . "\n";
  if ( $biq_amazon_item_list_page > 10 )
    $biq_list_page_start = $biq_amazon_item_list_page - 5;
  else
    $biq_list_page_start = 1;
  for ( $i=$biq_list_page_start; (($i <= $biq_amazon_page_count) AND ($i <= ($biq_list_page_start + 10))); $i++ )
  { 
    if ( $i == $biq_amazon_item_list_page )
      echo '<form class="form_item_list_page" method="post" action=""><a href=""><input type="submit" class="input_item_list_page active" name="biq_amazon_item_list_page" value="' . $i . '" /></a>  |  </form>' . "\n";
    else
      echo '<form class="form_item_list_page" method="post" action=""><a href=""><input type="submit" class="input_item_list_page nonactive" name="biq_amazon_item_list_page" value="' . $i . '" /></a>  |  </form>' . "\n";
  }
  echo '        <form class="form_item_list_page" method="post" action=""><a href=""><input type="submit" class="input_item_list_page_last" name="biq_amazon_item_list_page_last" value="" /></a> </form>' . "\n";
  echo '      </div>' . "\n";
  echo '    </div>' . "\n";

  echo '	</div>' . "\n";
}
else
{
  echo '  <p>' . __('Sorry, no products available.', 'biq') . '</p>' . "\n";
}

echo '	</div>' . "\n\n";

get_sidebar();

get_footer();

?>

#7 @christian_gnoth
14 years ago

  • Summary changed from is_front_page returns false - if used before the loop starts to is_front_page returns false - if used after the loop starts

#8 @nacin
14 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

I can't reproduce anything with that code per se, but it is obvious why is_front_page is returning two different values. Between calls, you call query_posts(), which resets the query object and changes all of the conditionals to reflect the new query.

If you don't want to alter things that low-level, then using get_posts or a new WP_Query will suffice.

Also, if you're going to use $_SERVER['PHP_SELF'], better not to use it, but if you do, then make sure you escape it. http://markjaquith.wordpress.com/2009/09/21/php-server-vars-not-safe-in-forms-or-links/

#9 @azizur
14 years ago

  • Cc azizur added
Note: See TracTickets for help on using tickets.