• Hey!

    Just discovered this plugin and I think it’s awesome! Clean, well-functioning, and not a bunch of bloat. I only miss one thing which would’ve greatly improved the functionality of it: custom post type/custom taxonomy support for the “magic slides” option.

    Currently it’s possible to choose categories of posts. But I would really like to display custom post types and certain taxonomies of said post type using the same intuitive and user friendly backend as the magic slides.

    I tried coding this function in but unfortunately didn’t get it to work as expected as I was missing some neccessary (un-minified) js so properly integrate it into the current plugin. The php functions for magic slides admin pages were fairly easily modified to add a multiselect with post types and the pulling alla taxonomies for the chosen post types, however. Let med know if you would like me to send my code and add the functionality to your plugin!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Isak

    (@tkeridun)

    Hey again!

    I decided to look at this again and picked up my modifications. this is what I’ve added so far:

    <?php
    
    /**
     * Adds admin functionality for the Recent Posts slide format.
     *
     * @since		1.7.1
     *
     * @package		Foyer
     * @subpackage	Foyer/admin
     * @author		Menno Luitjes <[email protected]>
     */
    class Foyer_Admin_Slide_Format_Recent_Posts {
    
    	/**
    	 * Saves additional data for the Recent Posts slide format.
    	 *
    	 * @since	1.7.1
    	 *
    	 * @param	int		$post_id	The ID of the post being saved.
    	 * @return	void
    	 */
    	static function save_slide( $post_id ) {
    		$slide_recent_posts_limit = intval( $_POST['slide_recent_posts_limit'] );
    		if ( empty( $slide_recent_posts_limit ) ) {
    			$slide_recent_posts_limit = '';
    		}
    
    		$slide_recent_posts_categories = '';
    		if (
    			! empty( $_POST['slide_recent_posts_categories'] ) &&
    			! empty( $_POST['slide_recent_posts_categories'][0] )
    		) {
    			$slide_recent_posts_categories = array_map( 'intval', $_POST['slide_recent_posts_categories'] );
    		}
    
    		$slide_recent_posts_display_thumbnail = '';
    		if ( isset( $_POST['slide_recent_posts_display_thumbnail'] ) ) {
    			$slide_recent_posts_display_thumbnail = intval( $_POST['slide_recent_posts_display_thumbnail'] );
    			if ( empty( $slide_recent_posts_display_thumbnail ) ) {
    				$slide_recent_posts_display_thumbnail = '';
    			}
    		}
    
    		$slide_recent_posts_use_excerpt = '';
    		if ( isset( $_POST['slide_recent_posts_use_excerpt'] ) ) {
    			$slide_recent_posts_use_excerpt = intval( $_POST['slide_recent_posts_use_excerpt'] );
    			if ( empty( $slide_recent_posts_use_excerpt ) ) {
    				$slide_recent_posts_use_excerpt = '';
    			}
    		}
    		
    <strong>		$slide_recent_posts_posttype = '';
    		if (
    			! empty( $_POST['slide_recent_posts_posttype'] ) &&
    			! empty( $_POST['slide_recent_posts_posttype'][0] )
    		) {
    			$slide_recent_posts_posttype = array_map( 'intval', $_POST['slide_recent_posts_posttype'] );
    		}</strong>
    		
    		update_post_meta( $post_id, 'slide_recent_posts_limit', $slide_recent_posts_limit );
    		update_post_meta( $post_id, 'slide_recent_posts_categories', $slide_recent_posts_categories );
    		update_post_meta( $post_id, 'slide_recent_posts_display_thumbnail', $slide_recent_posts_display_thumbnail );
    		update_post_meta( $post_id, 'slide_recent_posts_use_excerpt', $slide_recent_posts_use_excerpt );
    <strong>		update_post_meta( $post_id, 'slide_recent_posts_posttype', $slide_recent_posts_posttype );
    	}</strong>
    
    	/**
    	 * Outputs the meta box for the Recent Posts slide format.
    	 *
    	 * @since	1.7.1
    	 *
    	 * @param	WP_Post	$post	The post of the current slide.
    	 * @return	void
    	 */
    	static function slide_meta_box( $post ) {
    
    		$slide_recent_posts_limit = intval( get_post_meta( $post->ID, 'slide_recent_posts_limit', true ) );
    		
    <strong>		$slide_recent_posts_posttype = get_post_meta( $post->ID, 'slide_recent_posts_posttype', true );
    		if ( empty( $slide_recent_posts_posttype ) ) {
    			$slide_recent_posts_posttype = array();
    		}</strong>
    		
    		$slide_recent_posts_categories = get_post_meta( $post->ID, 'slide_recent_posts_categories', true );
    		if ( empty( $slide_recent_posts_categories ) ) {
    			$slide_recent_posts_categories = array();
    		}
    
    		$slide_recent_posts_display_thumbnail = get_post_meta( $post->ID, 'slide_recent_posts_display_thumbnail', true );
    		$slide_recent_posts_use_excerpt = get_post_meta( $post->ID, 'slide_recent_posts_use_excerpt', true );
    
    		?><table class="form-table">
    			<tbody>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_limit"><?php _e( 'Display a maximum of', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<input type="number" min="0" step="1" id="slide_recent_posts_limit" name="slide_recent_posts_limit" class="small-text" value="<?php echo $slide_recent_posts_limit; ?>" /> <?php _e( 'posts', 'foyer' ); ?>
    					</td>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_posttype[]"><?php _e( 'Only display posts from posttype', 'foyer' ); ?></label>
    					</th>
    					<strong><td>
    						<select name="slide_recent_posts_posttype[]" multiple><?php
    							foreach ( get_post_types( array(), 'objects' ) as $posttype ) {
    								?><option value="<?php echo intval( $posttype->id ); ?>" <?php if ( in_array( $posttype->id, $slide_recent_posts_posttype ) ) { ?>selected<?php } ?>><?php echo esc_html( $posttype->name ); ?></option><?php
    							}
    						?></select>
    					</td></strong>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_categories[]"><?php _e( 'Only display posts from categories', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<select name="slide_recent_posts_categories[]" multiple><?php
    							foreach ( get_categories( array( 'hide_empty' => false ) ) as $cat ) {
    								?><option value="<?php echo intval( $cat->term_id ); ?>" <?php if ( in_array( $cat->term_id, $slide_recent_posts_categories ) ) { ?>selected<?php } ?>><?php echo esc_html( $cat->name ); ?></option><?php
    							}
    							<strong>foreach ( get_terms( array(  'hide_empty' => false, ) ) as $term ) {
    								?><option value="<?php echo intval( $term->id ); ?>" <?php if ( in_array( $term->id, $slide_recent_posts_categories ) ) { ?>selected<?php } ?>><?php echo esc_html( $term->name ); ?></option><?php
    							}</strong>
    						?></select>
    					</td>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_display_thumbnail"><?php _e( 'Display featured image?', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<input type="checkbox" name="slide_recent_posts_display_thumbnail" id="slide_recent_posts_display_thumbnail"
    							value="1" <?php checked( $slide_recent_posts_display_thumbnail, 1 ); ?> />
    						<span><?php _e( 'Yes, display the featured image.', 'foyer' ); ?></span>
    					</td>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_use_excerpt"><?php _e( 'Use excerpt?', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<input type="checkbox" name="slide_recent_posts_use_excerpt" id="slide_recent_posts_use_excerpt"
    							value="1" <?php checked( $slide_recent_posts_use_excerpt, 1 ); ?> />
    						<span><?php _e( 'Yes, use the manual excerpt instead of the post content.', 'foyer' ); ?></span>
    					</td>
    				</tr>
    			</tbody>
    		</table><?php
    	}
    }

    My additions in bold above.

    This is not a complete solution.

    Suggestions for completion so far:

    • Only display post-types with post/page capabilities, using get_post_types_by_support() instead of get_post_types. (I tried it but didn’t get it to work for me yet).
    • Add jQuery to update the categories/terms list based on selected posttypes. Only display terms/categories that apply to chosen posttypes in the multiselect list.
    • In the above code, selected posttypes and categories don’t seem to get saved when saving the slide. Either that, or the saved values don’t display as selected when entering the edit slide screen. I wasn’t on a proper development platform when writing this so I couldn’t verify which. Anyway, the needs to be fixed.

    If the three points above are resolved, then I would deem this feature complete and ready for shipping. I’ll continue working on it but if you have time to help or just discuss it that would be appreciated.

    Thread Starter Isak

    (@tkeridun)

    Another little update.

    class-foyer-admin-slide-format-recent-posts.php

    
    <?php
    
    /**
     * Adds admin functionality for the Recent Posts slide format.
     *
     * @since		1.7.1
     *
     * @package		Foyer
     * @subpackage	Foyer/admin
     * @author		Menno Luitjes <[email protected]>
     */
    class Foyer_Admin_Slide_Format_Recent_Posts {
    
    	/**
    	 * Saves additional data for the Recent Posts slide format.
    	 *
    	 * @since	1.7.1
    	 *
    	 * @param	int		$post_id	The ID of the post being saved.
    	 * @return	void
    	 */
    	static function save_slide( $post_id ) {
    		$slide_recent_posts_limit = intval( $_POST['slide_recent_posts_limit'] );
    		if ( empty( $slide_recent_posts_limit ) ) {
    			$slide_recent_posts_limit = '';
    		}
    
    		$slide_recent_posts_categories = '';
    		if (
    			! empty( $_POST['slide_recent_posts_categories'] ) &&
    			! empty( $_POST['slide_recent_posts_categories'][0] )
    		) {
    			$slide_recent_posts_categories = array_map( 'intval', $_POST['slide_recent_posts_categories'] );
    		}
    
    		$slide_recent_posts_display_thumbnail = '';
    		if ( isset( $_POST['slide_recent_posts_display_thumbnail'] ) ) {
    			$slide_recent_posts_display_thumbnail = intval( $_POST['slide_recent_posts_display_thumbnail'] );
    			if ( empty( $slide_recent_posts_display_thumbnail ) ) {
    				$slide_recent_posts_display_thumbnail = '';
    			}
    		}
    
    		$slide_recent_posts_use_excerpt = '';
    		if ( isset( $_POST['slide_recent_posts_use_excerpt'] ) ) {
    			$slide_recent_posts_use_excerpt = intval( $_POST['slide_recent_posts_use_excerpt'] );
    			if ( empty( $slide_recent_posts_use_excerpt ) ) {
    				$slide_recent_posts_use_excerpt = '';
    			}
    		}
    		
    		$slide_recent_posts_posttype = '';
    		if (
    			! empty( $_POST['slide_recent_posts_posttypes'] ) &&
    			! empty( $_POST['slide_recent_posts_posttypes'][0] )
    		) {
    			$slide_recent_posts_posttypes = array_map( 'strval', $_POST['slide_recent_posts_posttypes'] );
    		}
    		
    		update_post_meta( $post_id, 'slide_recent_posts_limit', $slide_recent_posts_limit );
    		update_post_meta( $post_id, 'slide_recent_posts_categories', $slide_recent_posts_categories );
    		update_post_meta( $post_id, 'slide_recent_posts_display_thumbnail', $slide_recent_posts_display_thumbnail );
    		update_post_meta( $post_id, 'slide_recent_posts_use_excerpt', $slide_recent_posts_use_excerpt );
    		update_post_meta( $post_id, 'slide_recent_posts_posttypes', $slide_recent_posts_posttypes );
    	}
    
    	/**
    	 * Outputs the meta box for the Recent Posts slide format.
    	 *
    	 * @since	1.7.1
    	 *
    	 * @param	WP_Post	$post	The post of the current slide.
    	 * @return	void
    	 */
    	static function slide_meta_box( $post ) {
    
    		$slide_recent_posts_limit = intval( get_post_meta( $post->ID, 'slide_recent_posts_limit', true ) );
    		
    		$slide_recent_posts_posttypes = get_post_meta( $post->ID, 'slide_recent_posts_posttypes', true );
    		if ( empty( $slide_recent_posts_posttypes ) ) {
    			$slide_recent_posts_posttypes = array();
    		}
    		
    		$slide_recent_posts_categories = get_post_meta( $post->ID, 'slide_recent_posts_categories', true );
    		if ( empty( $slide_recent_posts_categories ) ) {
    			$slide_recent_posts_categories = array();
    		}
    
    		$slide_recent_posts_display_thumbnail = get_post_meta( $post->ID, 'slide_recent_posts_display_thumbnail', true );
    		$slide_recent_posts_use_excerpt = get_post_meta( $post->ID, 'slide_recent_posts_use_excerpt', true );
    
    		?><table class="form-table">
    			<tbody>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_limit"><?php _e( 'Display a maximum of', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<input type="number" min="0" step="1" id="slide_recent_posts_limit" name="slide_recent_posts_limit" class="small-text" value="<?php echo $slide_recent_posts_limit; ?>" /> <?php _e( 'posts', 'foyer' ); ?>
    					</td>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_posttypes[]"><?php _e( 'Only display posts from posttype', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<select name="slide_recent_posts_posttypes[]" multiple><?php
    							foreach ( get_post_types( array(), 'objects' ) as $posttype ) {
    								?><option value="<?php echo strval( $posttype->name ); ?>" <?php if ( in_array( strval( $posttype->name ), $slide_recent_posts_posttypes ) ) { ?>selected<?php } ?>><?php echo esc_html( $posttype->label ); ?></option><?php
    							}
    						?></select>
    					</td>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_categories[]"><?php _e( 'Only display posts from categories', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<select name="slide_recent_posts_categories[]" multiple><?php
    							foreach ( get_terms( array(  'hide_empty' => false, ) ) as $term ) {
    								?><option value="<?php echo intval( $term->term_id ); ?>" <?php if ( in_array( $term->term_id, $slide_recent_posts_categories ) ) { ?>selected<?php } ?>><?php echo esc_html( $term->name ); ?></option><?php
    							}
    						?></select>
    					</td>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_display_thumbnail"><?php _e( 'Display featured image?', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<input type="checkbox" name="slide_recent_posts_display_thumbnail" id="slide_recent_posts_display_thumbnail"
    							value="1" <?php checked( $slide_recent_posts_display_thumbnail, 1 ); ?> />
    						<span><?php _e( 'Yes, display the featured image.', 'foyer' ); ?></span>
    					</td>
    				</tr>
    				<tr>
    					<th scope="row">
    						<label for="slide_recent_posts_use_excerpt"><?php _e( 'Use excerpt?', 'foyer' ); ?></label>
    					</th>
    					<td>
    						<input type="checkbox" name="slide_recent_posts_use_excerpt" id="slide_recent_posts_use_excerpt"
    							value="1" <?php checked( $slide_recent_posts_use_excerpt, 1 ); ?> />
    						<span><?php _e( 'Yes, use the manual excerpt instead of the post content.', 'foyer' ); ?></span>
    					</td>
    				</tr>
    			</tbody>
    		</table><?php
    	}
    }
    

    recent-posts.php

    
    <?php
    /**
     * Recent Posts slide format template.
     *
     * @since	1.7.1
     */
    
    $slide = new Foyer_Slide( get_the_id() );
    
    $slide_recent_posts_limit = intval( get_post_meta( $slide->ID, 'slide_recent_posts_limit', true ) );
    $slide_recent_posts_categories = get_post_meta( $slide->ID, 'slide_recent_posts_categories', true );
    
    $slide_recent_posts_display_thumbnail = get_post_meta( $slide->ID, 'slide_recent_posts_display_thumbnail', true );
    $slide_recent_posts_use_excerpt = get_post_meta( $slide->ID, 'slide_recent_posts_use_excerpt', true );
    
    $slide_recent_posts_posttypes = get_post_meta( $slide->ID, 'slide_recent_posts_posttypes', true );
    
    // Prepare categories and limit for get_posts() query
    $post_args = array();
    
    if ( ! empty( $slide_recent_posts_posttypes ) ) {
    	$post_args['post_type'] = array_map( 'strval', $slide_recent_posts_posttypes );
    }
    
    if ( ! empty( $slide_recent_posts_categories ) ) {
    	$post_args['category__in'] = array_map( 'intval', $slide_recent_posts_categories );
    }
    
    if ( ! empty( $slide_recent_posts_limit ) ) {
    	$post_args['posts_per_page'] = $slide_recent_posts_limit;
    }
    else {
    	$post_args['nopaging'] = true;
    }
    
    foreach ( get_posts( $post_args ) as $slide_post ) {
    
    	if ( ! empty( $slide_recent_posts_use_excerpt ) ) {
    		$content = apply_filters( 'the_content', $slide_post->post_excerpt );
    	}
    	else {
    		$content = apply_filters( 'the_content', $slide_post->post_content );
    	}
    
    	?><div<?php $slide->classes(); ?><?php $slide->data_attr(); ?>>
    		<div class="inner">
    			<?php if ( ! empty( $slide_post->ID ) ) { ?>
    				<?php if ( ! empty( $slide_recent_posts_display_thumbnail ) && $attachment_id = get_post_thumbnail_id( $slide_post->ID ) ) { ?>
    					<figure>
    						<?php echo wp_get_attachment_image( $attachment_id, 'foyer' ); ?>
    					</figure>
    				<?php } ?>
    				<div class="foyer-slide-fields">
    					<div class="foyer-slide-field foyer-slide-field-title"><span><?php echo get_the_title( $slide_post->ID ); ?></span></div>
    					<div class="foyer-slide-field foyer-slide-field-date"><span><?php echo get_the_date( false, $slide_post->ID ); ?></span></div>
    					<?php if ( ! empty( $content ) ) { ?>
    						<div class="foyer-slide-field foyer-slide-field-content"><?php echo $content; ?></div>
    					<?php } ?>
    				</div>
    			<?php } ?>
    		</div>
    		<?php $slide->background(); ?>
    	</div><?php
    }
    

    A little cleaner now. Saving the meta box properties as it should, and no longer displaying duplicate post categories.

    This kinda works, however if I choose a custom term it fails. Choosing a custom posttype without a selected “category” (or rather term in this case) works beautifully. I’ll keep looking into what I might’ve missed.

    A jquery script for updating the options in the categories multiselect depending on which posttypes are chosen would be a nice feature though. I’m going to keep thinking about a nice way of solving this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Magic slides with custom post types’ is closed to new replies.