I’d suggest using the Customize API to add a section and field to the Customizer for controlling this.
So for example, you could add a Section for the Slider:
$wp_customize->add_section( 'my_slider', array(
'title' => 'Slider',
'priority' => 160,
) );
Then a Setting and Control for number of slides:
$wp_customize->add_setting( 'my_slider_number' );
$wp_customize->add_control( 'my_slider_number', array(
'type' => 'number',
'section' => 'my_slider',
'label' => 'Number of slides',
) );
Then you can use the saved value with get_theme_mod()
:
$args = array(
'post_type' => 'post',
'posts_per_page' => get_theme_mod( 'my_slider_number', 10 ),
'orderby' => 'date',
'order' => 'DESC'
'meta_query' => array(
array(
'key' => 'sfeatured',
'value' => 'super-sfeatured',
'compare' => '='
)
),
);
$the_query = new WP_Query( $args );
Thank you for your answer @jakept
I’ve had another idea but it doesn’t seem to work even if it should. Could you help me?
as I am using ACF plugin, I have created a new custom field named ‘slides_number’, and I have added to one of my pages (the one with ID=194). Then in my view I do this:
$slides_number = get_field('slides_number', 194);
$args = array(
'post_type' => 'post',
'posts_per_page' => $slides_number,
'meta_query' => array(
array(
'key' => 'sfeatured',
'value' => 'super-sfeatured',
'compare' => '='
)
),
'orderby'=>'date',
'order'=>'DESC'
);
$the_query = new WP_Query( $args );
But it doesn’t seem to get the value. What am I missing?
-
This reply was modified 6 years, 2 months ago by dacevid.
Are you sure $slides_number has no value? Or could it be that it has no effect in the query? This can happen when queries are overridden in the “pre_get_posts” action.
Try adding this line after getting the field:
echo "Number of slides: $slides_number<br>\n";
Upload the edited template, then reload the page. The output should appear on the page somewhere, including the field value. If there is no field value, there is a problem with ACF functionality. If there is a value but the slides returned count does not match, there is likely interfering code added through “pre_get_posts” action.