0

I am trying to load posts outside WordPress on a static html page. So far I have a working example using React, http://v2.wp-api.org/ and https://github.com/mzabriskie/axios. This working example using react currently displays the posts properly but it is fragile and there is no fall back. Codepen example here, https://codepen.io/krogsgard/pen/NRBqPp/

I use this example to get my feed source using axios axios.get(this.props.source). Then I use the examples react function to grab my latest three posts, including titles and images and load them into a static html page via

 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
            );
        })
    );
}

My blog's source wp json is

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

Which correctly loads my latest 3 blog posts into the <div id="blog-posts"> I'm looking for a vanilla js way to do this with some fallback helpers. In case I forget to include the featured image to a post, the posts will not fail to load. Any ideas or examples would greatly be appreciated!

1 Answer 1

1

You are working way to hard for this. Wordpress CMS is designed for stuff like this. You are able to display posts in the form of a RSS feed by categories, tags and other taxonomies. pretty easily

• If you are not too good with code, you can find many widgets that'll take care of most of the work.

• If you need to do it yourself, below should get you there with 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>

You need to configure the next options

var MYBLOG_LIMIT = 1; will define how many posts will show. By default is 1. var MYWRAPPER_CLASS = ‘homeblog’; – the class name of HTML element where the post(s) will be shown. var blog = WP.open(‘https://www.fldtrace.com/’); – this should link to your blog main domain (mandatory) by default, will be shown the post thumbnail and title both linked. The rest is CSS customization, including adjusting the thumbnail size.

Read more here at source article.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.