• Hello, I am fairly new to wordpress but can get a basic understanding of how most things work, however when it comes to programming new features, especially the one I’m looking for, I struggle … badly.

    I am attempting to create a fan fiction archive of sorts, and on each post page I wish to include a drop down menu that will allow navigation to each posts seperate chapters (these are just other posts with the same name, wordpress appends -1 or -2 and so on to the permalink).

    What I’m basically looking for is a drop down list that once clicked will take the user to that particular chapter. That list I imagine would need to be populated with a list of all the other pages named with the same name (slugs will obviously be different).

    The way I am organising the fan fictions is a category for each one, and with post tags I am denoting an episode, such as 1×01, 1×02 and so on that people can include to tag their post as being an episode.

    From there as above if they wish to use multiple chapters they just type the exact same name (of the episode) for the post title and it would add the respective number on the end.

    What I am essentially asking for is hack that counts the number of pages with the same title, and adds them to the dropdown with the text of chapter 1, chapter 2 and so on, selecting say for example chapter 4 would take the user to the 4th post of the same name.

    Here is how I am using my permalinks to order it all.

    /sg-rtnanc/1×01/meh-4/
    or
    /category/tag/postname

    If anyone could help me with this that would be fantastic!

Viewing 1 replies (of 1 total)
  • Thread Starter Azureum

    (@azureum)

    Never mind, I actually managed to code it myself pulling from various sources.

    Only thing I’m stuck with now is I would like it to not show up if there is only 1 post of the same name.

    Here is the code I am using:

    <?php $counter = '1'; ?>
    
    <form action="../">
    <select onchange="window.open(this.options[this.selectedIndex].value,'_top')">
    
    <option value="">Choose a chapter...</option>
    <?php
    
    $chapters = $wpdb->get_results("SELECT * FROM $wpdb->posts
    	WHERE post_title = '" . $post->post_title . "' AND post_status = 'publish' ORDER BY post_date");
    if ($chapters) :
    	foreach ($chapters as $post) :
    		setup_postdata($post);
    ?>
    	<option value="<?php the_permalink(); ?>">Chapter <?php echo $counter++; ?></option>
    <?php
    	endforeach;
    else :
    ?>
        <h2> Not Found</h2>
    <?php endif; ?>
    
    </select>
    </form>

    I am stuck on what code I would put in this mini loop so that it would check if the results returned are more than 1 and display the above drop down, if its just the 1 post then don’t display drop down.

Viewing 1 replies (of 1 total)
  • The topic ‘Drop down navigation by postname’ is closed to new replies.