• I am having 2 problems:

    1. the <!– more link –> does not work on my homepage. I have a separate template for my homepage (home.php). I have tried adding <?php global $more; $more = 0;?> and it still won’t work. Are there any known issues in 2.8.4?

    2. In my content loop I am trying to display an hr element after each entry, except the last one. I figure it needs to be something like this:
    <?php if((get($current_post))<(get($post_count))){echo("<hr/>");} ?> though I realize the syntax is probably way off — I am a somewhat new to php, but familiar with loops in general — any help would be much appreciated 🙂

    Thank you

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter mattyscribs

    (@mattyscribs)

    I have since found this link which works for me:

    http://www.texto.de/how-to-pages-that-display-all-posts-from-x-category-522/

    which uses this query:

    <?php rewind_posts();
    $my_query = new WP_Query('category_name=articles&showposts=2');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    global $more;
    $more = 0; ?>

    .. which appears to be the old way when I would rather use this way:

    $posts=get_posts('category_name=articles&showposts=2');
    foreach($posts as $post) {
    setup_postdata($post);

    1. No issues that I’ve seen. Where are you placing the <?php global $more; $more = 0;?> within the Loop?

    2. Assuming that the template doesn’t modify the Loop using query_posts, you should be able to grab the number of posts per page using $per_page = echo get_option( 'posts_per_page' );. So, if you set up a counter variable before the Loop ($c = 0) and increment this each time the Loop runs, you can compare the value of $c to $per_page and determine whether to output <hr />.

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php $c++;?>
    
    [ do the usual Loop stuff ]
    
    <?php if( $c < $per_page ):?>
    <hr />
    <?php endif;?>
    
     <?php endwhile; ?>
     <?php else : ?>
    <?php endif;?>

    Or at least that’s how it should work in theory…

    Thread Starter mattyscribs

    (@mattyscribs)

    thanks esmi — well I am new at all this.. I have managed to get things working thanks to your advice. One of my main issues was that I was trying to make all this happen on a static page, so I read up on that stuff and it helped. Finally, I played around and poked and got things to work the way I wanted. Here is what I came up with – this is for a static homepage which displays categorized posts in different places on the page, with the more tag working and your ‘islast’ type test to help with display.

    First, set the global $more variable at the top of the page, under the header include:
    <?php global $more;?>

    Then this will display the first looped content for my blog category, straight-forward enough, I just want to display the latest blog post here. Note how the ‘$more’ variable is referenced – this will be the same for all instances:

    <?php query_posts('category_name=blog&showposts=1'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" class="title"><?php the_title(); ?></a><br />
    	<small><?php the_time('F jS, Y') ?></small>
    	<?php
    		$more = 0;
    		the_content("<b>Read More &raquo;</b>");
    	?>
    <?php endwhile; else: endif; ?>

    …And then to display may articles I did this:

    <?php rewind_posts(); $c = 0; $toshow = 4; query_posts('category_name=articles'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<?php if ( !in_category('6') && ($c < $toshow) ): ?>
    		<?php $c++; ?>
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" class="title"><?php the_title(); ?></a><br />
    		<small><?php the_time('F jS, Y') ?></small>
    		<?php
    			$more = 0;
    			the_content("<b>Read More &raquo;</b>");
    		?>
    		<?php if($c < $toshow):?><hr /><?php endif; ?>
    <?php endif; endwhile; else: endif; ?>

    This has the “islast” logic (sorry I keep calling it that). I call a rewind_loop function before the query. I also had to reconstruct the logic of ‘showposts’ and ‘category_name’ so I could not display one category, but keep the counters working the way I wanted. I then have the one category I omitted here to display in yet another loop further down on the page, like so:

    <?php rewind_posts(); $c = 1; query_posts('category_name=legislative&showposts=2'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" class="title"><?php the_title(); ?></a><br />
    	<small><?php the_time('F jS, Y') ?></small>
    	<?php
    		$more = 0;
    		the_content("<b>Read More &raquo;</b>");
    	?>
    	<?php if($c<3):?><hr /><?php endif;?>
    	<?php $c++; ?>
    <?php endwhile; else: endif; ?>

    Here I use the category_name and showposts normally, since I don’t have the constraints I needed for the previous loop.

    I hope this is helpful to someone out there – took me a while to figure it out 😉 If you have any improvements on syntax or logic, I’d be happy to hear.

    thanks again esmi!

    Thread Starter mattyscribs

    (@mattyscribs)

    esmi – I am hoping you could continue to help me with something related – although the code I have above does work, it has somehow messed up the current page definition. In my sidebar on my homepage, it now says: ‘You are currently browsing the archives for the Legislative Issues category.’, which is in fact the last looped content on my page.

    For some reason the loops are causing the WP application to think I am on an archive page, which is messing up my template assignments, ie. the static homepage is using the page.php instead of the home.php file, and the is_front_page() doesn’t resolve true for the sidebar code I have going on.

    So I think maybe I need to close my loops in some way?

    A static home page will always use page.php unless you specify a custom page template. You can’t “close” while loops other than using an endwhile. It sounds like there’s a problem in your sidebar code.

    Thread Starter mattyscribs

    (@mattyscribs)

    thanks I think that all makes sense to me, but I am still having a problem –

    my home page is correctly using my “home.php” template, but in my sidebar, I have the following logic:

    <?php if ( is_front_page() || is_page() ) { ?>
    <!-- DO THIS -->
    <?php } else { ?>
    <!-- DO THAT -->
    <?php } ?>

    Now for some reason, the following code (which is in home.php) flips the switch in the logic above in my in the sidebar.
    <?php query_posts('category_name=blog&showposts=1'); ?>

    That piece of code makes my sidebar display “DO THAT” — when I remove it I get the “DO THIS” stuff. So something about the code is making the php think that I cam on an archive blog page or something, when I want it to think I am on my front page.

    Thanks for your help and I hope this makes sense.

    Try <?php if ( is_front_page() || is_home() || is_page() ) { ?>

    Thread Starter mattyscribs

    (@mattyscribs)

    thanks but doesn’t work..sorry :{

    Thread Starter mattyscribs

    (@mattyscribs)

    Hi Esmi – hate to be a bother but do you have any other suggestions? Otherwise, I’ll have to find a work-around and move on.. thanks again for your help.

    Thread Starter mattyscribs

    (@mattyscribs)

    I found my answer — must place

    <?php wp_reset_query(); ?>

    after each query.

    this topic can be resolved now. thanks again for your help esmi.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘islast for content loop and the more tag’ is closed to new replies.