我网站上的permalink经常坏掉。我发现它是由我创建的插件造成的。
这个插件基本上创建了一个自定义post类型,并在wp和表中使用自定义字段和存储。
我听说这件事发生是因为我把它们保存在wp-post的桌子上。那么我如何将它们保存到另一张桌子上呢?我该怎么从那张桌子上拿来。我可以通过传统的PHP和MySQL方法来实现它,但是我想利用WordPress函数。这能办到吗?
创建自定义post类型和分类法的代码
function dwwp_register_post_type(){
$singular = 'Speaker';
$plural = 'Speakers';
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'add_name' => 'Add New',
'add_new_item' => 'Add New '.$singular,
'edit' => 'Edit',
'edit_item' => 'Edit '.$singular,
'new_item' => 'New '.$singular,
'view' => 'View '.$singular,
'view_item' => 'View '.$singular,
'search_term' => 'Search '.$plural,
'parent' => 'Parent '.$singular,
'not_found' => 'No '.$plural.' Found',
'not_found_in_trash' => 'No '.$plural.' in Trash',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-businessman',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => false,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'rewrite' => array(
'slug' => 'speakers',
'with_front' => true,
'pages' => true,
'feeds' => true,
),
'supports' => array(
'title',
//'editor',
//'author',
//'custom-fields',
'thumbnail'
)
);
register_post_type('speakers', $args);
}
add_action('init', 'dwwp_register_post_type');
function dwwp_register_taxonomy(){
$plural = 'Years';
$singular = 'Year';
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search '.$plural,
'popular_items' => 'Popular '.$plural,
'all_items' => 'All '.$plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit '.$singular,
'update_item' => 'Update '.$singular,
'add_new_item' => 'Add New '.$singular,
'new_item_name' => 'New '.$singular.' Name',
'separate_items_with_commas' => 'Separate '.$plural.' with commas',
'add_or_remove_items' => 'Add or Remove '.$plural,
'choose_from_most_used' => 'Choose from the most used '.$plural,
'not_found' => 'No '.$plural.' found',
'menu_name' => $plural,
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array(
'slug' => 'year'
)
);
register_taxonomy('year', 'speakers', $args);
}
add_action('init', 'dwwp_register_taxonomy');现在,我为所有发言者创建了3个自定义字段:简短描述、完整描述和年份是自定义分类法。从输入字段存储在数据库中的代码
function dwwp_add_custom_metabox(){
add_meta_box('dwwp_meta', 'Speakers Description', 'dwwp_meta_callback', 'speakers', 'normal', 'core');
}
add_action('add_meta_boxes', 'dwwp_add_custom_metabox');
function dwwp_meta_callback( $post ){
wp_nonce_field(basename(__FILE__), 'dwwp_speakers_nonce');
$dwwp_stored_meta = get_post_meta($post->ID);
?>
Short Description
ID, 'short-description', true);
$editor = 'short-description';
$settings = array(
'textarea_rows' => 5,
);
wp_editor($content, $editor, $settings);
?>
Long Description
ID, 'long-description', true);
$editor = 'long-description';
$settings = array(
'textarea_rows' => 10,
);
wp_editor($content, $editor, $settings);
?>然后在网格中显示扬声器,并有一个弹出窗口在其他地方显示它们的详细信息。创建要在其他地方显示的短代码的代码。
array('2018', '2017', '2016')
), $atts
);
$return_args[] = '
Speakers
';
$args = array(
'post_type' => 'speakers',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'year',
'field' => 'name',
'terms' => $atts['years']
),
),
);
$speakers_list = new WP_Query($args);
if ($speakers_list->have_posts()):
while ($speakers_list->have_posts()): $speakers_list->the_post();
$return_args[] = '
'.get_the_title().'
';
endwhile;
endif;
$return_args[] = '';
$args = join($return_args);
return $args;
}
add_shortcode('speakers-grid-display', 'speakers_grid_display');
/*
=============================================================
Display speakers with pop ups
=============================================================
*/
function speakers_popup($atts, $content = null){
$atts = shortcode_atts(
array(
'years' => array('2018', '2017', '2016')
), $atts
);
$return_args[] = '
Speakers
';
$args = array(
'post_type' => 'speakers',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'year',
'field' => 'name',
'terms' => $atts['years']
),
),
);
$speakers_list = new WP_Query($args);
$i = 1;
if ($speakers_list->have_posts()):
while ($speakers_list->have_posts()): $speakers_list->the_post();
$return_args[] = '
'.get_the_title().'
';
$i++;
endwhile;
endif;
$return_args[] = '';
$speakers_list = new WP_Query($args);
$i = 1;
if ($speakers_list->have_posts()):
while ($speakers_list->have_posts()): $speakers_list->the_post();
$return_args[] = '
'.get_the_title().'
'.get_post_meta(get_the_ID(), "short-description", true).'
'.get_post_meta(get_the_ID(), "long-description", true).'
';
$i++;
endwhile;
endif;
$return_args = join($return_args);
return $return_args;
}
add_shortcode('speakers-popup', 'speakers_popup');然后我创建了另一个页面来重新排列发言者的名单
'speakers',
'orderby' => 'menu_order',
'order' => 'ASC',
'no_found_rows' => true,
'update_post_term_cache' => false,
'post_per_page' => 50
);
$speakers_list = new WP_Query($args);?>
Reorder Speakers
have_posts()): ?>
have_posts()): $speakers_list->the_post();
?>
No Speakers
(int)$item_id,
'menu_order' => $count
);
wp_update_post($post);
$count++;
}
wp_send_json_success('Post Saved.');
}
add_action('wp_ajax_save_post', 'dwwp_save_reorder');最后,在索引页中,代码是
wp_create_nonce('wp-speakers-order'),
'success' => __('Speakers sort order has been saved.'),
'failure' => __('There was an error saving the sort order, or you do not have priviledges')
));
}
}
add_action('admin_enqueue_scripts', 'dwwp_admin_enqueue_scripts');发布于 2018-04-22 02:57:10
您需要在插件激活时使用刷新重写规则,以便您的自定义post类型开始工作。
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'myplugin_flush_rewrites' );
function myplugin_flush_rewrites() {
// call your CPT registration function here
// (it should also be hooked into 'init')
dwwp_register_post_type();
flush_rewrite_rules();
}您可能已经注意到,如果其他一些代码刷新规则,比如访问Settings > Permalinks管理页面时,它可以随意工作而不必这样做。
https://wordpress.stackexchange.com/questions/301462
复制相似问题