• I am currently working on a webpage that show a slider in the homepage. To know which posts it should display there, it looks for posts marked as “featured” (which is a custom field) and it show the X more recent (where X is a number I set in code).

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 10,
        'meta_query' => array(
          array(
            'key' => 'sfeatured',
            'value' => 'super-sfeatured',
            'compare' => '='
          )
        ),
        'orderby'=>'date',
        'order'=>'DESC'
      );
      $the_query = new WP_Query( $args );

    I don’t like having to change the amount of posts shown in the slider from the code (now the amount is 10, as you see). I would like to have a “general” custom field in my WordPress admin page where I can set the number of slides I want to show.

    What is the best way to approach this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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 );
    
    • This reply was modified 6 years, 2 months ago by Jacob Peattie.
    Thread Starter dacevid

    (@dacevid)

    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.
    Moderator bcworkz

    (@bcworkz)

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding general custom fields’ is closed to new replies.