2017-05-31 141 views
0

我想在靜態html頁面上的WordPress外加載帖子。到目前爲止,我有一個使用React的示例,http://v2.wp-api.org/https://github.com/mzabriskie/axios。這個使用react的工作示例目前正確地顯示了帖子,但它很脆弱,並且沒有倒退。這裏的Codepen示例,https://codepen.io/krogsgard/pen/NRBqPp/在html頁面上加載WordPress帖子

我使用此示例使用axios axios.get(this.props.source)來獲取我的供稿源。然後,我用的例子反應函數搶我的最新的三個職位,包括標題和圖像,並通過

render: function render() { 
    return React.createElement(
     "div", 
     { className: "post-wrapper" }, 
     this.state.posts.map(function (post) { 
      return React.createElement(
       "div", 
       { key: post.link, className: "post" }, 
       React.createElement(
        "h2", 
        { className: "post-title" }, 
        React.createElement("a", { 
         href: post.link, 
         dangerouslySetInnerHTML: { __html: post.title.rendered } 
        }) 
       ), 
       post.featured_media ? React.createElement(
        "a", 
        { href: post.link }, 
        React.createElement("img", { src: post._embedded['wp:featuredmedia'][0].source_url }) 
       ) : null 
      ); 
     }) 
    ); 
} 

我博客的源WP JSON是

React.render(React.createElement(App, { source: 
"myBlogURL.com/wp-json/wp/v2/posts/?_embed&per_page=3" }), 
document.querySelector("#blog-post")); 

哪個正確加載它們加載到一個靜態的HTML頁面我最近的3篇博客文章到<div id="blog-posts"> 我正在尋找一個香草js的方式來做到這一點與一些後備助手。如果我忘記將特色圖片添加到帖子中,帖子將無法加載失敗。任何想法或例子將非常感激!

回答

1

您正在努力爲此努力。 WordPress的CMS是專爲這樣的東西。您可以通過類別,標籤和其他taxonomies以RSS提要的形式顯示帖子。很容易

•如果您對代碼不太好,可以找到許多widgets,它們將負責大部分工作。

•如果你需要自己做,下面應該讓你在那裏與JSON/jQuery。

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js'></script> 

<script type="text/javascript"> 
var MYBLOG_LIMIT = 3; 
var MYWRAPPER_CLASS = 'homeblog'; 

var WP={open:function(b){var a={posts:function(){var d=MYBLOG_LIMIT;var e=0;var c={all:function(g){var f=b+"/api/get_recent_posts/";f+="?count="+d+"&page="+e+"&callback=?";jQuery.getJSON(f,function(l){var k=l.posts;for(var j=0;j<k.length;j++){var h=k[j];h.createComment=function(i,m){i.postId=h.id;a.comments().create(i,m)}}g(k)})},findBySlug:function(f,h){var g=b+"/api/get_post/";g+="?slug="+f+"&callback=?";jQuery.getJSON(g,function(i){h(i.post)})},limit:function(f){d=f;return c},page:function(f){e=f;return c}};return c},pages:function(){var c={findBySlug:function(d,f){var e=b+"/api/get_page/";e+="?slug="+d+"&callback=?";jQuery.getJSON(e,function(g){f(g.page)})}};return c},categories:function(){var c={all:function(e){var d=b+"/api/get_category_index/";d+="?callback=?";jQuery.getJSON(d,function(f){e(f.categories)})}};return c},tags:function(){var c={all:function(e){var d=b+"/api/get_tag_index/";d+="?callback=?";jQuery.getJSON(d,function(f){e(f.tags)})}};return c},comments:function(){var c={create:function(f,e){var d=b+"/api/submit_comment/";d+="?post_id="+f.postId+"&name="+f.name+"&email="+f.email+"&content="+f.content+"&callback=?";jQuery.getJSON(d,function(g){e(g)})}};return c}};return a}}; 

var blog = WP.open('https://www.fldtrace.com'); 
blog.posts().all(function(posts){ 
    for(var i = 0; i < posts.length; i++){ 
    jQuery('.'+MYWRAPPER_CLASS).append(function(){ 
     return (posts[i].thumbnail) ? '<a class="lastpost_title" href="'+posts[i].url+'"> 
<h4>'+posts[i].title+'</h4> 

</a><a href="'+posts[i].url+'"><img src="'+posts[i].thumbnail+'"/></a>' : '<a href="'+posts[i].url+'"> 
<h4>'+posts[i].title+'</h4> 

</a>'; 

    }); 
    } 
}); 
</script> 

<div class="homeblog"> 
</div> 

您需要配置的下一個選項

VAR MYBLOG_LIMIT = 1;將定義將顯示多少帖子。默認 是1. var MYWRAPPER_CLASS ='homeblog'; - 將顯示帖子的HTML 元素的類名稱。 var blog = WP.open('https://www.fldtrace.com/'); - 這個應該鏈接到你的博客 默認主域名(必填),會顯示帖子縮略圖 和標題都鏈接。剩下的就是CSS自定義,包括調整縮略圖大小的 。

在源文章閱讀更多here

相關問題