theme customizer - How do I use add_control to offer a list of all pages in the customiser?

What I am trying to do is offer three pages to highlight on the front page. To do this, I would like to offer users a dr

What I am trying to do is offer three pages to highlight on the front page. To do this, I would like to offer users a dropdown list of all pages.

I have not worked out how to do this.

I know how to get the pages get_theme_mod( 'mytheme_user_page_1', '' ) is probably about right.

How do I set up the control?

$wp_customize->add_setting( 'mytheme_user_page_1', array(
    'default' => ''
) );

$wp_customize->add_control( array(
    'type' => 'range',
    'section' => 'my_custom_section', 
    'label' => __( 'Pick a page, bub' ),
    // something magical here 
) );

Update

I probably did not make this very clear (my bad) I also need to know how to populate the dropdown control. (Or not, see below - I might have missed a trick or two before).

Thanks to a link in the comments and then another link telling me to open my eyes (only far more polite than perhaps I deserved) I did see what was being pointed out. The manual says only this:

  • dropdown-pages (use the allow_addition argument to allow users to add new pages from the control)

That's every mention of both terms on the entire page. Maybe I am tired (that is quite likely) but I am struggling to understand how and why this works.

I only found it because I did a text search of the page for "drop" and got 1/1 results. Given the comments does this mean this populates a list of pages? And, if so, what on earth is the allow_addition argument? (And how do I use it?)

I am starting to suspect that I could chuck a bunch of stuff into my control and hope for the best. Something like this:

'type'=>'dropdown-pages',
'argument'=>'allow_addition', //maybe
'allow_addition'=>TRUE, //possibly

or

'type'=>array('dropdown-pages','allow_addition'), // ?? I'm just guessing

I would prefer to know why I have done it and what is happening here because I will feel obligated to support any themes I release. Responding to a question with, "TBH, I have no idea what I am doing" is not something I want in any possible future.

I apologise if I am being a bit slow today.

I would really appreciate someone taking a few minutes to help me understand. If you could explain the allow_addition argument while you are at it I will forever be in your debt.

What I am trying to do is offer three pages to highlight on the front page. To do this, I would like to offer users a dropdown list of all pages.

I have not worked out how to do this.

I know how to get the pages get_theme_mod( 'mytheme_user_page_1', '' ) is probably about right.

How do I set up the control?

$wp_customize->add_setting( 'mytheme_user_page_1', array(
    'default' => ''
) );

$wp_customize->add_control( array(
    'type' => 'range',
    'section' => 'my_custom_section', 
    'label' => __( 'Pick a page, bub' ),
    // something magical here 
) );

Update

I probably did not make this very clear (my bad) I also need to know how to populate the dropdown control. (Or not, see below - I might have missed a trick or two before).

Thanks to a link in the comments and then another link telling me to open my eyes (only far more polite than perhaps I deserved) I did see what was being pointed out. The manual says only this:

  • dropdown-pages (use the allow_addition argument to allow users to add new pages from the control)

That's every mention of both terms on the entire page. Maybe I am tired (that is quite likely) but I am struggling to understand how and why this works.

I only found it because I did a text search of the page for "drop" and got 1/1 results. Given the comments does this mean this populates a list of pages? And, if so, what on earth is the allow_addition argument? (And how do I use it?)

I am starting to suspect that I could chuck a bunch of stuff into my control and hope for the best. Something like this:

'type'=>'dropdown-pages',
'argument'=>'allow_addition', //maybe
'allow_addition'=>TRUE, //possibly

or

'type'=>array('dropdown-pages','allow_addition'), // ?? I'm just guessing

I would prefer to know why I have done it and what is happening here because I will feel obligated to support any themes I release. Responding to a question with, "TBH, I have no idea what I am doing" is not something I want in any possible future.

I apologise if I am being a bit slow today.

I would really appreciate someone taking a few minutes to help me understand. If you could explain the allow_addition argument while you are at it I will forever be in your debt.

Share Improve this question edited Jul 1, 2019 at 3:08 Matthew Brown aka Lord Matt asked Jul 1, 2019 at 1:29 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges 6
  • 1 See this. – Sally CJ Commented Jul 1, 2019 at 2:02
  • I know how to add a control. How do I add a list of all pages as a control? – Matthew Brown aka Lord Matt Commented Jul 1, 2019 at 2:14
  • 2 @MatthewBrownakaLordMatt See Sally's link. dropdown-pages is an accepted type for a control. – Jacob Peattie Commented Jul 1, 2019 at 2:20
  • How many times have I been on that page and not seen it? Thank you for being patient with me. I'm still struggling (see update) but I at least have a clue. – Matthew Brown aka Lord Matt Commented Jul 1, 2019 at 3:10
  • 1 You don’t need to populate it. Just set the type to dropdown-pages and you’ll get a dropdown with a list of pages. That’s it. I suggest just trying it. – Jacob Peattie Commented Jul 1, 2019 at 3:14
 |  Show 1 more comment

1 Answer 1

Reset to default 3

To add a Customiser control for selecting a page from a dropdown you just need to set the type argument to dropdown-pages:

$wp_customize->add_control( array(
    'type'    => 'dropdown-pages',
    'section' => 'my_custom_section', 
    'label'   => __( 'Pick a page, bub' ),
) );

This will give you a control that's a <select> dropdown with a list of pages automatically populated. When saved it will save the post ID of the page, or 0 if none is selected.

The 'allow_addition' argument is an additional argument that can be passed to $wp_customize->add_control() when using dropdown-pages that will allow the user to create a page from the Customiser, just like with the settings in the Home page settings section of the Customiser. You would use this argument like this:

$wp_customize->add_control( array(
    'type'           => 'dropdown-pages',
    'allow_addition' => true,
    'section'        => 'my_custom_section', 
    'label'          => __( 'Pick a page, bub' ),
) );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745355986a4624119.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信