Support » Developing with WordPress » WordPress API single post

  • Resolved Radu33

    (@radu33)


    Hello,

    I’m building a small wordpress sitee that should grab the posts from another website that we have. So I created a small plugin that is using the Wp API to grab the latest 5 posts using:
    $response = wp_remote_get( 'https://website.com/blog/wp-json/wp/v2/posts?per_page=5' );

    Now once I got the 5 posts and I show them on my page, how do I approach creating the single posts? The list of posts are on my new website /blog When I click the post link I want it to get me to /blog/post-name which is fairly easy to do, however, I have no idea how to display the content of the single post inside /blog/post-name as currently this post doesn’t exist in the DB I get a 404 error

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You ask about creating a single post, but I think you mean serving a single post that resides on the other site.

    There shouldn’t be relative links in WP, they’ll surely fail with certain requests. When you display a post from another site, its links should be absolute, so a link to the single post would normally take one back to the original site on which the post exists.

    If you want the remote posts to be served from your small site, you’ll need to alter the links for the posts to lead to something your site knows how to process. The current page already got the post from the initial API request. You could have the page’s scripts display the full single post in an element on the current page. This is often presented as a sidebar list of posts. The main page area might first display the first post’s content. When someone clicks on another sidebar link, script replaces the original main area content with that of the clicked post.

    If you’d rather mimic normal behavior where the single post is served on a separate page, you’d need a special page template which gets passed the post ID. The template code can then request the post on the other site through another API call.

    Radu33

    (@radu33)

    @bcworkz Thank you! yes, exactly I want to serve them from my website. Your idea sounds good, but I still want to have /blog/slug when accessing the single post. Is there a way I can altern single.php to make it work?

    Or shall I create a new template for that?

    Moderator bcworkz

    (@bcworkz)

    Don’t directly alter single.php, that would conflict with your blogging element. Add a new custom page template which contains the necessary PHP code. You can start with a copy of single.php in order to use your theme’s DOM structure, but alter where the post data comes from. Add a new WP page that will use this template.

    When you add custom templates, it’s best to use a child theme to contain your custom work.

    Radu33

    (@radu33)

    @bcworkz Thank you, I actually already created a custom template, which takes the query var from a function which adds a rewrite_rule add_rewrite_rule( 'blog/(.+?)/?$', 'index.php?post_slug=$matches[1]', 'top' );. And based on this query var I make a request to my bigger blog and display the content.

    I’m not sure how actually correct this is based on wordpress codex, but it works ๐Ÿ™‚

    Thank you for your suggestions ๐Ÿ™‚

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.