把下面的代码复制到 模板风格的 functions.php 文件中即可生效。兼容到最新的PHP 7.4 以及wordpress5.4版本。

add_action( 'wp_head', 'starfire', 0 );
if ( ! function_exists( 'starfire' ) ) {
	function starfire() {
		global $wpdb;
		$post_id = isset( $post_id ) ? $post_id : get_the_ID();
		$copy    = get_post_meta( $post_id, 'author', true );
		if ( is_singular() && empty( $copy ) ) {
			date_default_timezone_set( 'PRC' );
			echo '<meta property="og:type" content="article"/>
		<meta property="article:published_time" content="' . get_the_date( 'c' ) . '"/>
		<meta property="og:release_date" content="' . get_the_date( 'c' ) . '"/>
		<meta property="article:author" content="';
			bloginfo( 'name' );
			echo '" />';
			echo '<meta property="og:author" content="';
			bloginfo( 'name' );
			echo '" />';
			echo '<meta property="og:url" content="';
			the_permalink();
			echo '"/>';
			//输出文章标题+分隔符+网站名称,不喜欢这种形式的请自行改造(如果不需要这个标签,请删除以下三行)。
			echo '<meta property="og:title" content="' . trim( wp_title( '', 0 ) ) . ' | ';
			bloginfo( 'name' );
			echo '" />';
			//输出博客名称,如果想改成其他内容,比如作者请自行修改 bloginfo('name')
			echo '<meta property="article:published_first" content="';
			bloginfo( 'name' );
			echo ',';
			the_permalink();
			//默认截取文章220个字作为摘要,可以自行修改下行220为其他整数
			echo '" /><meta property="og:description" content="' . get_mypost_excerpt( $post_id, 220 ) . '……" />
        <meta property="og:image" content="' . get_mypost_thumbnail( $post_id ) . '" />
        <meta itemprop="image" content="' . get_mypost_thumbnail( $post_id ) . '" />';
		}
	}
}

function get_mypost_excerpt( $post_ID, $len ) {
	if ( ! function_exists( 'utf8Substr' ) ) {
		function utf8Substr( $str, $from, $len ) {
			return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' .
			                     '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s',
				'$1', $str );
		}
	}
	$post = get_post( $post_ID );
	if ( $post->post_excerpt ) {
		return $post->post_excerpt;
	} else {
		if ( preg_match( '/<p>(.*)<\/p>/iU', trim( strip_tags( $post->post_content, "<p>" ) ), $result ) ) {
			$post_content = $result['1'];
		} else {
			$post_content_r = explode( "\n", trim( strip_tags( $post->post_content ) ) );
			$post_content   = $post_content_r['0'];
		}

		return utf8Substr( $post_content, 0, $len );
	}
}

function get_mypost_thumbnail( $post_ID ) {
	if ( has_post_thumbnail() ) {
		$timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post_ID ), 'full' );
		$url          = $timthumb_src[0];
	} else {
		$post = get_post( $post_ID );
		preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode( $post->post_content ), $matches );
		if ( $matches && isset( $matches[1] ) && isset( $matches[1][0] ) ) {
			$url = $matches[1][0];
		} else {
			$url = '';
		}
	}

	return $url;
}
点赞 ({{click_count}}) 收藏 (0)

WordPress主题-大前端DUX 6.4

路由器怎么设置