Get post from a single category with have_posts () WordPress

0

I'm showing the last post of a blog on an external website that I'm developing out of wordpress.

<?php
    //Include WordPress
    define('WP_USE_THEMES', false);
    require('./blog/wp-load.php');
    //Define quantos posts serão exibidos
    query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<li>
    <h4><?php the_title(); ?></h4>                      
    <span><?php the_time("d/m/Y"); ?></span>
    <?php the_category_ID(); ?>
    <?php the_content(); ?>
    <div>
    <a href="<?php the_permalink(); ?>">&laquo; Leia Mais...</a>
    </div>
</li>
<?php endwhile;?>

The point is that the blog is separated into two categories and I would like to display only one of them.

link where I got these functions.

link

    
asked by anonymous 10.08.2017 / 20:31

1 answer

1

Since WordPress version 4.7 no longer needs to load WordPress in this way to fetch content, just use the native REST API:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://example.com/wp-json/wp/v2/posts?per_page=3&categories={ID_DA_CATEGORIA}',
));

$resp = curl_exec($curl);
$posts = json_decode( $resp, true );
curl_close($curl);

foreach( $posts as $post ) : ?>
    <li>
        <h4><?php echo $post['title']['rendered']; ?></h4>
        <span><?php echo date("d/m/Y", strtotime( $post['date'] ) ); ?></span>
        <?php // {ID_DA_CATEGORIA} ?>
        <?php echo $post['content']['rendered']; ?>
        <div>
            <a href="<?php echo $post['link']; ?>">&laquo; Leia Mais...</a>
        </div>
    </li>
<?php endforeach;?>

Now $posts is an associative array with information from the last 3 posts in the category you requested. In this array are all the information that is being requested there in the markup as title, link, date, etc.

Example returned object:

{
    "id": 173,
    "date": "2017-07-25T01:35:08",
    "date_gmt": "2017-07-25T01:35:08",
    "guid": {
        "rendered": "http://example.com//?p=173"
    },
    "modified": "2017-08-04T13:15:31",
    "modified_gmt": "2017-08-04T13:15:31",
    "slug": "resource-31",
    "status": "publish",
    "type": "post",
    "link": "http://example.com/resource-31/",
    "title": {
        "rendered": "Resource 31"
    },
    "content": {
        "rendered": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris tellus lorem, aliquet ac est ac, convallis luctus nunc. Aliquam vitae mi ullamcorper elit vulputate gravida a nec turpis. Cras turpis ipsum, pretium in molestie in, facilisis eget elit. Phasellus semper dolor eu velit tempor, id interdum mi cursus. Mauris et leo in quam commodo rhoncus. Aliquam a erat iaculis, dignissim est eget, rutrum justo. Phasellus vel orci id risus maximus pellentesque ac nec sapien. Etiam sed commodo erat, in suscipit magna. Nullam accumsan nisl ex, sit amet malesuada enim luctus sed.</p>\n",
        "protected": false
    },
    "excerpt": {
        "rendered": "<p>In ornare ante lectus, nec mollis mi cursus non. Mauris risus ante, tincidunt sit amet euismod eget, congue non est. Nam vitae vulputate leo. Vivamus fringilla nulla ut nisl ornare pulvinar. Aliquam imperdiet pellentesque risus.</p>\n",
        "protected": false
    },
    "author": 12,
    "featured_media": 0,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "template": "",
    "format": "standard",
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/173"
            }
        ],
        "collection": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts"
            }
        ],
        "about": [
            {
                "href": "http://example.com/wp-json/wp/v2/types/post"
            }
        ],
        "author": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/users/12"
            }
        ],
        "replies": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/comments?post=173"
            }
        ],
        "version-history": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/173/revisions"
            }
        ],
        "wp:attachment": [
            {
                "href": "http://example.com/wp-json/wp/v2/media?parent=173"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
}
    
10.08.2017 / 23:24