Wordpress发布短代码

时间:2016-11-30 17:05:01

标签: php wordpress post shortcode

我想在不使用插件的情况下在WordPress的其他帖子和页面中显示帖子。我搜索了这个网站(以及其他网站),但没有找到我想要的确切答案。

我想以这种方式在帖子和页面中显示短代码:

[insert_post id="99"] 其中99是帖子的ID(或页面,如果可能的话)。

我想提出以下内容: 标题, 内容(前30个字), 阅读更多按钮, 特色图片(拇指)。

任何提示都将受到赞赏。

1 个答案:

答案 0 :(得分:1)

您需要先创建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