'widget_custom_post_widget', 'description' => __( 'Displays custom post content in a widget', 'custom-post-widget' ) ); parent::__construct( 'custom_post_widget', __( 'Content Block', 'custom-post-widget' ), $widget_ops ); } function form( $instance ) { $custom_post_id = ''; // Initialize the variable if (isset($instance['custom_post_id'])) { $custom_post_id = esc_attr($instance['custom_post_id']); }; $show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : true; $show_featured_image = isset( $instance['show_featured_image'] ) ? $instance['show_featured_image'] : true; $apply_content_filters = isset( $instance['apply_content_filters'] ) ? $instance['apply_content_filters'] : true; ?>
' . __( 'Edit Content Block', 'custom-post-widget' ) . '' ; ?>
id="get_field_id( 'show_custom_post_title' ); ?>" name="get_field_name( 'show_custom_post_title' ); ?>" />
id="get_field_id( 'show_featured_image' ); ?>" name="get_field_name( 'show_featured_image' ); ?>" />
id="get_field_id( 'apply_content_filters' ); ?>" name="get_field_name( 'apply_content_filters' ); ?>" />
post_content; if ( $post_status == 'publish' ) { // Display custom widget frontend if ( $located = locate_template( 'custom-post-widget.php' ) ) { require $located; return; } if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected $content = apply_filters( 'the_content', $content); } echo $before_widget; if ( $show_custom_post_title ) { echo $before_title . apply_filters( 'widget_title',$content_post->post_title) . $after_title; // This is the line that displays the title (only if show title is set) } if ( $show_featured_image ) { echo get_the_post_thumbnail( $content_post -> ID ); } echo do_shortcode( $content ); // This is where the actual content of the custom post is being displayed echo $after_widget; } } } // Create the Content Block custom post type function cpw_post_type_init() { $labels = array( 'name' => _x( 'Content Blocks', 'post type general name', 'custom-post-widget' ), 'singular_name' => _x( 'Content Block', 'post type singular name', 'custom-post-widget' ), 'plural_name' => _x( 'Content Blocks', 'post type plural name', 'custom-post-widget' ), 'add_new' => _x( 'Add Content Block', 'block', 'custom-post-widget' ), 'add_new_item' => __( 'Add New Content Block', 'custom-post-widget' ), 'edit_item' => __( 'Edit Content Block', 'custom-post-widget' ), 'new_item' => __( 'New Content Block', 'custom-post-widget' ), 'view_item' => __( 'View Content Block', 'custom-post-widget' ), 'search_items' => __( 'Search Content Blocks', 'custom-post-widget' ), 'not_found' => __( 'No Content Blocks Found', 'custom-post-widget' ), 'not_found_in_trash' => __( 'No Content Blocks found in Trash', 'custom-post-widget' ) ); $content_block_public = false; // added to make this a filterable option $options = array( 'labels' => $labels, 'public' => apply_filters( 'content_block_post_type', $content_block_public ), 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_icon' => 'dashicons-screenoptions', 'supports' => array( 'title','editor','revisions','thumbnail','author' ) ); register_post_type( 'content_block',$options ); } add_action( 'init', 'cpw_post_type_init' ); function content_block_messages( $messages ) { $messages['content_block'] = array( 0 => '', 1 => current_user_can( 'edit_theme_options' ) ? sprintf( __( 'Content Block updated. Manage Widgets', 'custom-post-widget' ), esc_url( 'widgets.php' ) ) : sprintf( __( 'Content Block updated.', 'custom-post-widget' ), esc_url( 'widgets.php' ) ), 2 => __( 'Custom field updated.', 'custom-post-widget' ), 3 => __( 'Custom field deleted.', 'custom-post-widget' ), 4 => __( 'Content Block updated.', 'custom-post-widget' ), 5 => isset($_GET['revision']) ? sprintf( __( 'Content Block restored to revision from %s', 'custom-post-widget' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => current_user_can( 'edit_theme_options' ) ? sprintf( __( 'Content Block published. Manage Widgets', 'custom-post-widget' ), esc_url( 'widgets.php' ) ) : sprintf( __( 'Content Block published.', 'custom-post-widget' ), esc_url( 'widgets.php' ) ), 7 => __( 'Block saved.', 'custom-post-widget' ), 8 => current_user_can( 'edit_theme_options' ) ? sprintf( __( 'Content Block submitted. Manage Widgets', 'custom-post-widget' ), esc_url( 'widgets.php' ) ) : sprintf( __( 'Content Block submitted.', 'custom-post-widget' ), esc_url( 'widgets.php' ) ), 9 => sprintf( __( 'Content Block scheduled for: %1$s.', 'custom-post-widget' ), date_i18n( __( 'M j, Y @ G:i' , 'custom-post-widget' ), strtotime(isset($post->post_date) ? $post->post_date : null) ), esc_url( 'widgets.php' ) ), 10 => current_user_can( 'edit_theme_options' ) ? sprintf( __( 'Content Block draft updated. Manage Widgets', 'custom-post-widget' ), esc_url( 'widgets.php' ) ) : sprintf( __( 'Content Block draft updated.', 'custom-post-widget' ), esc_url( 'widgets.php' ) ), ); return $messages; } add_filter( 'post_updated_messages', 'content_block_messages' ); // Add the ability to display the content block in a reqular post using a shortcode function custom_post_widget_shortcode( $atts ) { extract( shortcode_atts( array( 'id' => '', 'slug' => '', 'class' => 'content_block', 'suppress_content_filters' => 'no', 'featured_image' => 'no', 'featured_image_size' => 'medium', 'title' => 'no', 'title_tag' => 'h3' ), $atts ) ); if ( $slug ) { $block = get_page_by_path( $slug, OBJECT, 'content_block' ); if ( $block ) { $id = $block->ID; } } $content = ""; if( $id != "" ) { $args = array( 'post__in' => array( $id ), 'post_type' => 'content_block', ); $content_post = get_posts( $args ); foreach( $content_post as $post ) : $content .= '