[resolved] Drop down list of pages from a category (3 posts)

  1. Exponential
    Posted 2 years ago #

    This is a little bit unorthodox however I hope someone can help me!

    I am using Categories for not only posts but also my pages and this is now working well. What I would now like to do is display all pages within the same category as the current page ID in a drop down list where the default value is the current page.


    If I am on a page called "Page 2" for example. "Page 2" is in the category "Category 1" along with 2 other pages (Page 1 & Page 3). What I would like is a drop down which then displays "Item 2" as default but also has options for the 2 other pages.

    Drop Down List:

    - Page 1
    - Page 2 (Default Value)
    - Page 3

    When another page is selected it automatically links you to that page.

  2. Exponential
    Posted 2 years ago #

    I think I have made some progess. I have now managed to get a list of pages from within the same category as the current page using the following code (there is probably an easier way of doing however I am a beginner so please bare with!)

    <form action="<? bloginfo('url'); ?>" method="get">
    	<select onChange="document.location = this.value" value="GO" class="form-control">
    	<?php ( $cat_ID ); ?>
    	$categories = get_the_category();
    	$category_id = $categories[0]->cat_ID;
    	$pages = get_posts(array(
    		'category' => $category_id,
    		'post_type' => 'page',
    		'posts_per_page' => -1,
    		'post_status' => 'publish',
    		'orderby' => 'menu_order',
    		'order' => 'ASC'
    	foreach( $pages as $page ) {
    	<option value="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></option>
    	<?php }	?>

    However, the only sticking point I have at the moment is how to set the Default value as the current page.

    Any help would be appreciated!

  3. Exponential
    Posted 2 years ago #

    By doing more research and googling a few things I have found a solution using JQuery.

    <script type='text/javascript'>
    $('select option:contains("<?php echo get_the_title($ID); ?>")').prop('selected',true);

    This adapted bit of codes grabs the current page name via "get_the_title" and then the JQuery sets that option as default using the :contains(text) selector.

    Source: http://jsfiddle.net/halfcube/XyTtu/
    Source: http://stackoverflow.com/questions/4781420/how-to-set-default-value-in-dropdownlist-using-jquery

Topic Closed

This topic has been closed to new replies.

About this Topic