var getUrlParameter = function getURLParameter(name, url) {
// Create a URL object from the url string
var urlObj = new URL(url);
// Get the search parameters object
var params = urlObj.searchParams;
// Get the value of the name parameter
return params.get(name);
}
$link = $this.attr("href");
$paged = getUrlParameter("teams_paged", $link);
$_SERVER[‘HTTP_HOST’]获取当前域名,parse_url()解析地址。
<?php if ($_SERVER['HTTP_HOST'] != parse_url($meta['pro_link'])["host"]) {echo 'target="_blank" rel="noopener noreferrer"';}?>
以仅输出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();
}
\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);
})
h1 {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
clip-path: inset(0px 0px 99.9% 99.9%);
overflow: hidden;
height: 1px;
width: 1px;
padding: 0;
border: 0;
}
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)",
})
})
}
在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);
假设文章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 } ?>
//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’);
}
}