Guest User

Untitled

a guest
Aug 9th, 2013
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.12 KB | None | 0 0
  1. /* ================== В этом блоке все относится к статьям ===========*/
  2.  
  3. /* Регистрируем тип записей для статей */
  4.  
  5. add_action('init', 'my_custom_init2');
  6. function my_custom_init2() {  
  7.  
  8.   $labels = array(  
  9.     'name' => 'Статьи о снах',
  10.     'singular_name' => 'Статья',
  11.     'add_new' => 'Добавить новую',  
  12.     'add_new_item' => 'Добавить новую статью',  
  13.     'edit_item' => 'Редактировать статью',  
  14.     'new_item' => 'Новая статья',  
  15.     'view_item' => 'Посмотреть статью',  
  16.     'search_items' => 'Найти статью',  
  17.     'not_found' =>  'Статей не найдено',  
  18.     'not_found_in_trash' => 'В корзине статей не найдено',  
  19.     'parent_item_colon' => '',  
  20.     'menu_name' => 'Статьи'  
  21.   );  
  22.  
  23.   $args = array(  
  24.     'labels' => $labels,  
  25.     'public' => true,  
  26.     'publicly_queryable' => true,  
  27.     'show_ui' => true,  
  28.     'show_in_menu' => true,  
  29.     'query_var' => true,  
  30.     'rewrite' => false,
  31.     'capability_type' => 'post',  
  32.     'has_archive' => 'stati',  
  33.     'hierarchical' => false,  
  34.     'menu_position' => 5,  
  35.     'supports' => array('title','editor','author','custom-fields','comments','thumbnail')  
  36.   );  
  37.  
  38.   register_post_type('stati', $args);  
  39. }
  40.  
  41. /* Регистрируем таксономии для статей */
  42.  
  43. function create_my_taxonomies() {
  44.  
  45.     register_taxonomy('metki', 'stati', array(
  46.         'hierarchical' => false,
  47.         'label' => 'Метки',
  48.         'query_var' => true,
  49.         'rewrite' => false
  50.     ));
  51.  
  52.     register_taxonomy('rubriki', 'stati', array(
  53.         'hierarchical' => true,
  54.         'publicly_queryable' => true,  
  55.         'label' => 'Рубрики',
  56.         'rewrite' => false,
  57.         'query_var' => true
  58.     ));
  59.  
  60. }
  61.  
  62. add_action('init', 'create_my_taxonomies', 0);
  63.  
  64. /* переписываем URL статей, архива и связанных таксономий */
  65. global $wp_rewrite;
  66.  
  67. // структура статей 
  68.  
  69. $stati_structure = '/stati/%category%/%stati%/';
  70. $wp_rewrite->add_rewrite_tag("%stati%", '([^/]+)', "stati=");
  71. $wp_rewrite->add_permastruct('stati', $stati_structure, false);
  72.  
  73. // структура комментариев для статей 
  74. $stati_comment = '/stati/%category%/%stati%/';
  75. $wp_rewrite->add_rewrite_tag("%stati%", '([^/]+)', "stati=");
  76. $wp_rewrite->add_rewrite_tag('%cpage%', '1',"stati=$matches[1]&cpage=1");
  77. $wp_rewrite->add_permastruct('comment-page-1', $stati_comment, true);
  78.  
  79. // структура архива статей
  80. $stati_archive_structure = '%stati_cpt%/page/%paged%/';
  81. $wp_rewrite->add_rewrite_tag('%stati_cpt%', 'stati', '/sonnik/?post_type=stati');
  82. $wp_rewrite->add_rewrite_tag('%paged%', '([^/]+)', '/sonnik/?post_type=stati&paged=$matches[1]');
  83. $wp_rewrite->add_permastruct('stati_archive', $stati_archive_structure, false);
  84.  
  85. // структура рубрик для статей     
  86. $rubriki_structure = '/stati/rubriki/%rubriki%';
  87. $wp_rewrite->add_rewrite_tag("%rubriki%", '([^/]+)', "rubriki=");
  88. $wp_rewrite->add_permastruct('rubriki', $rubriki_structure, false);
  89.  
  90. // структура меток для статей   
  91. $metki_structure = '/stati/metki/%metki%';
  92. $wp_rewrite->add_rewrite_tag("%metki%", '([^/]+)', "metki=");
  93. $wp_rewrite->add_permastruct('metki', $metki_structure, false);
  94.  
  95.  
  96. add_filter('post_type_link', 'stati_permalink', 10, 3);
  97.  
  98. function stati_permalink($permalink, $post_id, $leavename) {
  99.     $post = get_post($post_id);
  100.     $rewritecode = array( '%category%');
  101.  
  102.     if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  103.         $unixtime = strtotime($post->post_date);
  104.      
  105.         $category = '';
  106.         if ( strpos($permalink, '%category%') !== false ) {
  107.             $cats = wp_get_object_terms($post->ID, 'rubriki');
  108.             if ( $cats ) {
  109.                 usort($cats, '_usort_terms_by_ID'); // order by ID
  110.                 $category = $cats[0]->slug;
  111.             }
  112.         }
  113.         $rewritereplace = array($category);
  114.         $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
  115.     }
  116.     return $permalink;
  117.    
  118. }
  119.  
  120. $wp_rewrite->flush_rules();
  121.  
  122. // Переписываем стандартный цикл Wordpresss при необходимых условиях
  123.  
  124. function hwl_home_pagesize( $query ) {
  125.  
  126.     if ( is_post_type_archive( 'stati' ) and !is_tax('rubriki') and !is_tax('metki') and !is_admin()) {
  127.         // Display 50 posts for a custom post type called 'movie'
  128.         $query->set( 'posts_per_page', 5 );
  129.         return;
  130.     }
  131.    
  132.      if ( is_tax('rubriki')) {
  133.         // Display 50 posts for a custom post type called 'movie'
  134.         $query->set( 'posts_per_page', -1 );
  135.         return;
  136.     }
  137.    
  138.     if ( is_tax('metki')) {
  139.         // Display 50 posts for a custom post type called 'movie'
  140.         $query->set( 'posts_per_page', 10 );
  141.         return;
  142.     }
  143. }
  144. add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );
  145.  
  146. add_action( 'load-post.php', 'thumb_meta' );
  147. add_action( 'load-post-new.php', 'thumb_meta' );
Advertisement
Add Comment
Please, Sign In to add comment