WordPress的帖子短代码
问题描述:
我想在WordPress中显示其他帖子和页面内的帖子,而不使用插件。我搜索了这个网站(和其他人),但没有找到我正在寻找的明确答案。WordPress的帖子短代码
我想以这种方式来显示的文章和网页内的短代码:
[insert_post id="99"]
其中99是帖子的ID(或页,如果可能的话)。
我想在下面拉: 标题, 内容(第30个话), 更多按钮, 特色图像(拇指)。
任何提示将不胜感激。
答
您需要先创建shortcode
。此代码在您的functions.php
。这是一个基本的例子来达到你想达到什么目的:
function insert_post_shortcode_function($atts) {
$attributes = shortcode_atts(array(
'id' => '' // attribute_name => default_value
), $atts);
$return = '';
$post = get_post($attributes['id']);
$return .= get_the_post_thumbnail($post->ID);
$return .= $post->post_title;
$return .= $post->post_content;
$return .= '<a href="'.get_the_permalink($post->ID).'">Read more</a>';
return $return;
}
add_shortcode('insert_post', 'insert_post_shortcode_function');
现在,您可以在您的文章的内容使用[insert_post id="x"]
。
有关创建简码的更多信息 - https://codex.wordpress.org/Shortcode_API
+0
很好的答案,而不是直接访问post_content我想我会应用内容过滤器,看看[the_content](https://developer.wordpress.org/reference/functions/the_content/) – andrew
您能否提供努力获得结果? – inaliahgle