Skip to content

Instantly share code, notes, and snippets.

@Manoz
Last active January 1, 2016 18:09
Show Gist options
  • Save Manoz/8181799 to your computer and use it in GitHub Desktop.
Save Manoz/8181799 to your computer and use it in GitHub Desktop.
Wordpress full-width post width get_post_meta() and 'add_meta_boxes'. Works with my css grid system. Screenshot : http://i.imgur.com/CgTaLuH.png
<?php while (have_posts()) : the_post(); ?>
<div class="main-content col-1-1">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Your single post content
</article>
</div>
<?php endwhile; ?>
<?php while (have_posts()) : the_post(); ?>
<div class="main-content col-9-12">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Your single post content
</article>
</div>
<?php get_sidebar(); ?>
<?php endwhile; ?>
/**
* Let's build this fucking 'full-width' post feature.
* It took me about 2 hours on stackoverflow to find a fucking solution.
*/
add_action( 'add_meta_boxes', 'wpsb_meta_box' );
function wpsb_meta_box() {
add_meta_box( 'wpsb-meta-box', 'Full width post?', 'wpsb_meta_box_callback', 'post', 'normal', 'high' );
}
function wpsb_meta_box_callback( $post ) {
$values = get_post_custom( $post->ID );
$check = isset( $values['wpsb_meta_box_check'] ) ? esc_attr( $values['wpsb_meta_box_check'][0] ) : '';
wp_nonce_field( 'wpsb_meta_box_nonce', 'meta_box_nonce' );
?>
<p>Hide the fucking sidebar for this post?</p>
<p>
<input type="checkbox" name="wpsb_meta_box_check" id="wpsb_meta_box_check" <?php checked( $check, 'on' ); ?> />
<label for="wpsb_meta_box_check">Full-width post pls!</label>
</p>
<?php
}
add_action( 'save_post', 'wpsb_meta_box_save' );
function wpsb_meta_box_save( $post_id ) {
// Bail if we're doing an auto save (yep, i know, this is crappy)
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// If our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'wpsb_meta_box_nonce' ) ) return;
// If our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
/* Now we can actually save our checkbox.
* In your database, the meta will be saved in the table 'wp_postmeta'.
* You can see that the 'meta_value' for your 'post_id' is set to 'on' or 'off'.
*/
$chk = ( isset( $_POST['wpsb_meta_box_check'] ) && $_POST['wpsb_meta_box_check'] ) ? 'on' : 'off';
update_post_meta( $post_id, 'wpsb_meta_box_check', $chk );
}
<?php
/**
* @package WPS-Blog
* @since 1.0
*/
get_template_part('templates/header');
/* If the post has a meta_value set to 'on' in database,
* it means the post is 'full width' so we need the sidebar (content-single.php)
*
* If the post don't have a meta_value set to 'on' in database,
* it means the post is not full width so we don't need the sidebar (content-single-full.php)
*/
$key = get_post_meta( get_the_ID(), 'wpsb_meta_box_check', true );
if( $key == 'on' ) {
get_template_part('templates/content', 'single-full');
} else {
get_template_part('templates/content', 'single');
}
get_template_part('templates/footer');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment