要调用的是站内文章,如果使用get_posts的话可以很好的调用文章的元信息,包括浏览量,缩略图之类的,甚至文章摘要(如果你有的话)。再加上可以自定义样式,这个内链看上去可要比普通的A标签高大上多了。比如下面的:
我们可以用短 代码的方式添加文章ID来直接调用文章,以下是实现方法,相比其他站分享的代码,已经整合缩略图了。
下面的代码加入到functions.php中:
/** * WordPress 添加短代码在文章内调用站内指定文章内容! */ function dsqnw_fa_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="post-arts"> <div class="post-arts-thumbnail"> <a href="' . get_permalink() . '" title="' . get_the_title() . '" isconvert="1"> <img src="' .get_mypost_thumbnail($post_id). '" alt="' . get_the_title() . '" width="160" height="160"> </a> </div> <div class="post-arts-info"> <h2 class="post-arts-title"> <a href="' . get_permalink() . '" rel="bookmark" title="' . get_the_title() . '" isconvert="1">' . get_the_title() . '</a> </h2> <div class="post-arts-excerpt"> ' . mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 120,"...") . '</div> <div class="post-arts-comments">' .get_comments_number(). '</div> </div> </div> </div> '; } wp_reset_postdata(); return $content; } add_shortcode('dsqnw_fa_insert_post', 'dsqnw_fa_insert_posts');
你可以根据你自己的需要来调整代码,也可以自己自定义CSS样式,这里就不给出CSS代码了。喜欢我这的CSS那就直接拔!
调用方式
/* 文章内调用 /* -------------------------------- */ [dsqnw_fa_insert_post ids=123,245] /* 其他页面调用 /* -------------------------------- */ do_shortcode('[dsqnw_fa_insert_post ids=123,245]')