mtthwbsh
mtthwbsh

Reputation: 227

Applying anchor link to WordPress pagination twice on same page

For my portfolio site, I would like to add anchor links to both the 'work' and 'blog' sections so that when clicked through to the next page it goes to the respective section. I noticed this is possible using jQuery from this question: WordPress pagination - Adding an Anchor link, but am unsure how this would work with two loops on the same page?

my current loops look like this, just replacing categories for each section:

 <?php $paged = (get_query_var('page')) ? get_query_var('page') : 1;
 $args=array('category_name'=>'portfolio','posts_per_page'=>4,'paged'=>$paged);
 query_posts($args);
 if (have_posts()) : while (have_posts()) : the_post(); ?>  

    <div class="blog-post">
    <div class="thumbnail">

    <a href="<?php the_permalink(); ?>">
    <?php
        if ( has_post_thumbnail() ) {
        the_post_thumbnail();
        }   
    ?>  
    </a>

    <a class="details" href="<?php the_permalink(); ?>">
    <h6><?php echo get_the_excerpt(); ?></h6>
    </a><!-- DETAILS -->
    </div><!-- THUMBNAIL -->        

    <div class="aside">

    <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

    </div><!-- ASIDE -->
    </div><!-- BLOG - POST -->

<?php endwhile; ?>
<div class="navigation">
<h3><?php posts_nav_link('&#8734;','&#171; Newer Posts','Older Posts &#187;'); ?></h3>
</div><!-- PAGED-NAVIGATION -->
<?php wp_reset_query(); ?>

Upvotes: 0

Views: 2140

Answers (2)

scott
scott

Reputation: 1070

Ah I see what you mean; actually you are better off using wordpresses $args for the paginate_links() function. You can see it here: http://codex.wordpress.org/Function_Reference/paginate_links .

The one you want to change is 'format'=>'?page=%#%', (which is the page number) and changing it to something like 'format' => '?page=%#%#work', and 'format' => '?page=%#%#blog',

So you can do: echo paginate_links(array('format' => '?page=%#%#work')); which should make clicking the link jump back down to the work anchor.

The problem is, you will still have a page jump if the user isn't scrolled exactly to the position of the anchor link. You are best to implement an Ajax solution so there is no reload of the page at all. Here is a good tutorial to get you started: http://wp.tutsplus.com/articles/getting-started-with-ajax-wordpress-pagination/

Upvotes: 1

scott
scott

Reputation: 1070

Where you have your first <div class="navigation"> Add an id="work" and to the second id="blog" so you would have

<div class="navigation" id="work">
</div> 
<div class="navigation" id="blog"> 
</div> 

Somewhere on your page.

Then you need a small modification to the jquery from the question you referred to to make the correct anchor link:

<script type="text/javascript">
$(document).ready(function() {
    $('.pagenavi a').each(function(i,a){
        $(a).attr('href',$(a).attr('href')+'#'+a.parents('.navigation').attr('id'));
        //$(a).attr('href',$(a).attr('href')+'#blog') <-- instead of this 
    });
});
</script>

The parents('.navigation').attr('id') tells jquery to move up the dom intil it finds the navigition tag, and just grab it's ID to use as the text for the achor text

If you already have the ids blog and work on the page, you could use rel="work" instead and then you would in the jquery use attr('rel')

Upvotes: 0

Related Questions