仿 WordPress 的 Post Embed 文章卡片内链功能

WordPress  3年前 (2020-04-17)     1,321    本文共计2880个字,预计阅读时长10分钟。  

本文转自常阳时光,不再赘述原文的文字部分,文后会附上链接,顺便羡慕一下原作者的才华

1、下面的代码加入到主题functions.php中

// WordPress 文章内链短代码 - https://fatesinger.com/76585
function xx_insert_posts( $atts, $content = null ){
    extract( shortcode_atts( array('ids' => ''), $atts ) );
    global $post;
    $content = '';
    $postids =  explode(',', $ids);
    $inset_posts = get_posts(array('post__in'=>$postids));
    foreach ($inset_posts as $key => $post) {
        setup_postdata( $post );
        $content .=  '
	        <div class="wp-embed-post">
	        	<p class="wp-embed-post-heading"><a target="_blank" class="wp-embed-post-title" href="' . get_permalink() . '">'. get_the_title() . '</a></p>
	        	<div class="wp-embed-post-excerpt">'.wp_trim_words( get_the_content(), 100, '...' ).'</div>
	        	<div class="wp-embed-post-footer">
	        		<div class="wp-embed-post-site-title">
	        			<a href="'.get_author_posts_url( get_the_author_meta( 'ID' ) ) .'" title="查看作者 '.get_the_author().' 发布的所有文章" rel="author">'
	        				.get_avatar( get_the_author_meta('email'), '16' ).'<span>'.get_the_author().'</span>
	        			</a>
	        		</div>
	        		<div class="wp-embed-post-meta">
	        			<div class="wp-embed-post-reads">阅读 
							'.the_views(false, '', '', false).'
						</div>
						<div class="wp-embed-post-comments">
							<a target="_blank" class="wp-embed-post-title" href="' . get_permalink() . '#comments"> 评论 ' . get_comments_number(). '</a>
						</div>
					</div>
				</div>
			</div>';
    }
    wp_reset_postdata();
    return $content;
}
add_shortcode('xx_insert_post', 'xx_insert_posts');

2、CSS 参考

/** WordPress 文章内链短代码 New Embed**/
.wp-embed-post {
    background: #fff;
    border: 1px solid #e5e5e5;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
    color: #82878c;
    font-size: 14px;
    overflow: auto;
    padding: 16px;
    margin-bottom: 16px;
}
.wp-embed-post a {
    color: #666;
    text-decoration: none;
}

.wp-embed-post-featured-image {
    margin-bottom: 20px;
}
.wp-embed-post-featured-image img {
    border: medium none;
    height: auto;
    width: 100%;
}
.wp-embed-post-featured-image.square {
    float: left;
    margin-right: 20px;
    max-width: 160px;
}
.wp-embed-post p {
    margin: 0;
}
p.wp-embed-post-heading {
    font-size: 20px;
    margin: 0 0 4px!important;
}
.wp-embed-post-heading a {
    color: #32373c;
}
.wp-embed-post .wp-embed-post-more {
    color: #b4b9be;
}
.wp-embed-post-footer {
    display: table;
    margin-top: 16px;
    width: 100%;
}
.wp-embed-post-site-title .avatar {
    border: 0 none;
    height: 25px;
    left: 0;
    position: absolute;
    -ms-transform: translateY(-50%);
	-webkit-transform: translateY(-50%);
	-moz-transform: translateY(-50%);
	-o-transform: translateY(-50%);
    transform: translateY(-50%);
    width: 25px;
}
.wp-embed-post-site-title a {
    display: inline-block;
    padding-left: 32px;
    position: relative;
}
.wp-embed-post-meta, .wp-embed-post-site-title {
    display: table-cell;
}
.wp-embed-post-meta {
    text-align: right;
    vertical-align: middle;
    white-space: nowrap;
}
.wp-embed-post-comments, .wp-embed-post-reads {
	color: #666;
    display: inline;
}
.wp-embed-post-comments a, .wp-embed-post-share-tab-button {
    display: inline-block;
}
.wp-embed-post-comments + .wp-embed-post-share {
    margin-left: 10px;
}
/** WordPress 文章内链短代码 New Embed - END **/

3、使用方法

直接在文章中(html 代码模式下)使用短代码 [xx_insert_post ids=111,222] 即可。

如果不是在文章内容中,而是在其他地方想调用,则可使用 do_shortcode('[xx_insert_post ids=111,222]') 来调用。

4、原文网址(常阳时光):https://cyhour.com/435/

[Lonesome_insert_post ids=197]



微信扫描二维码阅读



本文由:龙哥 发表于 3年前 (2020-04-17),共2880字
版权声明:①欢迎转载但请注明出处。②如涉及版权联系(izhailong#qq.com)删除!
转载请注明:仿 WordPress 的 Post Embed 文章卡片内链功能   ||   https://www.izhailong.com/183.html

暂无评论

暂无评论...