WordPress.org

Ready to get started?Download WordPress

Forums

Ambrosite Next/Previous Page Link Plus
[resolved] Can this work on category, archive or taxonomy pages? (7 posts)

  1. Puffertle
    Member
    Posted 1 year ago #

    Is it possible to use this plugin on category, archive or taxonomy pages?

    For instance, if there's a category called 2012, and a viewer is on the category page (archive view), when the next link is clicked it'd go to the next category page (archive view) called 2013? If the previous link is clicked it'd go the the previous category page (archive view) called 2011?

    Thanks in advance for your help.

    http://wordpress.org/extend/plugins/ambrosite-nextprevious-page-link-plus/

  2. ambrosite
    Member
    Plugin Author

    Posted 1 year ago #

    No, my plugin cannot do that. However I did a quick forum search and found this, maybe it will work for you:

    http://wordpress.org/support/topic/add-a-next-category-page-link?replies=4

  3. Puffertle
    Member
    Posted 1 year ago #

    Thank you for showing me this. It is very close to what I was looking to do. It seems to work on a category/archive page, but I can't seem to get it to work on a taxonomy page.

    I realize that this isn't your plugin, but you wouldn't happen to know off-hand why simply changing this:

    foreach(get_categories()

    To this:

    foreach(get_categories('taxonomy=theslugofthetaxonomy')

    Fails to work? Thanks again for your help!

  4. ambrosite
    Member
    Plugin Author

    Posted 1 year ago #

    That looks like it should work. If I was going to investigate this, I would start by assigning the results of get_categories to a variable and then doing a var_dump to see what it is actually returning.

  5. Puffertle
    Member
    Posted 1 year ago #

    I'll attempt that and follow up on this issue elsewhere, as it's no longer relevant to the Next/Previous Page Link Plus plugin. Thank you for your help!

  6. ambrosite
    Member
    Plugin Author

    Posted 1 year ago #

    You're welcome. Let me know what you find out; I am curious now why that change you made does not work.

  7. Puffertle
    Member
    Posted 1 year ago #

    In case anyone comes to this thread in the future, this is what I did to solve my problem.

    Here's the original by alchyhmyth 2011 for reference:

    <?php
    //alchyhmyth 2011
    //transformationpowertools.com/wordpress
    //
    //a simple next/prev category navigation for category archives
    //
    foreach(get_categories() as $all_cat) {  $cat_ids[] = $all_cat->term_id; }
      $this_cat = get_query_var('cat');
      $this_cat_position = array_search( $this_cat, $cat_ids ); ?>
    
    <?php $prev_cat_position = $this_cat_position -1;
          if( $prev_cat_position >=0 ) {
          $prev_cat_id = array_slice( $cat_ids, $prev_cat_position, 1 );
        echo '<a href="' . get_category_link($prev_cat_id[0]) . '">« ' . get_category($prev_cat_id[0])->name . '</a>'; } ?>
    
    <?php $next_cat_position = $this_cat_position +1;
          if( $next_cat_position < count($cat_ids) ) {
          $next_cat_id = array_slice( $cat_ids, $next_cat_position, 1 );
    echo '<a href="' . get_category_link($next_cat_id[0]) . '">' . get_category($next_cat_id[0])->name . ' »</a>'; } ?>

    This works fine on a regular category archive. But, it does not work on a taxonomy archive. I don't know why. But, if you modify it this way, it works:

    <?php
    //original by alchyhmyth 2011
    //transformationpowertools.com/wordpress
    //
    //modified for a specificed taxonomy by Puffertle
    //
    //a simple next/prev navigation for a specificed taxonomy's archives
    //
    
    foreach(get_categories('taxonomy=theslugofthetaxonomy') as $all_cat) { $cat_ids[] = $all_cat->term_id; $cat_slug[] = $all_cat->slug; $cat_title[] = $all_cat->cat_name; }
    
    $this_cat = get_queried_object()->term_id;
    $this_cat_position = array_search( $this_cat, $cat_ids ); ?>
    
    <?php $prev_cat_position = $this_cat_position -1;
    if( $prev_cat_position >=0 ) {
    
    	$prev_cat_id = array_slice( $cat_ids, $prev_cat_position, 1 );
    	$prev_cat_slug = array_slice( $cat_slug, $prev_cat_position, 1 );
    	$prev_cat_name = array_slice( $cat_title, $prev_cat_position, 1 );
    	$my_prev_link_id = $prev_cat_id[0];
    	$my_prev_link_slug = $prev_cat_slug[0];
    	$my_prev_link_name = $prev_cat_name[0];
    
    	$my_prev_link = get_term_link($my_prev_link_slug, 'theslugofthetaxonomy');
    	echo '<a href="' . $my_prev_link . '">« ' . $my_prev_link_name . '</a>';
    
    } ?>
    
    <?php $next_cat_position = $this_cat_position +1;
    if( $next_cat_position < count($cat_ids) ) {
    
    	$next_cat_id = array_slice( $cat_ids, $next_cat_position, 1 );
    	$next_cat_slug = array_slice( $cat_slug, $next_cat_position, 1 );
    	$next_cat_name = array_slice( $cat_title, $next_cat_position, 1 );
    	$my_next_link_id = $next_cat_id[0];
    	$my_next_link_slug = $next_cat_slug[0];
    	$my_next_link_name = $next_cat_name[0];
    
    	$my_next_link = get_term_link($my_next_link_slug, 'theslugofthetaxonomy');
    	echo '<a href="' . $my_next_link . '">' . $my_next_link_name . ' »</a>'; 
    
    } ?>

    Just make sure to change the three (3) instances of theslugofthetaxonomy to the actual slug of the taxonomy you're wanting to navigate through.

    Things I noticed:

    get_query_var('cat')

    Wasn't working on a taxonomy archive, but this did:

    get_queried_object()->term_id

    Also, when dealing with a taxonomy, get_category_link and get_category were not working. Instead, get_term_link had to be used. Also, the taxonomy ID was not sufficient for what had to be done. The slug and name had to be added:

    $cat_ids[] = $all_cat->term_id; $cat_slug[] = $all_cat->slug; $cat_title[] = $all_cat->cat_name;

    So they could be called here:

    $prev_cat_id = array_slice( $cat_ids, $prev_cat_position, 1 );
    	$prev_cat_slug = array_slice( $cat_slug, $prev_cat_position, 1 );
    	$prev_cat_name = array_slice( $cat_title, $prev_cat_position, 1 );
    	$my_prev_link_id = $prev_cat_id[0];
    	$my_prev_link_slug = $prev_cat_slug[0];
    	$my_prev_link_name = $prev_cat_name[0];

    Your advice about the var_dump was helpful, as it let me see what values were available, though upon a first glance the output didn't look any different from a category archive. I think the problem may have had something to do with how WordPress treats taxonomies differently from a category.

    I am not very proficient with PHP, so if you see a better way to do this than what I've shown please let me know.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic