2016-11-30 101 views
-1

我想在WordPress中顯示其他帖子和頁面內的帖子,而不使用插件。我搜索了這個網站(和其他人),但沒有找到我正在尋找的明確答案。WordPress的帖子短代碼

我想以這種方式來顯示的文章和網頁內的短代碼:

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

我想在下面拉: 標題, 內容(第30個話), 更多按鈕, 特色圖像(拇指)。

任何提示將不勝感激。

+0

您能否提供努力獲得結果? – inaliahgle

回答

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

+0

很好的答案,而不是直接訪問post_content我想我會應用內容過濾器,看看[the_content](https://developer.wordpress.org/reference/functions/the_content/) – andrew