• Resolved cpkid2

    (@cpkid2)


    Please check out my code. Basically, I want another
    < li > added when it’s on the “how-tos” page.

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2>Connect</h2>
    		<center>
    		<a title="Follow Us On Twitter!" href="https://www.twitter.com/socialmediate" style="margin-right: 10px;" target="_blank"><img alt="Follow Us On Twitter!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/twitter.png"/></a>
    		<a title="Add Us On Facebook!" href="https://www.facebook.com/pages/Social-Mediate/127797375765" target="_blank"><img alt="Add Us On Facebook!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/facebook.png"/></a>
    		</center>
    		</li>
    
    		<?php
    		if (is_page('how-tos')) { ?>
    		<li><h2>Previous How To's</h2>
    		<?php query_posts('cat=4'); ?>
    		</li>
    		<?php } ?>
    	</ul>
    </div>
Viewing 15 replies - 1 through 15 (of 18 total)
  • Try adding wp_reset_query(); to your code before you do the conditional.

    Thread Starter cpkid2

    (@cpkid2)

    That got the h2 element to show up but not the posts in category 4. Any ideas? Thanks for your help!

    Well this retreives the posts <?php query_posts('cat=4'); ?>
    but you need to create a loop to display the post info.

    You’ve used query_posts() incorrectly.

    First, query_posts() doesn’t display anything. It just queries the DB. You need to provide code to actually display the data.

    Second, query_posts() is meant to modify the main loop for the page, not for creating secondary loops. See the link above. Using query_posts() like this can under the right circumstances corrupt your main page loop. You are in fact, with or without wp_reset_query(), overwriting the main page loop. Be aware of that.


    This page
    has all the information you need to do what you are trying to do.

    Thread Starter cpkid2

    (@cpkid2)

    Thanks for the replies.

    @apljdi I tried following the instructions in the tutorial and came up w/ the following. It’s not working but I’m hoping you can give me some guidance.

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2>Connect</h2>
    		<center>
    		<a title="Follow Us On Twitter!" href="https://www.twitter.com/socialmediate" style="margin-right: 10px;" target="_blank"><img alt="Follow Us On Twitter!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/twitter.png"/></a>
    		<a title="Add Us On Facebook!" href="https://www.facebook.com/pages/Social-Mediate/127797375765" target="_blank"><img alt="Add Us On Facebook!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/facebook.png"/></a>
    		</center>
    		</li>
    
    		<?php
    		$prev = new WP_Query('cat=3');
    		$prev->query('showposts=10');
    		if ('is_page('features')');
    		echo('<li><h2>Previous Features</h2></li>');
    		while ($prev->have_posts()) : $prev->the_post(); ?>
    		<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
    		<?php endwhile; endif; ?>
    	</ul>
    </div>

    Your query works, actually. Put print_r($prev); right underneath it and load the page.

    I think your problem is here: if ('is_page('features')');. You shouldn’t have single quotes around the function, only around ‘features’. I can’t figure out why you didn’t get a syntax error with that, honestly. Also, what you want after the ‘if’ is a colon not a semi-colon. So, it should be:

    if (is_page('features')):
    		echo('<li><h2>Previous Features</h2></li>');
    		while ($prev->have_posts()) : $prev->the_post(); ?>
    		<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
    		<?php endwhile; endif; ?>

    Frankly, though I hate that alternate ‘if’ syntax. I never use it. It confuses me. I’d use brackets.

    if (is_page('features')){
    		echo('<li><h2>Previous Features</h2></li>');
    		while ($prev->have_posts()) : $prev->the_post(); ?>
    		<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
    		<?php endwhile; endif; ?>
    }
    Thread Starter cpkid2

    (@cpkid2)

    Thanks, I tried your code. However, I get an unexpected endif syntax error on line 23…

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2>Connect</h2>
    		<center>
    		<a title="Follow Us On Twitter!" href="https://www.twitter.com/socialmediate" style="margin-right: 10px;" target="_blank"><img alt="Follow Us On Twitter!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/twitter.png"/></a>
    		<a title="Add Us On Facebook!" href="https://www.facebook.com/pages/Social-Mediate/127797375765" target="_blank"><img alt="Add Us On Facebook!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/facebook.png"/></a>
    		</center>
    		</li>
    
    		<?php
    		$prev = new WP_Query('cat=3');
    		$prev->query('showposts=10');
    		print_r($prev);
    		if (is_page('features')){
    		echo('<li><h2>Previous Features</h2></li>');
    		while ($prev->have_posts()) : $prev->the_post(); ?>
    		<div class="spost"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
    		<?php endwhile; ?>
    		}
    	</ul>
    </div>

    Thread Starter cpkid2

    (@cpkid2)

    I realized the code above doesn’t include the endif. I must have taken it out at the last second. Anyway, this is the code.

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2>Connect</h2>
    		<center>
    		<a title="Follow Us On Twitter!" href="https://www.twitter.com/socialmediate" style="margin-right: 10px;" target="_blank"><img alt="Follow Us On Twitter!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/twitter.png"/></a>
    		<a title="Add Us On Facebook!" href="https://www.facebook.com/pages/Social-Mediate/127797375765" target="_blank"><img alt="Add Us On Facebook!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/facebook.png"/></a>
    		</center>
    		</li>
    
    		<?php
    		$prev = new WP_Query('cat=3');
    		$prev->query('showposts=10');
    		print_r($prev);
    		if (is_page('features')){
    		echo('<li><h2>Previous Features</h2></li>');
    		while ($prev->have_posts()) : $prev->the_post(); ?>
    		<div class="spost"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
    		<?php endwhile; endif; ?>
    		}
    	</ul>
    </div>

    Try this… (preferences for syntax aside)

    You can re-use query_posts, if you use reset or rewind posts appropriately, but the new query is suitable to.. (or get_posts would have sufficed to).

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2 style="text-align:center">Connect</h2>
    			<a title="Follow Us On Twitter!" href="https://www.twitter.com/socialmediate" style="margin-right: 10px;" target="_blank">
    				<img alt="Follow Us On Twitter!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/twitter.png"/>
    			</a>
    			<a title="Add Us On Facebook!" href="https://www.facebook.com/pages/Social-Mediate/127797375765" target="_blank">
    				<img alt="Add Us On Facebook!" src="https://www.socialmediate.com/wp-content/themes/socialmediate/images/facebook.png"/>
    			</a>
    		</li>
    
    		<?php if(is_page('features')) : ?>
    		<?php $prev = new WP_Query(); $prev->query('showposts=10&cat=3'); ?>
    
    		<li><h2>Previous Features</h2></li>
    		<li>
    			<?php while ($prev->have_posts()) : $prev->the_post(); ?>
    
    			<div class="spost">
    				<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    			</div>
    
    			<?php endwhile; ?>
    		</li>
    		<?php endif; ?>
    	</ul>
    </div>

    However, I get an unexpected endif syntax error on line 23…

    Right. That’s because I forgot to remove the endif. ??

    It should look more like this:

    if (is_page('features')){
    		echo('<li><h2>Previous Features</h2></li>');
    		while ($prev->have_posts()) : $prev->the_post(); ?>
    		<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
    		<?php endwhile;
    }

    Hopefully I didn’t screw it up again.

    You’d be better off moving the whole new query inside that IF… (as per the code i posted above).

    Else you’re creating the new query even when the condition is not met.

    Not meaning to shoot you in the foot here apljdi, but for the sake of encouraging better coding practices.. ??

    Thread Starter cpkid2

    (@cpkid2)

    @t310s_ I just tried your code. However, the “Previous Features” li didn’t show up on the ‘features’ page.

    Well i don’t see why not, did any code below that get output to the screen?

    Update that line.

    <li><h2>Previous Features</h2></li>

    to..

    <li><?php if(!$prev->have_posts()) { echo 'No posts found for the query'; } else { ?><h2>Previous Features</h2><?php } ?></li>

    If you don’t see that LI element whatsoever, then that indicates the..
    is_page('features') is not getting a match..

    Is that the correct spelling for that page name in that line?

    Thread Starter cpkid2

    (@cpkid2)

    Yes, that’s the correct spelling. The code still isn’t outputting the LI element. Here is my sidebar.php code. Maybe there is something else in here that you can catch although I followed exactly like you said.

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2 style="text-align:center">Connect</h2>
    			<a title="Follow Us On Twitter!" href="" style="margin-right: 10px;" target="_blank">
    				<img alt="Follow Us On Twitter!" src=""/>
    			</a>
    			<a title="Add Us On Facebook!" href="" target="_blank">
    				<img alt="Add Us On Facebook!" src=""/>
    			</a>
    		</li>
    
    		<?php if(is_page('features')) : ?>
    		<?php $prev = new WP_Query(); $prev->query('showposts=10&cat=3'); ?>
    
    		<li>
    		<?php if(!$prev->have_posts()) { echo 'No posts found for the query'; } else { ?><h2>Recent Features</h2><?php } ?></li>
    
    			<?php while ($prev->have_posts()) : $prev->the_post(); ?>
    
    			<div class="spost">
    				<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    			</div>
    
    			<?php endwhile; ?>
    		<?php endif; ?>
    		</li>
    	</ul>
    </div>

    Created a page called features…

    Plonk the code in my sidebar..

    Changed the category ID to match one i have…

    Result: 2 posts shown from the category (i’ve literally only got 10 posts total on the test install – only 2 in that cat)

    So it does work for me…

    Check the spelling of the page name (does the title have a capital F), check the category ID, is that definately right?..

    Before changing the cat ID, i saw the “No posts found” message i’d added in before (because i don’t have a category with the ID of 3 – as per code above).

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Variable Sidebar…why isn’t my code working?’ is closed to new replies.