Title: need code to select page
Last modified: October 26, 2020

---

# need code to select page

 *  [shaibustephen](https://wordpress.org/support/users/shaibustephen/)
 * (@shaibustephen)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/need-code-to-select-page/)
 * I am creating a custom front page and in this page to display only content from
   each page and not post to form different block. My problem now is the code to
   effect this. Let say I have a page named services and I want the content to be
   displayed in block 1, programming page and this to be displayed block 2 etc. 
   what code should I use in my widget to give room for page selection unlike as
   was the case of post? Or what alternative way can I get this done?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13581457)
 * You should read the code of Twenty Seventeen. [https://wordpress.org/themes/twentyseventeen/](https://wordpress.org/themes/twentyseventeen/)
   
   It has a Customizer option for the front page to allow you to choose which Pages
   to show.
 *  Thread Starter [shaibustephen](https://wordpress.org/support/users/shaibustephen/)
 * (@shaibustephen)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13582712)
 * OK. thanks
 *  Thread Starter [shaibustephen](https://wordpress.org/support/users/shaibustephen/)
 * (@shaibustephen)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13588577)
 * I copied below from somewhere and applied it in my plugin and gives exactly what
   I want.
    The problem right now is that though it selects pages but doesn’t save
   the chosen page. what should I add to it.
 * <select name=”easyreg_redirect_page”>
    <option selected=”selected” value=”0″>
   <?php echo esc_attr( __( ‘Select page’ ) ); ?></option> <?php $selected_page 
   = get_option( ‘option_key’ ); $pages = get_pages(); foreach ( $pages as $page){
   $option = ‘<option value=”‘ . $page->ID . ‘” ‘; $option .= ( $page->ID == $selected_page)?‘
   selected=”selected”‘ : ”; $option .= ‘>’; $option .= $page->post_title; $option.
   = ‘</option>’; echo $option; } ?> </select>
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13588770)
 * It is easy to show a dropdown list of pages. It’s even easier when you use the
   WP function for it: [https://developer.wordpress.org/reference/functions/wp_dropdown_pages/](https://developer.wordpress.org/reference/functions/wp_dropdown_pages/)
 * The problem with copying that code is that showing the list is the only thing
   it does. Saving the choice is different if it’s in the Customizer or the editor
   or an options page. The Customizer is probably easiest; that’s why I suggested
   you look at Twenty Seventeen. The Customizer does the saving for you. You just
   have to initialize with the option name and default value. Then do the rendering
   according to the saved value.
 *  Thread Starter [shaibustephen](https://wordpress.org/support/users/shaibustephen/)
 * (@shaibustephen)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13589087)
 * ok. This is what i could see in the customizer.php as you have pointed out. I
   concern is that as i am using it in wordpress plugin for my widget area, how 
   do i use this function then to execute dropdown page list for selection
    /** *
   Filter number of front page sections in Twenty Seventeen. * * [@since](https://wordpress.org/support/users/since/)
   Twenty Seventeen 1.0 * * [@param](https://wordpress.org/support/users/param/)
   int $num_sections Number of front page sections. */ $num_sections = apply_filters(‘
   twentyseventeen_front_page_sections’, 4 );
 *  // Create a setting and control for each of the sections available in the theme.
   
   for ( $i = 1; $i < ( 1 + $num_sections ); $i++ ) { $wp_customize->add_setting(‘
   panel_’ . $i, array( ‘default’ => false, ‘sanitize_callback’ => ‘absint’, ‘transport’
   => ‘postMessage’, ) );
 *  $wp_customize->add_control(
    ‘panel_’ . $i, array( /* translators: %d: The front
   page section number. */ ‘label’ => sprintf( __( ‘Front Page Section %d Content’,‘
   twentyseventeen’ ), $i ), ‘description’ => ( 1 !== $i ? ” : __( ‘Select pages
   to feature in each area from the dropdowns. Add an image to a section by setting
   a featured image in the page editor. Empty sections will not be displayed.’, ‘
   twentyseventeen’ ) ), ‘section’ => ‘theme_options’, ‘type’ => ‘dropdown-pages’,‘
   allow_addition’ => true, ‘active_callback’ => ‘twentyseventeen_is_static_front_page’,));
 *  $wp_customize->selective_refresh->add_partial(
    ‘panel_’ . $i, array( ‘selector’
   => ‘#panel’ . $i, ‘render_callback’ => ‘twentyseventeen_front_page_section’, ‘
   container_inclusive’ => true, ) ); } } add_action( ‘customize_register’, ‘twentyseventeen_customize_register’);
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13589271)
 * OK, a plugin could have a widget (with its own interface which has to work in
   the widget page and in the Customizer) or a meta box in the editor, or a block,
   or a button on the Classic editor toolbar, or an options page, or a Customizer
   section.
    But you can’t just take code written for one environment and use it
   in another. They are all different for how you save the options and any needed
   Javascript. Using the options once they are saved is different if it’s in a widget
   than in a normal option or in post meta.
 * If you are set on a widget, you only need to select one Page and then the user
   can add as many of those widgets as they want. Widget options are in the options
   table, but the code to handle them is within the Widget class that your code 
   extends. Take a look at the core Pages widget to see, although it might not have
   any options.
 *  Thread Starter [shaibustephen](https://wordpress.org/support/users/shaibustephen/)
 * (@shaibustephen)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13590022)
 * Below is what I have done for category list and how do I change or amend this
   to display pages
 * <p>
    <label for=”<?php echo $this->get_field_id(‘cat_id’); ?>”><?php _e( ‘Category
   Name:’ , ‘bav_itp’)?></label> <select id=”<?php echo $this->get_field_id(‘cat_id’);?
   >” name=”<?php echo $this->get_field_name(‘cat_id’); ?>”> <option value=”0″>Choose
   category:</option> <?php $this->categories = get_categories(); foreach ( $this-
   >categories as $cat ) { $selected = ( $cat->term_id == esc_attr( $cat_id ) ) ?‘
   selected = “selected” ‘ : ”; $option = ‘<option ‘.$selected .’value=”‘ . $cat-
   >term_id; $option = $option .'”>’; $option = $option .$cat->name; $option = $
   option .'</option>’; echo $option; } ?> </select> </p>

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘need code to select page’ is closed to new replies.

## Tags

 * [page widget](https://wordpress.org/support/topic-tag/page-widget/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 2 participants
 * Last reply from: [shaibustephen](https://wordpress.org/support/users/shaibustephen/)
 * Last activity: [5 years, 7 months ago](https://wordpress.org/support/topic/need-code-to-select-page/#post-13590022)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
