JordanBarber
JordanBarber

Reputation: 2101

Trouble with query_posts inside of if statement

Having some trouble with a wordpress blog. I am trying to show posts with specific categories based on the section of the site the user came from. Everything is working except the 'query_posts' inside of the the if/else statement. I have the following php:

<?php
/*
Template Name: Blog
*/
get_header(); ?>


<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer(); 
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
    $text = 'test';
    query_posts('cat=experienced-professionals');
    // wp_reset_query();
} else {
    $text = 'heyo';
    query_posts('cat=college-students');
    // wp_reset_query();
}
?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div style="background:red;width:100%;height:200px;"></div>

<div id="container">
    <div id="content" role="main">

        <h1 class="entry-title"><?php the_title(); ?></h1>
        <p><?php echo $text; ?></p>
        <?php print_r($came_from); ?>
    </div><!-- #content -->
</div><!-- #container -->

<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->

<?php wp_reset_query(); ?>

<?php get_footer(); ?>

I know my referer variable and if/else statement are working because the $text variable changes as it should based on the section of the site I get to the blog from. However no matter how I get to the blog, the page is showing all posts and ignoring the query_posts category inside the if/else statment. Can someone please help?

Upvotes: 0

Views: 138

Answers (1)

JordanBarber
JordanBarber

Reputation: 2101

Nevermind figured this out...hope it helps someone else! Fixed it with this:

<?php
/*
Template Name: Blog
*/
get_header(); ?>


<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer(); 
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
    $text = 'test';
    $queryCategory = 'experienced-professionals';
    // wp_reset_query();
} else {
    $text = 'heyo';
    // query_posts('cat=college-students');
    $queryCategory = 'college-students';
    // wp_reset_query();
}
?>

<?php query_posts( array ( 'category_name' => $queryCategory ) ); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div style="background:red;width:100%;height:200px;"></div>

        <div id="container">
            <div id="content" role="main">

                <h1 class="entry-title"><?php the_title(); ?></h1>
                <p><?php echo $text; ?></p>
                <p><?php echo $queryCategory; ?></p>
                <?php print_r($came_from); ?>
            </div><!-- #content -->
        </div><!-- #container -->

<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->

<?php wp_reset_query(); ?>

<?php get_footer(); ?>

Upvotes: 1

Related Questions