Last active
April 25, 2021 03:23
-
-
Save MWDelaney/10114310 to your computer and use it in GitHub Desktop.
WordPress Custom Post Type and Shortcode for Portfolio Items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Custom Post Type - Portfolio Items | |
| Plugin URI: | |
| Description: Custom Post Types and shortcodes for Portfolio Items. | |
| Version: 1.0 | |
| Author: Michael W. Delaney | |
| Author URI: | |
| */ | |
| add_shortcode( 'portfolio', 'portfolio_shortcode' ); | |
| function add_menu_icons_styles_portfolio(){ | |
| ?> | |
| <style> | |
| #adminmenu .menu-icon-portfolio div.wp-menu-image:before { | |
| content: "\f322"; | |
| } | |
| </style> | |
| <?php | |
| } | |
| add_action( 'admin_head', 'add_menu_icons_styles_portfolio' ); | |
| add_action( 'init', 'portfolio_custom_init' ); | |
| function portfolio_custom_init() { | |
| $labels = array( | |
| 'name' => _x('Portfolio', 'post type general name'), | |
| 'singular_name' => _x('Portfolio Item', 'post type singular name'), | |
| 'add_new' => _x('Add New', 'Portfolio Item'), | |
| 'add_new_item' => __('Add New Portfolio Item'), | |
| 'edit_item' => __('Edit Portfolio Item'), | |
| 'new_item' => __('New Portfolio Item'), | |
| 'all_items' => __('All Portfolio Items'), | |
| 'view_item' => __('View Portfolio Items'), | |
| 'search_items' => __('Search Portfolio Items'), | |
| 'not_found' => __('No portfolio items found'), | |
| 'not_found_in_trash' => __('No potfolio items found in Trash'), | |
| 'parent_item_colon' => '', | |
| 'menu_name' => 'Portfolio' | |
| ); | |
| $args = array( | |
| 'labels' => $labels, | |
| 'public' => true, | |
| 'publicly_queryable' => true, | |
| 'show_ui' => true, | |
| 'show_in_menu' => true, | |
| 'query_var' => true, | |
| 'rewrite' => true, | |
| 'capability_type' => 'post', | |
| 'has_archive' => false, | |
| 'hierarchical' => false, | |
| 'menu_position' => 20, | |
| 'supports' => array( 'title', 'editor', 'revisions', 'page-attributes', 'thumbnail', 'excerpt' ), | |
| 'menu_icon' => '', | |
| ); | |
| register_post_type('portfolio',$args); | |
| } | |
| // Add Shortcode | |
| function portfolio_shortcode( $atts ) { | |
| extract( shortcode_atts( array( | |
| "name" => false, | |
| ), $atts ) ); | |
| global $cfs; | |
| $ul_class = 'list-portfolio'; | |
| $ul_class .= ( $name ) ? '' : ' row'; | |
| $li_class = 'list-portfolio-item'; | |
| $col_class = ( $name ) ? '' : ' col-sm-6'; | |
| $lis = ''; | |
| if($name) { | |
| $query = new WP_Query( array( | |
| 'post_type' => 'portfolio', | |
| 'name' => $name, | |
| 'order' => 'ASC', | |
| ) ); | |
| } else { | |
| $query = new WP_Query( array( | |
| 'post_type' => 'portfolio', | |
| 'orderby' => 'menu_order', | |
| 'order' => 'ASC', | |
| ) ); | |
| } | |
| while ( $query->have_posts() ) : $query->the_post(); | |
| $lis .= sprintf( | |
| '<div class="%s"><a class="%s" href="%s"> %s <h4>%s</h4><p>%s</p></a></div>', | |
| esc_attr( $col_class ), | |
| esc_attr( $li_class ), | |
| esc_url( get_permalink() ), | |
| get_the_post_thumbnail( $post_id, 'thumbnail', array('class' => 'pull-left portfolio-thumb img-circle') ), | |
| get_the_title(), | |
| get_the_excerpt() | |
| ); | |
| endwhile; | |
| wp_reset_query(); | |
| return sprintf( | |
| '<div class="%s">%s</div>', | |
| esc_attr( $ul_class ), | |
| $lis | |
| ); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment