• Resolved markheppner

    (@markheppner)


    I’m trying to do something and I’m starting to believe it’s not possible. Maybe someone will confirm that.

    I’m building a new theme using Hybrid and a child theme. I added a top navigation bar that is fixed, so it stays at the top as you scroll down the page. I want it to display the titles of the posts that are showing on the page.

    I have it working, but I can’t get it to display just the posts on the current page (I have it paginated with just 2 posts to figure this out).

    This is the stuff I have in my functions.php file:

    function news_page_nav() {
    
    global $post;
    
    $args_pages = array(
    	'post_type' => 'post',
    	'numberposts' => 0,
    	'post_status' => 'publish',
    	'order' => 'DESC',
    	'nopaging' => false,
    	);
    $myposts = get_posts($args_pages);
    
    	echo '<div id="top-nav">' . "\n\t\t\t";
    	echo '<div id="post-nav">' . "\n\t\t\t";
    
    	echo '<ul class="top-anchors">';
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    	<li><a href="#post-<?php the_ID(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach;
    	echo '</ul>';
    
    	echo "\n\t</div><!-- #post-nav -->\n";
    	echo "\n\t</div><!-- #top-nav -->\n";
    
    }

    As you can see, it’s outputting a list of the titles (with anchored links) of the posts. Unfortunately, I can’t get it to paginate with the posts. I don’t understand the $nopaging parameter and what exactly it’s supposed to do. Any ideas?

    http://codex.wordpress.org/Template_Tags/get_posts

Viewing 2 replies - 1 through 2 (of 2 total)
  • Actually you should NOT need to use get_posts as the $posts object will be set automatically by WordPress.

    This in a header.php works fine using the WordPress Default theme.

    <?php
    if (have_posts()) {
    while (have_posts()) {
    the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    <?php
    }
    }
    rewind_posts();
    ?>

    [edit]

    Thread Starter markheppner

    (@markheppner)

    Wow, I am dumb. Thank you so much, that works perfectly!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Navigation with get_posts template tag’ is closed to new replies.