Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Created March 11, 2018 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/9e734713a2eb4ad5428971ccc8c210ab to your computer and use it in GitHub Desktop.
Save nfsarmento/9e734713a2eb4ad5428971ccc8c210ab to your computer and use it in GitHub Desktop.
CPT metabox check option to show CPT post on page template
<article <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>">
<header class="heading-cpt">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'full' );
}
?>
<div class="hover-btn-cpt hidden">
<a class="btn-cpt" href="<?php the_permalink(); ?>"><?php _e( 'READ', 'genium' ); ?></a>
<i class="fa fa-angle-right"></i>
</div>
</header>
<div class="entry-summary">
<h4 class="entry-title"><?php the_title(); ?></h4>
</div>
<div class="entry-author">
<p><?php echo __( 'Author:', 'genium' ); ?> <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" rel="author" class="fn"><?php echo get_the_author(); ?></a></p>
</div>
</a>
</article>
/**
* Show post on home page check metabox on cpt events
*/
add_action('post_submitbox_misc_actions', 'metabox_reports');
add_action('save_post', 'metabox_reports_save');
function metabox_reports()
{
$post_id = get_the_ID();
if (get_post_type($post_id) != 'reports') {
return;
}
$value = get_post_meta($post_id, '_reports_custom_field', true);
wp_nonce_field('my_custom_nonce_'.$post_id, 'my_custom_nonce');
?>
<div class="misc-pub-section misc-pub-section-last">
<label><input type="checkbox" value="1" <?php checked($value, true, true); ?> name="_reports_custom_field" /><?php _e('Show on home page', 'pmg'); ?></label>
</div>
<?php
}
function metabox_reports_save($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (
!isset($_POST['my_custom_nonce']) ||
!wp_verify_nonce($_POST['my_custom_nonce'], 'my_custom_nonce_'.$post_id)
) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['_reports_custom_field'])) {
update_post_meta($post_id, '_reports_custom_field', $_POST['_reports_custom_field']);
} else {
delete_post_meta($post_id, '_reports_custom_field');
}
}
<?php
/*
Template Name: Template page
*/
?>
<?php
$args = array(
'post_type' => 'reports',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_reports_custom_field',
'value' => '1'
)
)
);
$testimonials = new WP_Query( $args );
if( $testimonials->have_posts() ) :
?>
<?php
$i = 1;
while( $testimonials->have_posts() ) :
$testimonials->the_post();
?>
<div class="col-md-4 col-sm-12 no-padding no-gutter hover-<?php echo $i; ?>">
<?php get_template_part( 'partials/content', 'reports' ); ?>
</div>
<?php $i++;
endwhile;
wp_reset_postdata();
?>
<?php
else :
esc_html_e( 'No testimonials in the diving taxonomy!', 'genium' );
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment