Advertisement
alamin_89

Recent Post Shortcode

Apr 27th, 2020
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. // Add a new custom grid shortcode module
  2. // Usage [mjelectric_post_grid posts_per_page="4" term="uncategorized"]
  3. // You can also go to Visual Composer > Shortcode Mapper to add your custom module
  4. function mjelectric_post_grid_shortcode( $atts ) {
  5.  
  6. // Parse your shortcode settings with it's defaults
  7. $atts = shortcode_atts( array(
  8. 'posts_per_page' => '-1',
  9. 'term' => ''
  10. ), $atts, 'mjelectric_post_grid' );
  11.  
  12. // Extract shortcode atributes
  13. extract( $atts );
  14.  
  15. // Define output var
  16. $output = '';
  17.  
  18. // Define query
  19. $query_args = array(
  20. 'post_type' => 'post', // Change this to the type of post you want to show
  21. 'posts_per_page' => $posts_per_page,
  22. );
  23.  
  24. // Query by term if defined
  25. if ( $term ) {
  26.  
  27. $query_args['tax_query'] = array(
  28. array(
  29. 'taxonomy' => 'category',
  30. 'field' => 'ID',
  31. 'terms' => $term,
  32. ),
  33. );
  34.  
  35. }
  36.  
  37. // Query posts
  38. $custom_query = new WP_Query( $query_args );
  39.  
  40. // Add content if we found posts via our query
  41. if ( $custom_query->have_posts() ) {
  42.  
  43. // Open div wrapper around loop
  44. $output .= '<div class="recent-post-wrapp">';
  45.  
  46. // Loop through posts
  47. while ( $custom_query->have_posts() ) {
  48.  
  49. // Sets up post data so you can use functions like get_the_title(), get_permalink(), etc
  50. $custom_query->the_post();
  51.  
  52. // This is the output for your entry so what you want to do for each post.
  53. $output .= '<div class="recent-post-grid-column">';
  54. $output .= '<div class="recent-post-grid-article-wrapp">';
  55.  
  56. $output .= '<div class="recent-post-grid-img">';
  57.  
  58. $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>';
  59.  
  60. $output .= '</div>';
  61.  
  62. $output .= '<div class="recent-post-grid-content">';
  63.  
  64. $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>';
  65.  
  66. $output .= '<div class="recent-post-content-title"><a href="' . get_the_permalink() . '"><h4>' . get_the_title() . '</h4></a></div>';
  67.  
  68. $output .= '<div class="recent-post-content-cat-meta">' . get_the_category_list( ', ', '', $custom_query->ID ) . '</div>';
  69.  
  70. $output .= '</div>';
  71.  
  72. $output .= '</div>';
  73.  
  74. $output .= '</div>';
  75.  
  76. }
  77.  
  78. // Close div wrapper around loop
  79. $output .= '</div>';
  80.  
  81. // Restore data
  82. wp_reset_postdata();
  83.  
  84. }
  85.  
  86. // Return your shortcode output
  87. return $output;
  88.  
  89. }
  90. add_shortcode( 'mjelectric_post_grid', 'mjelectric_post_grid_shortcode' );
  91.  
  92.  
  93. function my_post_time_ago_function() {
  94. return sprintf( esc_html__( '%s ', 'textdomain' ), human_time_diff(get_the_time ( 'U' ), current_time( 'timestamp' ) ) );
  95. }
  96.  
  97.  
  98. function gt_get_post_view() {
  99. $count = get_post_meta( get_the_ID(), 'post_views_count', true );
  100. return "$count views";
  101. }
  102. function gt_set_post_view() {
  103. $key = 'post_views_count';
  104. $post_id = get_the_ID();
  105. $count = (int) get_post_meta( $post_id, $key, true );
  106. $count++;
  107. update_post_meta( $post_id, $key, $count );
  108. }
  109. function gt_posts_column_views( $columns ) {
  110. $columns['post_views'] = 'Views';
  111. return $columns;
  112. }
  113. function gt_posts_custom_column_views( $column ) {
  114. if ( $column === 'post_views') {
  115. echo gt_get_post_view();
  116. }
  117. }
  118. add_filter( 'manage_posts_columns', 'gt_posts_column_views' );
  119. add_action( 'manage_posts_custom_column', 'gt_posts_custom_column_views' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement