投稿页面只输出指定的block内容

以仅输出table为例。
首先在function里添加如下函数。

function wpdocs_display_post_table_block() {
	global $post;
	$blocks = parse_blocks( $post->post_content );
	foreach ( $blocks as $block ) {
		if ( 'core/table' === $block['blockName'] ) {
			echo apply_filters( 'the_content', render_block( $block ) );
			break;
		}	
	}
}

在投稿页如下

if ( has_block( 'core/table' ) ) {
	wpdocs_display_post_table_block();
}

js正则表达式将字符串的数字自动添加标签

\d:表示一个十进制的数字[0-9]
\D:表示非数字
\w:表示一个字[0-9a-zA-Z]
\W:表示除[0-9a-zA-Z]之外的字符

$& 表示整个用于匹配的原字符串。

var $element = $(".plan-number-strong");
        $element.each(function() {
            var modifiedText = $(this).text().replace(/\d+/g, '<span>$&</span>');
            $(this).html(modifiedText);
        })

js阻止事件向上传递

JS事件模型为向上冒泡,如onclick事件在某一DOM元素被触发后,事件将跟随节点向上传播,直到有click事件绑定在某一父节点上,如果没有将直至文档的根。

function stopFunc(e) {
        e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
      }

随屏幕滚动 文字横向滚动

function leftScroll() {
        var $ttl = $('.scroll_left');
        $ttl.each(function () {
            var $this = $(this);
            var $scrolled = $(window).scrollTop();
            var $a = $(this).offset().top;
            $scrolled = $a - $scrolled;
            $this.css({
                "transform": "matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, "+($scrolled * 0.5) + ", 0, 0, 1)",
            })
        })
    }

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');
 }
}