skip to Main Content

I was using Redux option framework before and using it’s feature [data] argument to select page using dropdown to select WordPress page from dropdown, it gives us page ID and we can create anchor links using that page ID in frontend. Now I am trying Kirki customizer framework but I don’t find any option to do so, I somehow found that we can do it by ‘choices’ argument in SELECT option but is being unable to figure out how to do it? Is anyone available to help?

2

Answers


  1. Chosen as BEST ANSWER

    Latest version of Kirki came with solution with Dropdown_Pages control and my problem is solved. Here is example from the kirki documentation:

    new KirkiFieldDropdown_Pages(
        [
            'settings' => 'dropdown_pages_setting',
            'label'    => esc_html__( 'Dropdown Pages', 'kirki' ),
            'section'  => 'section_id',
            'default'  => 42,
            'priority' => 10,
        ]
    );
    

    For more information visit: https://docs.themeum.com/kirki/controls/dropdown-pages/


  2. In Kirki, you can use the ‘choices’ argument to create a dropdown select field in the WordPress customizer. To create anchor links with page IDs, you can follow these steps:

    First, ensure that you have Kirki installed and properly configured in your WordPress theme.

    Define a section and a control for your dropdown select field in your theme’s functions.php or in a separate customizer configuration file:

    // Replace 'your_theme_prefix' with your actual theme prefix.
    Kirki::add_config( 'your_theme_prefix', array(
        'capability'  => 'edit_theme_options',
        'option_type' => 'theme_mod',
    ) );
    
    Kirki::add_section( 'page_select_section', array(
        'title' => esc_html__( 'Page Selector', 'your_theme_textdomain' ),
        'panel' => 'your_customizer_panel_id', // Replace with your actual panel ID.
    ) );
    
    Kirki::add_field( 'your_theme_prefix', array(
        'type'        => 'select',
        'settings'    => 'selected_page',
        'label'       => esc_html__( 'Select a Page', 'your_theme_textdomain' ),
        'section'     => 'page_select_section',
        'default'     => '',
        'choices'     => kirki_get_pages(), // This function retrieves a list of pages.
    ) );
    

    In the above code, replace ‘your_theme_prefix’ with your actual theme prefix and adjust other settings as needed.

    Create a callback function to retrieve the list of pages for the ‘choices’ argument. You can place this function in your functions.php file or a custom functions file:

    function kirki_get_pages() {
        $pages = get_pages();
        $page_choices = array();
    
        foreach ( $pages as $page ) {
            $page_choices[ $page->ID ] = $page->post_title;
        }
    
        return $page_choices;
    }
    

    Now, you have a dropdown select field in the WordPress customizer that lists all the pages on your site. Users can select a page, and its ID will be saved in the ‘selected_page’ theme_mod option.

    To create anchor links using the selected page ID on your frontend, you can retrieve the selected page ID using get_theme_mod(‘selected_page’) and then construct your anchor link accordingly:

    $selected_page_id = get_theme_mod('selected_page');
    if (!empty($selected_page_id)) {
        $anchor_link = get_permalink($selected_page_id) . '#your-anchor';
        echo '<a href="' . esc_url($anchor_link) . '">Link to Selected Page with Anchor</a>';
    }
    

    Replace ‘your-anchor’ with the actual anchor you want to use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search