Структура url должна быть следующей:
/blog/
/blog/category/
/blog/category/article
При переходе на пост без category 404 страница
<?php
/**
Register post type success stories
*/
function register_blog_post_type() {
$labels = array(
'name' => __( 'Blog' ),
'singular_name' => __( 'Blog' ),
'add_new' => _x('Add New', 'Blog item'),
'add_new_item' => __('Add New Blog item'),
'edit_item' => __('Edit Blog Item'),
'new_item' => __('New Blog Item'),
'view_item' => __('View Blog Item'),
'search_items' => __('Search Blog'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
"show_in_rest" => false,
'query_var' => true,
'rewrite' => array('slug' => "blog/%blog-category%","with_front" => true),
"hierarchical" => false,
'capability_type' => 'post',
'menu_position' => 6,
'has_archive' => 'blog',
'supports' => array( 'title', 'editor', 'thumbnail','excerpt','author','comments'),
);
register_post_type( 'blog', $args);
}
add_action( 'init', 'register_blog_post_type' );
/**
Register taxonomy success stories
*/
function blog_taxonomy() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
"hierarchical" => true,
'rewrite' => array('slug'=>'blog'),
);
register_taxonomy('blog-category', 'blog', $args);
}
add_action( 'init', 'blog_taxonomy');
/**
Metablock activation function, success stories
*/
function blog_extra_fields() {
add_meta_box( 'extra_fields', 'Preview fields', 'blog_extra_fields_box_func', 'blog', 'normal', 'high' );
}
add_action('add_meta_boxes', 'blog_extra_fields', 1);
/**
Admin box, success stories
*/
function blog_extra_fields_box_func( $post ){
?>
<p>
<?php
$categories = get_the_terms($post->ID,'blog-category');
if($categories == false) {
?>
<p>Пост не имеет категорий</p>
<?php }else{?>
<p>Пост главный в категории:
<?php $mark_v = get_post_meta($post->ID, 'main_category_id', 1);?>
<?php foreach ($categories as $blog_category){?>
<label><input type="radio" name="extra[main_category_id]" value="<?php echo $blog_category->term_id;?>" <?php checked( $mark_v, $blog_category->term_id); ?> /><?php echo $blog_category->name;?></label>
<?php }?>
</p>
<?php }?>
<p>
<input type="hidden" name="extra[main-blog]" value="">
<label><input type="checkbox" name="extra[main-blog]" value="1" <?php checked( get_post_meta($post->ID, 'main-blog', 1), 1 )?> /> Blog main</label>
<!-- <input type="hidden" name="extra[main-category]" value="">-->
<!-- <label><input type="checkbox" name="extra[main-category]" value="1" --><?php //checked( get_post_meta($post->ID, 'main-category', 1), 1 )?><!-- /> Main category</label>-->
</p>
<input type="hidden" name="extra_fields_nonce" value="<?php echo wp_create_nonce(__FILE__); ?>" />
<?php
}
function blog_extra_fields_update( $post_id ){
if (
empty( $_POST['extra'] )
|| ! wp_verify_nonce( $_POST['extra_fields_nonce'], __FILE__ )
|| wp_is_post_autosave( $post_id )
|| wp_is_post_revision( $post_id )
)
return false;
$_POST['extra'] = array_map( 'sanitize_text_field', $_POST['extra'] );
foreach( $_POST['extra'] as $key => $value ){
if( empty($value) ){
delete_post_meta( $post_id, $key );
continue;
}
update_post_meta( $post_id, $key, $value );
}
return $post_id;
}
add_action( 'save_post', 'blog_extra_fields_update', 0 );
function custom_author_archive( &$query ) {
if ($query->is_author)
$query->set( 'post_type', 'blog' );
}
add_action( 'pre_get_posts', 'custom_author_archive' );
function reading_time() {
global $post;
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
$readingtime = ceil($word_count / 200);
return $readingtime;
}
function enqueue_comment_reply() {
if( is_singular() )
wp_enqueue_script('comment-reply');
}
add_action( 'wp_enqueue_scripts', 'enqueue_comment_reply' );
?>
<?php
function mytheme_comment($comment, $args, $depth) {
static $counter = 1;
$GLOBALS['comment'] = $comment;
if($counter < 5){
?>
<div class="mention">
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="mention__box">
<span class="mention__name"><?php echo $comment->comment_author;?></span>
<span class="mention__date"><?php echo date( 'F j,Y', strtotime($comment->comment_date )); ?></span>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em><php _e('Your comment is awaiting moderation.') ?></em>
<br />
<?php endif; ?>
<div class="mention__text">
<?php comment_text() ?>
</div>
<div class="reply">
<?php comment_reply_link(array_merge
( $args, array('depth' => $depth,'reply_text' => 'Answer', 'max_depth' => $args['max_depth']))) ?>
</div>
</div>
</div>
<?php } else {?>
<div class="mention__hide">
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="mention">
<div class="mention__box">
<span class="mention__name"><?php echo $comment->comment_author;?></span>
<span class="mention__date"><?php echo date( 'F j, Y', strtotime($comment->comment_date )); ?></span>
</div>
<div class="mention__text">
<?php comment_text() ?>
</div>
<div class="reply">
<?php comment_reply_link(array_merge
( $args, array('depth' => $depth,'reply_text' => 'Answer', 'max_depth' => $args['max_depth']))) ?>
</div>
</div>
</div>
</div>
<?php }?>
<?php
$counter++;
}
add_filter('post_type_link', 'cj_update_permalink_structure', 10, 2);
function cj_update_permalink_structure( $post_link, $post )
{
if ( false !== strpos( $post_link, '%blog-category%' ) ) {
$taxonomy_terms = get_the_terms( $post->ID, 'blog-category' );
foreach ( (array) $taxonomy_terms as $term ) {
if ( ! $term->parent ) {
$post_link = str_replace( '%blog-category%', $term->slug, $post_link );
}
}
}
return $post_link;
}