wp分类链接去掉category的方法

在function.php中加入如下代码,不适用有子分类的。

function remove_category($string, $type)
{if ($type != 'single' && $type == 'category' && (strpos($string, 'category') !== false)) {$url_without_category = str_replace("/category/", "/", $string);return trailingslashit($url_without_category);}
    return $string;}add_filter('user_trailingslashit', 'remove_category', 100, 2);

wordpress随机获取文章内的gallery和video

假设文章ID=5

随机输出相册内20张图片:

<?php 
        $post=5;
        $ids = [];
        $array_key_names=[];
        if (has_block('gallery', $post)) {
            $post_blocks = parse_blocks(get_the_content($post));
            $ids = $post_blocks[0]['attrs']['ids'];
        } 
        else {
            $gallery = get_post_gallery( $post->ID, false );
            $ids = explode ( ",", $gallery['ids'] );
        }
        shuffle($ids);
        $array_key_names = array_rand($ids,20);
        foreach($array_key_names as $array_key_name){
// echo ...;
}?>

随机输出1个视频

<?php
$post = 5;
$ids = [];
$videos = [];
$array_key_name = [];
if (has_block('video', $post)) {
    $post_blocks = parse_blocks(get_the_content($post));
    foreach ($post_blocks as $post_block) {
        array_push($videos, $post_block['attrs']['id']);
    }
}
$ids = array_filter($videos);
shuffle($ids);
if (!empty($ids)) {
    $array_key_name = array_rand($ids, 1);
    $video_id = $ids[$array_key_name];
?>
      <video preload="auto" loop="true" muted="true" playsinline="true">
<source src="<?php echo wp_get_attachment_url($video_id); ?>" type="video/mp4">
      </video>
<?php } ?>

wp自动获取年份

//Auto Copyright function auto_copyright($year = ‘auto’) { if (intval($year) == ‘auto’) { $year = date(‘Y’); } if (intval($year) == date(‘Y’)) { echo intval($year); } if (intval($year) < date('Y')) { echo intval($year) . ' - ' . date('Y'); } if (intval($year) > date(‘Y’)) { echo date(‘Y’); } }

非管理员账户,只能看见自己发布的投稿或者媒体文件

都要添加到”pre_get_posts”这个钩子

仅见投稿:

function posts_for_current_author($query) {
   global $pagenow;

   if( 'edit.php' != $pagenow || !$query->is_admin )
       return $query;

   if( !current_user_can( 'edit_others_posts' ) ) {
       global $user_ID;
       $query->set('author', $user_ID );
   }
   return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

仅见媒体文件:

add_action('pre_get_posts','users_own_attachments');
function users_own_attachments( $wp_query_obj ) {
    $user = wp_get_current_user();
    if ( !current_user_can('administrator') ) {
        global $current_user, $pagenow;
    $is_attachment_request = ($wp_query_obj->get('post_type')=='attachment');
    if( !$is_attachment_request )
        return;
    if( !is_a( $current_user, 'WP_User') )
        return;
    if( !in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' ) ) )
        return;
    if( !current_user_can('delete_pages') )
        $wp_query_obj->set('author', $current_user->ID );
    return;
    }
}

仅见媒体文件和投稿:

add_action('pre_get_posts', 'query_set_only_author' ); 
function query_set_only_author( $wp_query ) {
 global $current_user;
 $current_user = wp_get_current_user();
 if( is_admin() && !current_user_can('edit_others_posts') ) {
    $wp_query->set( 'author', $current_user->ID );
    add_filter('views_edit-post', 'fix_post_counts');
    add_filter('views_upload', 'fix_media_counts');
 }
}

更改wp后台默认投稿名称

function revcon_change_post_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'News';
    $submenu['edit.php'][5][0] = 'News';
    $submenu['edit.php'][10][0] = 'Add News';
    $submenu['edit.php'][16][0] = 'News Tags';
}
function revcon_change_post_object() {
    global $wp_post_types;
    $labels = &$wp_post_types['post']->labels;
    $labels->name = 'News';
    $labels->singular_name = 'News';
    $labels->add_new = 'Add News';
    $labels->add_new_item = 'Add News';
    $labels->edit_item = 'Edit News';
    $labels->new_item = 'News';
    $labels->view_item = 'View News';
    $labels->search_items = 'Search News';
    $labels->not_found = 'No News found';
    $labels->not_found_in_trash = 'No News found in Trash';
    $labels->all_items = 'All News';
    $labels->menu_name = 'News';
    $labels->name_admin_bar = 'News';
}
 
add_action( 'admin_menu', 'revcon_change_post_label' );
add_action( 'init', 'revcon_change_post_object' );

wp将自定义分类checkBox更改为radio

wp_terms_checklist() ;

function term_radio_checklist( $args ) {
    if ( ! empty( $args['taxonomy'] ) && $args['taxonomy'] === 'other_tag' /* <== Change to your required taxonomy */ ) {
        if ( empty( $args['walker'] ) || is_a( $args['walker'], 'Walker' ) ) { // Don't override 3rd party walkers.
            if ( ! class_exists( 'Radio_Checklist' ) ) {
                class Radio_Checklist extends Walker_Category_Checklist {
                    public function walk( $elements, $max_depth, ...$args ) {
                        $output = parent::walk( $elements, $max_depth, ...$args );
                        $output = str_replace(
                            array( 'type="checkbox"', "type='checkbox'" ),
                            array( 'type="radio"', "type='radio'" ),
                            $output
                        );

                        return $output;
                    }
                }
            }

            $args['walker'] = new Radio_Checklist;
        }
    }
    return $args;
}
add_filter( 'wp_terms_checklist_args', 'term_radio_checklist' );

规定时间内发布的文章显示new标签

<?php
$pub_date = strtotime(get_the_date("Y-m-d H:i:s")) + 30*24*60*60;
$cur_date = strtotime(date("Y-m-d H:i:s")); ?>
<span class="time <?php if($pub_date >= $cur_date ){ echo 'shownew';}?>"><?php the_time('Y.m.d'); ?></span>

Mange users columns

Add columns:

function new_modify_user_table( $column ) {
    $column['phone'] = 'Phone';
    $column['xyz'] = 'XYZ';
    return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table' );

function new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'phone' :
            return get_the_author_meta( 'phone', $user_id );
        case 'xyz' :
            return '';
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );

Remove columns:

$c = array( ‘cb’ => ‘<input type=”checkbox” />’, ‘username’ => __( ‘Username’ ), ‘name’ => __( ‘Name’ ), ’email’ => __( ‘E-mail’ ), ‘role’ => __( ‘Role’ ), ‘posts’ => __( ‘Posts’ ) );

If for example you need to remove E-mail column you should unset a column with ’email’ ID. Let’s go to the final step:

add_filter('manage_users_columns','remove_users_columns');
function remove_users_columns($column_headers) {
    if (current_user_can('moderator')) {
      unset($column_headers['email']);
    }
 
    return $column_headers;
}

We use manage_users_columns filter to achieve this purpose. This code removes ‘E-mail’ folder for users with ‘moderator’ role. Replace it with your own one.
In order to remove other column replace ’email’ column ID to that column ID: role, posts, etc.

Other variant for the list of roles:

add_filter('manage_users_columns','remove_users_columns');
function remove_users_columns($column_headers) {
    $roles = array('moderator', 'users-list-viewer');
    foreach($roles as $role) {
        if (current_user_can($role)) {
            unset($column_headers['email']);
            break;
        }
    }
 
    return $column_headers;
}

Another variant for the list of users ID:

add_filter('manage_users_columns','remove_users_columns');
function remove_users_columns($column_headers) {
    global $current_user;
 
    $users = array(27, 70530, 70531, 70532);
 
    if (in_array($current_user->ID, $users)) {
        unset($column_headers['email']);
    }    
 
    return $column_headers;
}