Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Add a new custom grid shortcode module
- // Usage [mjelectric_post_grid posts_per_page="4" term="uncategorized"]
- // You can also go to Visual Composer > Shortcode Mapper to add your custom module
- function mjelectric_post_grid_shortcode( $atts ) {
- // Parse your shortcode settings with it's defaults
- $atts = shortcode_atts( array(
- 'posts_per_page' => '-1',
- 'term' => ''
- ), $atts, 'mjelectric_post_grid' );
- // Extract shortcode atributes
- extract( $atts );
- // Define output var
- $output = '';
- // Define query
- $query_args = array(
- 'post_type' => 'post', // Change this to the type of post you want to show
- 'posts_per_page' => $posts_per_page,
- );
- // Query by term if defined
- if ( $term ) {
- $query_args['tax_query'] = array(
- array(
- 'taxonomy' => 'category',
- 'field' => 'ID',
- 'terms' => $term,
- ),
- );
- }
- // Query posts
- $custom_query = new WP_Query( $query_args );
- // Add content if we found posts via our query
- if ( $custom_query->have_posts() ) {
- // Open div wrapper around loop
- $output .= '<div class="recent-post-wrapp">';
- // Loop through posts
- while ( $custom_query->have_posts() ) {
- // Sets up post data so you can use functions like get_the_title(), get_permalink(), etc
- $custom_query->the_post();
- // This is the output for your entry so what you want to do for each post.
- $output .= '<div class="recent-post-grid-column">';
- $output .= '<div class="recent-post-grid-article-wrapp">';
- $output .= '<div class="recent-post-grid-img">';
- $output .= '<a class="recent-post-grid-link" href="' . get_the_permalink() . '"><div class="recent-post-thumbnail-bg" style="background-image: url(' . get_the_post_thumbnail_url() . ')" ></div></a>';
- $output .= '</div>';
- $output .= '<div class="recent-post-grid-content">';
- $output .= '<div class="recent-post-content-meta"><a class="author-info" href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '">' . get_the_author() . '</a><div class="posted-time">' . get_the_date('M j, D') . ' . ' . my_post_time_ago_function() . ' </div></div>';
- $output .= '<div class="recent-post-content-title"><a href="' . get_the_permalink() . '"><h4>' . get_the_title() . '</h4></a></div>';
- $output .= '<div class="recent-post-content-cat-meta">' . get_the_category_list( ', ', '', $custom_query->ID ) . '</div>';
- $output .= '</div>';
- $output .= '</div>';
- $output .= '</div>';
- }
- // Close div wrapper around loop
- $output .= '</div>';
- // Restore data
- wp_reset_postdata();
- }
- // Return your shortcode output
- return $output;
- }
- add_shortcode( 'mjelectric_post_grid', 'mjelectric_post_grid_shortcode' );
- function my_post_time_ago_function() {
- return sprintf( esc_html__( '%s ', 'textdomain' ), human_time_diff(get_the_time ( 'U' ), current_time( 'timestamp' ) ) );
- }
- function gt_get_post_view() {
- $count = get_post_meta( get_the_ID(), 'post_views_count', true );
- return "$count views";
- }
- function gt_set_post_view() {
- $key = 'post_views_count';
- $post_id = get_the_ID();
- $count = (int) get_post_meta( $post_id, $key, true );
- $count++;
- update_post_meta( $post_id, $key, $count );
- }
- function gt_posts_column_views( $columns ) {
- $columns['post_views'] = 'Views';
- return $columns;
- }
- function gt_posts_custom_column_views( $column ) {
- if ( $column === 'post_views') {
- echo gt_get_post_view();
- }
- }
- add_filter( 'manage_posts_columns', 'gt_posts_column_views' );
- add_action( 'manage_posts_custom_column', 'gt_posts_custom_column_views' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement