Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Рандомные записи
  4. Plugin URL: none
  5. Description: Случайные записи вызываются шорткодом вида [GetCommentsPosts count="4"] где count количество записей, при равном 0 выведет все
  6. Author: Владимир Марченко
  7. */
  8.  
  9. function GetCommentsPosts($count=0)
  10. {
  11. global $wpdb;
  12.  
  13. extract( shortcode_atts( array( 'count' => 0 ), $count ) );
  14.  
  15. if ( $count == 0){
  16. $args = array(
  17. 'numberposts' => 0,
  18. 'category' => 0,
  19. 'orderby' => 'rand',
  20. 'order' => 'DESC',
  21. 'include' => array(),
  22. 'exclude' => array(),
  23. 'meta_key' => '',
  24. 'meta_value' =>'',
  25. 'post_type' => 'functions',
  26. 'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
  27. );
  28. } else {$args = array(
  29. 'numberposts' => 5,
  30. 'category' => 0,
  31. 'orderby' => 'rand',
  32. 'order' => 'DESC',
  33. 'include' => array(),
  34. 'exclude' => array(),
  35. 'meta_key' => '',
  36. 'meta_value' =>'',
  37. 'post_type' => 'functions',
  38. 'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
  39. );}
  40.  
  41.  
  42. // параметры по умолчанию
  43.  
  44.  
  45. $posts = get_posts( $args );
  46. $lr = true;
  47. foreach($posts as $post){ setup_postdata($post);
  48. // print_r($post);
  49.  
  50. $thumbnail = get_the_post_thumbnail($post->ID, array(432,432));
  51. if ($lr) {
  52. $echo_comments .='<div class="comment_block">'.$thumbnail.' <div class="comment_title">'.$post->post_title.'<div class="comment_descr">'.$post->post_content.'</div></div></div>';
  53. $lr = false;
  54. } else {
  55. $echo_comments .='<div class="comment_block"> <div class="comment_title">'.$post->post_title.'<div class="comment_descr">'.$post->post_content.'</div></div>'.$thumbnail.'</div>';
  56. $lr = true;
  57. }
  58.  
  59. }
  60. echo $echo_comments;
  61. wp_reset_postdata(); // сброс
  62.  
  63.  
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71. add_action( 'init', 'comments_index' );
  72.  
  73. function comments_index()
  74. {
  75. $labels = array(
  76. 'name' => 'Список коментариев',
  77. 'singular_name' => 'Комментарии',
  78. 'add_new' => 'Добавить комент',
  79. 'add_new_item' => 'Добавить новый комент',
  80. 'edit_item' => 'Редактировать комент',
  81. 'new_item' => 'Новый комент',
  82. 'all_items' => 'Все коментарии',
  83. 'view_item' => 'Просмотр комнтариев на сайте',
  84. 'search_items' => 'Искать комент',
  85. 'not_found' => 'Коментариев не найдено.',
  86. 'not_found_in_trash' => 'В корзине нет коментариев.',
  87. 'menu_name' => 'Комментарии'
  88. );
  89.  
  90. $args = array(
  91. 'labels' => $labels,
  92. 'public' => true,
  93. 'show_ui' => true,
  94. 'has_archive' => true,
  95. 'menu_icon' => get_stylesheet_directory_uri() .'/function_icon.png',
  96. 'menu_position' => 20,
  97. 'supports' => array( 'title', 'editor', 'comments', 'author', 'thumbnail')
  98. );
  99.  
  100. register_post_type('functions', $args);
  101. }
  102.  
  103. add_shortcode('GetCommentsPosts', 'GetCommentsPosts');
  104.  
  105. //add_shortcode('тег шорткода', 'название функции');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement