Since version 4.7 of Wordpress no longer need to load Wordpress this way to search for 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']; ?>">« 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 of 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
}
]
}
}
https://stackoverflow.com/questions/45621495/get-post-from-a-single-category-with-have-posts-wordpress
– Bruno Cabral
ok, someone at Soen gave you this answer. It’s still wrong because it does 2 things that should not be done: load all the WP via
wp-load.php
to search for posts and usequery_posts
.– Ricardo Moraleida
I understood you are right, but I need to understand the issue to resolve there in Soen tbm.
– Bruno Cabral
And I don’t understand this your answer, I develop in front-end and used very little php...
– Bruno Cabral
$posts
is an array that returns all these post values right? and with this variable that I work to get the post data? pq actually I just need the post title and the image that comes inside the post, the text I want to ignore.– Bruno Cabral
I edited to insert a full example looking for the same information that was in your html
– Ricardo Moraleida
Thanks @Ricardo, it’s clearer. But it hasn’t worked yet.. I got the following error.. I believe it’s the foreach argument.. Warning: Invalid argument supplied for foreach()
– Bruno Cabral
That mistake says that
$posts
is not an array. You have to check your call to see if the answer is correct. Give avar_dump( $resp )
to see what’s being returned. I tested this code here and it worked.– Ricardo Moraleida
I tested the
var_dump( $resp )
but the error page and loses all the css, where I include in the code to see what is returning?– Bruno Cabral
even though the page lost css and got disfigured he returned to me
string(1270) "
– Bruno Cabral
Let’s go continue this discussion in chat.
– Bruno Cabral