• Resolved Exponential

    (@exponential)


    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.

    Example:

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Exponential

    (@exponential)

    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 ); ?>
    	<?php
    	$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 }	?>
    	</select>
    </form>

    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!

    Thread Starter Exponential

    (@exponential)

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

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

    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: https://jsfiddle.net/halfcube/XyTtu/
    Source: https://stackoverflow.com/questions/4781420/how-to-set-default-value-in-dropdownlist-using-jquery

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Drop down list of pages from a category’ is closed to new replies.