WordPress.org

Ready to get started?Download WordPress

Forums

Ambrosite Next/Previous Post Link Plus
weird behavior with menu_order and in_same_cat (6 posts)

  1. Mike Stop Continues
    Member
    Posted 3 years ago #

    I'm getting some perplexing behavior in trying to make simple hierarchy navigation.

    I'm using regular pages to build a number of hierarchies for different sections of my site and turned to this plugin to help navigate the trees.

    Consider this structure:

    + Project 1
    + Project 2
    --+ Page 2.1
    ----+ Subpage 2.1.1
    ----+ Subpage 2.1.2
    ----+ Subpage 2.1.3
    + Project 3

    I'd like to focus on the behavior in navigating Project 2. I only want to see pages from the same category and I want to be able to read the pages in order: Project 2 -> Page 2.1 -> Subpage 2.1.1 -> Subpage 2.1.2 -> Subpage 2.1.3

    The thing is, on Project 2, I get these links:
    Prev: Project 1 <--- but it's in a different category!
    Next: Project 3 <--- but it's in a different category!

    And on Page 2.1, I get:
    Prev: Subpage 2.1.3 <--- strange reversal
    Next: Project 2 <--- strange reversal

    On Subpage 2.1.1:
    Prev: ---- <--- should be Page 2.1
    Next: Subpage 2.1.2

    On Subpage 2.1.2:
    Prev: Subpage 2.1.1
    Next: Subpage 2.1.3

    On Subpage 2.1.3:
    Prev: Subpage 2.1.2
    Next: Page 2.1 <--- should be ------

    That may be confusing...

    Instead of this:
    ---- -> 2 -> 2.1 -> 2.1.1 -> 2.1.2 -> 2.1.3 -> ----
    I get this:
    1 -> 2 -> 3
    ---- -> 2.1.1 -> 2.1.2 -> 2.1.3 -> 2.1 -> 2

    The big questions:
    1) Why does in_same_cat fail on the root node?
    2) Why is the menu_order correct on the lowest level, but reversed in the parents?

    It may be my code, but I don't think so...

    $next = next_post_link_plus(array(
    	'order_by' => 'menu_order',
    	'order_2nd' => 'menu_order',
    	'meta_key' => '',
    	'loop' => false,
    	'end_post' => false,
    	'thumb' => false,
    	'max_length' => 0,
    	'format' => '<div class="floatright">%link</div>',
    	'link' => '%title &rarr;',
    	'tooltip' => '',
    	'in_same_cat' => true,
    	'in_same_tax' => false,
    	'in_same_format' => true,
    	'ex_cats' => '',
    	'ex_cats_method' => 'strong',
    	'ex_posts' => '',
    	'before' => '',
    	'after' => '',
    	'num_results' => 1,
    	'echo' => false,
    ));
    
    $prev = previous_post_link_plus(array(
    	'order_by' => 'menu_order',
    	'order_2nd' => 'menu_order',
    	'meta_key' => '',
    	'loop' => false,
    	'end_post' => false,
    	'thumb' => false,
    	'max_length' => 0,
    	'format' => '<div class="floatleft">%link</div>',
    	'link' => '&larr; %title',
    	'tooltip' => '',
    	'in_same_cat' => true,
    	'in_same_tax' => false,
    	'in_same_format' => true,
    	'ex_cats' => '',
    	'ex_cats_method' => 'strong',
    	'ex_posts' => '',
    	'before' => '',
    	'after' => '',
    	'num_results' => 1,
    	'echo' => false,
    ));

    Thanks for your help!

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

  2. ambrosite
    Member
    Plugin Author

    Posted 3 years ago #

    Mike,

    My plugin is not intended to work with pages at all, only posts. I have no idea what would happen if you tried to use it with pages, but I am not surprised to hear that you are getting unpredictable results.

    In general, the whole concept of next and previous does not work in a hierarchy, only in a linear sequence of nodes. That is why WordPress does not have a built-in "next_page_link" function. It is not a bug in the code, but rather a logic problem.

  3. Mike Stop Continues
    Member
    Posted 3 years ago #

    I understand.

    Today or tomorrow I'll try to make it work somehow. I'll pass on the code if I can. I believe wordpress can retrieve hierarchical pages in linear order because it does so on the pages admin page. The challenge will be to figure out how.

    Thanks for replying so quickly. Your plugin is great too.

  4. ambrosite
    Member
    Plugin Author

    Posted 3 years ago #

    Thanks for the kind words, and for your offer, but there is nothing in my plugin that could even remotely be adapted to this problem. Traversing a node hierarchy is a completely different logical problem from sorting a linear sequence of nodes.

    If you really want to do this, you should look at the Walker_Page class from wp-includes/post-template.php. That is the code that WordPress uses to generate hierarchical page lists (with wp_list_pages, for example). The only possible way to make a next_page_link function would be to walk the page tree similar to how that code does it.

  5. Mike Stop Continues
    Member
    Posted 3 years ago #

    I think you're right about adapting your code, but perhaps not. Here's what I ended up making. It returns data for next, prev, first, last, up, and children. It's not very efficient, but to make it so would require I rewrite get_pages()...

    [Code moderated as per the Forum Rules. Please use the pastebin]

  6. Mike Stop Continues
    Member
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic