WordPress.org

Forums

If archives or search results < X, do not display navigation bar (5 posts)

  1. kReEsTaL
    Member
    Posted 8 years ago #

    Hi :)

    My WP install is configured to show 15 posts/results/archives per page. So, when you display a page with more than 15 posts/results/archives, the navigation bar displays to allow the visitor to browse the previous posts/results/archives.

    BUT when there are LESS THAN 15 posts (or whatever number you set up in your config), my navigation bar shows all the same, causing some CSS trouble (since there are no "Previous" nor "Next" links, my div is empty and screws everything up).

    So I would like to say in PHP: "if there are LESS - OR - AS MANY AS 15 posts/results/archives, do NOT display the div which contains the navigation links".

    Can anybody help me with this please? Maybe it's simple but I'm no PHP developper. I guess it has something to do with the php endif or something, but I can't figure it out and do not want to break my site.

    Here is my search.php page:

    <?php get_header(); ?>
    
    <?php get_sidebar(); ?>
    
    	<div id="content" class="hasLayout">
    
    	<?php if (have_posts()) : ?>
    
    	<div id="titre_h2">
    		<h2 class="pagerech">Résultats de la recherche</h2>
    	</div>
    
    		<div class="navigation_fix">
    			<div class="alignleft"><?php next_posts_link('&laquo; Résultats précédents') ?></div>
    			<div class="alignright"><?php previous_posts_link('Résultats suivants &raquo;') ?></div>
    		</div>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<div class="post hasLayout">
    				<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permalink vers <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    
    				<p class="postmeta">Ecrit le <?php the_time('j F Y') ?> <?php _e('à'); ?> <?php the_time() ?>, mis à jour le <?php mdv_last_modified(); ?> · <?php _e('Posté dans'); ?> <?php the_category(', ') ?>
    
    				<p class="postmetatag">Tags: <?php STP_PostTags(); ?>
    
    				<p class="postmetadata"><?php comments_popup_link('Aucun commentaire »', '1 commentaire »', '% commentaires »'); ?>
    
    			</div>
    
    	<?php endwhile; ?>
    
    		<div class="navigation_fix">
    			<div class="alignleft"><?php next_posts_link('&laquo; Résultats précédents') ?></div>
    			<div class="alignright"><?php previous_posts_link('Résultats suivants &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2>Aucun billet trouvé. Lancez une autre recherche.</h2>
    		<form id="searchform" method="get" action="<?php echo get_settings('siteurl'); ?>/index.php">
    		Rechercher: <input type="text" name="s" size="30" /> <input type="submit" value="<?php _e('Ok'); ?>" />
    
    		</form>
    
    	<?php endif; ?>
    
    	</div>
    
    <?php get_footer(); ?>

    I do not post my archives.php page, I think it might be the same kind of solution - but tell me whenever I need to post it.

  2. gt1892
    Member
    Posted 8 years ago #

    I needed something like this for a site I am working on.

    For it to work on your page you'll need to have the following:

    <?php if (($wp_query->post_count) >= 15) : ?>

    just above your "navigation_fix" div.

    Then have:

    <?php endif; ?>

    immediately below it.

    Does that makes sense?

  3. moshu
    Member
    Posted 8 years ago #

    The problem is you are using the wrong template tag.
    This is what you need: http://codex.wordpress.org/Template_Tags/posts_nav_link

  4. kReEsTaL
    Member
    Posted 8 years ago #

    Thank you for your replies!

    @gt1892: your tip definitely fixed my problem! Thank you so much! :)

  5. moshu
    Member
    Posted 8 years ago #

    I repeat: that's an old template tag and using the proper tag you don't need any fix.

Topic Closed

This topic has been closed to new replies.

About this Topic