Skip to content

Instantly share code, notes, and snippets.

@johnie
Last active August 29, 2015 14:21
Show Gist options
  • Save johnie/6c37acfced9b7d1f73a9 to your computer and use it in GitHub Desktop.
Save johnie/6c37acfced9b7d1f73a9 to your computer and use it in GitHub Desktop.
Custom Controller for latest posts
<?php
$wp_customize->add_control( new WP_Customize_Latest_Post_Control( $wp_customize, 'latest_post_control', array(
'label' => __( 'Select A Featured Post', 'mytheme' ),
'section' => 'header_section',
'settings' => 'featured_post',
'post_type' => 'page'
)));
<?php
if( class_exists( 'WP_Customize_Control' ) ):
class WP_Customize_Latest_Post_Control extends WP_Customize_Control {
public $type = 'latest_post_dropdown';
public $post_type = 'post';
public function render_content() {
$latest = new WP_Query( array(
'post_type' => $this->post_type,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
));
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<select <?php $this->link(); ?>>
<?php
while( $latest->have_posts() ) {
$latest->the_post();
echo "<option " . selected( $this->value(), get_the_ID() ) . " value='" . get_the_ID() . "'>" . the_title( '', '', false ) . "</option>";
}
?>
</select>
</label>
<?php
}
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment