• Resolved Waningett

    (@waningett)


    Hej!

    On my site I have an index page that displays only the most resent post (the whole blog thing is not the main focus on the site). I managed this with the following code, which worked like a dream offline on my localhost, but now the site is online the previous/next navigation is gone.
    I tried to replace the code with the showposts query, but that didn’t work as hoped… It looks fine, as long as you don’t use the navigation, cause it affects the rest of the site, but not the blog itself.
    I would like to find a way to make things work online as well as they do off line.
    Any thoughts?

    <?php if ( have_posts() ) : ?>
    			<?php /* Start the Loop */ ?>
    		<?php while ( have_posts() ) : the_post(); ?>
    			<?php static $count=0;
    				if($count=="1"){break;}
    				else{ ?>
    
    		<?php get_template_part( 'content', get_post_format() ); ?>
    			<?php $count++; } ?>
    			<?php endwhile; ?>
    
    			<?php waningett_content_nav( 'nav-below' ); ?>
    
    			<?php else : ?>
    			<?php get_template_part( 'no-results', 'index' ); ?>
    
    		<?php endif; ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • I suspect the problem is that you are breaking out of the while loop instead of limiting the number of posts retrieved. Try adding this just before the first line of the code you showed:

    <?php global $wp_query;
    query_posts(
       array_merge(
          $wp_query->query,
          array('posts_per_page' => 1)
       )
    );
    ?>
    Thread Starter Waningett

    (@waningett)

    Thanks vtxyzzy, it almost works ??

    The thing is that the navigation is back and it works on the posts, that is the good news, but the bad new is that it also affects the static part of the site… It now shows the 404-template part in the main area where the static text used to be. Any tips on how to solve this problem?
    It’s so frustrating that some things don’t work the same offline as they do online..

    It sounds like you have another query in the template. In that case, you need to save and restore the $wp_query:

    <?php $temp_query = clone $wp_query; ?>
    -- put existing code here --
    <?php $wp_query = clone $temp_query;
    wp_reset_query(); ?>

    Normally you would change from query_posts() to using a new WP_Query, but I suspect that will not work with the waningett_content_nav() function.

    Thread Starter Waningett

    (@waningett)

    Hej and thanks again for your response, but… it still doesn’t do the trick.
    Would it help if I give the code for the waningett_content_nav() function?

    if ( ! function_exists( 'waningett_content_nav' ) ):
    function waningett_content_nav( $nav_id ) {
        global $wp_query, $post;
        if ( is_single() ) {
            $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
            $next = get_adjacent_post( false, '', false );
    
            if ( ! $next && ! $previous )
                return;
        }
    
        if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
            return;
    
        $nav_class = 'site-navigation paging-navigation';
        if ( is_single() )
            $nav_class = 'site-navigation post-navigation';
        ?>
        <nav role="navigation" id="<?php echo $nav_id; ?>" class="<?php echo $nav_class; ?>">
            <h1 class="assistive-text"><?php _e( 'Post navigation', 'waningett' ); ?></h1>
    
        <?php if ( is_single() ) : ?>
    
            <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'waningett' ) . '</span> %title' ); ?>
            <?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'waningett' ) . '</span>' ); ?>
    
        <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) :  ?>
    
            <?php if ( get_next_posts_link() ) : ?>
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> ?ldre', 'waningett' ) ); ?></div>
            <?php endif; ?>
    
            <?php if ( get_previous_posts_link() ) : ?>
            <div class="nav-next"><?php previous_posts_link( __( 'Nyare <span class="meta-nav">&rarr;</span>', 'waningett' ) ); ?></div>
            <?php endif; ?>
    
        <?php endif; ?>
    
        </nav>
        <?php
    }
    endif;

    and maybe you can check the site itself, https://www.waningett.se.
    Mind you, the navigation works as a charm for the single pages, and I would like to keep it that way, but it’s the index page that is the problem..
    Thank you so much for your help! I am fist and far most a designer and not a programer, which becomes painfully clear in this situation ??

    Please put the entire template code for the page into a pastebin and post a link to it here.

    Thread Starter Waningett

    (@waningett)

    Hej again!
    Ok, I’ve posted the different template-parts that make up my index page into this paste bin: https://pastebin.com/848VEHgM
    I really appreciate your help!

    The only loop I found in that code is for the asides. Have you tried setting ‘Blog pages show at most’ to 1 in Admin->Settings->Reading?

    If not, take out the query code I suggested and try that.

    Thread Starter Waningett

    (@waningett)

    I do feel a bit stupid now, that the answer actually was so simple, but I guess sometimes it takes an extra set of eyes to se what is going on.
    I thank you ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘One post on index, with navigation’ is closed to new replies.