• Hello all,

    I’ve been struggling to create a second loop that would run on my home page (go see at https://www.htgp.org). I have the standard wordpress loop running lower on the index page, but its getting stuck on the loop I’m trying to create now.

    The goal for this loop is to have the last 5 posts from the ‘Slides’ Category show up in the formatted way I have, but I’m using custom fields to do this, and I’m doing the same as I did for my thumbnail system which you can see at: https://www.htgp.org/blog.

    but it’s obviously not working and giving me a parse error.

    I could really use some help on this… this is the code I have so far :…….

    <?php query_posts('category_name=blog&showposts=5');
    	$slide-image = get_post_meta($post->ID, 'Slide-img', $single = true);
    	$thumb-image_class = get_post_meta($post->ID, 'Slide-img Class', $single = true); ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post();?>
    
    <div id="slideshow">
    	<div id="slide-image-wrap">
        	<div class="slide-img">
        	<?php if($slide-image !== '') { ?>
                <a href="<?php the_permalink() ?>">
                	<img border="none" src="<?php echo $slide-image; ?>" class="<?php echo $slide-image_class; ?> slide-image" alt="<?php the_title(); ?>" />
            	</a>
            <?php }
                else { ?>
                	<a href="<?php the_permalink() ?>">
                    	<img border="none" src="https://www.htgp.org/wp-content/themes/HTGP/images/backgrounds/slide-default.png" class="left" alt="<?php the_title(); ?>" />
                    </a>
           	<?php } ?>
        </div>
        <div id="slide-text-wrap">
        	<div class="slide-title">
            	<a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
            </div>
            <div class="slide-excerpt">
                <?php the_excerpt(); ?>
    
    		</div>
        </div>
    </div>
    
    <?php endwhile; ?>
    <?php endif; ?>

    Now this is the complete loop I’ve made this far for generating the ‘slides’ category posts, and it does not work.

    Thanks in advance,
    Alex

Viewing 15 replies - 1 through 15 (of 15 total)
  • Use a new query, PHP example below…

    Using Query Posts will set the parameters for the main loop, so you need to use a new one…

    <?php $myquery = new WP_Query('category_name=yourcat'); ?>
    
    <?php while ($myquery->have_posts()) : $myquery->the_post(); ?>
    
    <?php endwhile;?>
    
    <?php wp_reset_query(); ?>

    Or perhaps you just need to reset the query… example above assumes you can manage adding the HTML bits in yourself… ??

    Thread Starter stompy1208

    (@stompy1208)

    thanks, but I still have the problem that I cannot define my variables… I don’t know if I did it right, but here’s what I got:

    <?php $myquery = new WP_Query('category_name=Slides'); ?>
    
    <?php while ($myquery->have_posts()) : $myquery->the_post();
    	$slide-image = get_post_meta($post->ID, 'Slide-img', $single = true);
    	$thumb-image_class = get_post_meta($post->ID, 'Slide-img Class', $single = true); ?>

    with all the code neccesary and it still gives me a “parse error, unexpected “=” on line 4…(which s the one that starts with “$slide-image”)

    $single = true

    Only need be

    TRUE

    Thread Starter stompy1208

    (@stompy1208)

    what’s that supposed to mean? I have the following code, and it still isn’t working:

    <?php $myquery = new WP_Query('category_name=Slides'); ?>
    
    <?php while ($myquery->have_posts()) : $myquery->the_post();
    	$slide-image = get_post_meta($post->ID, 'Slide-img', $single = TRUE);?>
    
    <div id="slideshow">
    	<div id="slide-image-wrap">
        	<div class="slide-img">
        	<?php if($slide-image !== '') { ?>
                <a href="<?php the_permalink() ?>">
                	<img border="none" src="<?php echo $slide-image; ?>" class="slide-image" alt="<?php the_title(); ?>" />
            	</a>
            <?php }
                else { ?>
                	<a href="<?php the_permalink() ?>">
                    	<img border="none" src="https://www.htgp.org/wp-content/themes/HTGP/images/backgrounds/slide-default.png" class="left" alt="<?php the_title(); ?>" />
                    </a>
           	<?php } ?>
        </div>
        <div id="slide-text-wrap">
        	<div class="slide-title">
            	<a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
            </div>
            <div class="slide-excerpt">
                <p><?php the_excerpt(); ?></p>
    		</div>
        </div>
    </div>
    
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

    Could you just tell me what I should have? or exactly what to change because this is starting to annoy me :).

    t31os did provide you with a reasonable solution to try. Replace the entire phrase/parameter:

    $single = true

    with the single word

    TRUE

    Yes, sorry just replied quickly last time…

    This line…

    $slide-image = get_post_meta($post->ID, 'Slide-img', $single = TRUE);

    becomes…

    $slide-image = get_post_meta($post->ID, 'Slide-img', TRUE);

    Works out to be line 4 also… so hopefully that’s the issue…

    Let us know if that worked…

    Thread Starter stompy1208

    (@stompy1208)

    nope… still not working…
    Is it even possible to define variables in two different loops?
    should I try defining the $slide-image in my “standard” loop lower on the page?
    Is there another way to define them, because the parse error has a problem with the “=” sign in the line…this is complicated.

    Thread Starter stompy1208

    (@stompy1208)

    looks like it works now, but its not showing the images…. arggg..

    I changed the “$slide-image” to “$slide_image” after inserting the old way into my regular loop and it having the same problem, so apparently no “-” can be used in variables??? ??

    Still having a problem though… ??

    Thread Starter stompy1208

    (@stompy1208)

    looks like I got it fixed!!! FINALLY!!!
    now I’ve got to fix the CSS… A LOT!! ??

    thanks for the help,

    Alex

    Thread Starter stompy1208

    (@stompy1208)

    oh.. by the way… final code =

    <?php query_posts('category_name=Slides');
    	   $slide_image = get_post_meta($post->ID, 'Slide-img', TRUE); ?>
    
    <div id="slideshow">
    	<div id="slide-image-wrap">
        	<div class="slide-img">
        	<?php if($slide_image !== '') { ?>
                <a href="<?php the_permalink() ?>">
                	<img border="none" src="<?php echo $slide_image; ?>" class="slide-image" alt="<?php the_title(); ?>" />
            	</a>
            <?php }
                else { ?>
                	<a href="<?php the_permalink() ?>">
                    	<img border="none" src="https://www.htgp.org/wp-content/themes/HTGP/images/backgrounds/slide-default.png" class="left" alt="<?php the_title(); ?>" />
                    </a>
           	<?php } ?>
        </div>
        <div id="slide-text-wrap">
        	<div class="slide-title">
            	<a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
            </div>
            <div class="slide-excerpt">
                <p><?php the_excerpt(); ?></p>
    		</div>
        </div>
    </div>
    
    <?php wp_reset_query(); ?>

    Thanks again

    Thread Starter stompy1208

    (@stompy1208)

    Looks like I spoke a little too fast fellas…. :((((

    I have the whole thing working, except for the fact that only ONE post shows up… :??

    I don’t have the CSS ready for it to look good with multiple posts, but the following code:

    <?php query_posts('category_name=Slides&showposts=5');
    	   $slide_image = get_post_meta($post->ID, 'Slide-img', TRUE); ?>
    
    <div class="slide">
    	<div class="slide-image-wrap">
        	<div class="slide-img">
        	<?php if($slide_image !== '') { ?>
                <a href="<?php the_permalink() ?>">
                	<img border="none" src="<?php echo $slide_image; ?>" class="slide-image" alt="<?php the_title(); ?>" />
            	</a>
            <?php }
                else { ?>
                	<a href="<?php the_permalink() ?>">
                    	<img border="none" src="https://www.htgp.org/wp-content/themes/HTGP/images/backgrounds/slide-default.png" class="left" alt="<?php the_title(); ?>" />
                    </a>
           	<?php } ?>
        	</div>
        </div>
        <div class="slide-text-wrap">
        	<div class="slide-title">
            	<a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
            </div>
            <div class="slide-excerpt">
                <p><?php the_excerpt(); ?></p>
    		</div>
        </div>
    </div>
    
    <?php wp_reset_query(); ?>

    shows only 1(the latest) post in the “Slides” category. All three posts shown lower on the page, I have associated with the “slides” category, but only one shows up… ??? this should be the last problem… i hope… ??

    Where’s the while loop gone?

    The query can’t loop through each result without it.

    Thread Starter stompy1208

    (@stompy1208)

    oohh … ok thanks, I’ll try that when I get home.

    Thread Starter stompy1208

    (@stompy1208)

    looks like it worked perfectly guys (or girls)… thanks a bunch!

    Glad you got it sorted mate.. ??

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘WordPress query_post setting variables’ is closed to new replies.