Skip to content

Instantly share code, notes, and snippets.

@szelag-michal
Last active June 27, 2017 22:26
Show Gist options
  • Save szelag-michal/756c73709592ea21c55b8cd8f836cb6f to your computer and use it in GitHub Desktop.
Save szelag-michal/756c73709592ea21c55b8cd8f836cb6f to your computer and use it in GitHub Desktop.
Show Post / Page / Custom Post Type to Home Page
//Show on homepage
function show_on_homepage() {
$post_types = array('post', 'page');
add_meta_box(
'meta-box-id', __( 'Show On Home Page', 'show_on_homepage' ),
'show_on_homepage_callback',
$post_types,
'side');
}
add_action( 'add_meta_boxes', 'show_on_homepage' );
function show_on_homepage_callback( $post ) {
global $post;
$custom = get_post_custom($post->ID);
$show_on_homepage = $custom["show_on_homepage"][0];
$show_on_homepage_value = get_post_meta($post->ID, 'show_on_homepage', true);
if($show_on_homepage_value == "yes") $show_on_homepage_checked = 'checked="checked"';
echo '<label><input type="checkbox" name="show_on_homepage" value="yes"'.$show_on_homepage_checked.'/>Show</label>';
}
function save_show_on_homepage( $post_id ) {
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, "show_on_homepage", $_POST["show_on_homepage"]);
}
add_action( 'save_post', 'save_show_on_homepage' );
<?php
$post_types = array('post', 'page');
$args = array (
'showposts' => 10,
'post_type' => $post_types,
'meta_key' => 'show_on_homepage');
$query = new WP_Query($args);
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment