← WordPress

últimos 5 posts através da API do wordpress

Lida 5604 vezes

Offline

Nuno 
Administrador
Mensagens 7780 Gostos 216
Feedback +2

Troféus totais: 42
Trófeus: (Ver todos)
100 Poll Votes Level 7 Search Mobile User Level 6 Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes

É possível criar um pequeno script em php, para ir buscar por exemplo os últimos 5 posts de um blog wordpress e apresenta-los noutro site.

http://kobesearch.cpan.org/htdocs/WordPress-API/WordPress/API/Post.html

Será que qualquer coisa do género iria funcionar? Ou isto so funciona para acesso remoto?

Código: [Seleccione]
<?php 
$wp 
= new elWpAPI('http://.../xmlrpc.php''admin''...');


 
$last10Ppos =  $wp->getPosts(5); 
?>
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

RSS?
Offline

Nuno 
Administrador
Mensagens 7780 Gostos 216
Feedback +2

Troféus totais: 42
Trófeus: (Ver todos)
100 Poll Votes Level 7 Search Mobile User Level 6 Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes

RSS?

RSS não é uma opção... Não faz sentido ires buscar os posts por RSS, se poderes ter links directos.
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Vou ver se consigo fazer algo. ;)
Offline

Nuno 
Administrador
Mensagens 7780 Gostos 216
Feedback +2

Troféus totais: 42
Trófeus: (Ver todos)
100 Poll Votes Level 7 Search Mobile User Level 6 Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes

Este gajo diz que assim dá
http://www.corvidworks.com/articles/wordpress-content-on-other-pages

Código: [Seleccione]
<?php
// Include Wordpress 
define('WP_USE_THEMES'false);
require(
'/var/www/example.com/blog/wp-load.php');
query_posts('showposts=1');
?>


Mas o output foi este...

Código: [Seleccione]
Fatal error: Call to undefined method stdClass::set_prefix() in /home/maistraf/public_html/wp-settings.php  on line 287
Offline

bonsempregos 
Membro
Mensagens 2008 Gostos 7
Feedback +3

Troféus totais: 29
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

se isso é para um script externo, e presumindo que esta na mesma maquina, pq nao fazes um query directo a base de dados do wordpress?

Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Pelo o que estou a ver, o xmlrpc só dá para POST e não GET, precisas de um plugin, mas acho para fazeres download tens de te registar -> http://blog.5ubliminal.com/posts/remote-control-wordpress-blog-xmlrpc-api/
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Nop, tou errado... Mas não estou a ver como chamar o xmlrpc. :/

EDIT:
Arranjei maneira, mas não encontro como pegar nos posts, só encontro as paginas. -> http://codex.wordpress.org/XML-RPC_wp :/
Offline

Nuno 
Administrador
Mensagens 7780 Gostos 216
Feedback +2

Troféus totais: 42
Trófeus: (Ver todos)
100 Poll Votes Level 7 Search Mobile User Level 6 Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes

Nop, tou errado... Mas não estou a ver como chamar o xmlrpc. :/

EDIT:
Arranjei maneira, mas não encontro como pegar nos posts, só encontro as paginas. -> http://codex.wordpress.org/XML-RPC_wp :/

ORiOn, já estou a dar cabeçadas nas paredes...

Não percebo, tem as funcionalidades todas, mesmo um get post :/
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

ORiOn, já estou a dar cabeçadas nas paredes...

Não percebo, tem as funcionalidades todas, mesmo um get post :/
Acho estranho não encontrar grande info :S

No codex(link que te mostrei), não tem get post. Só get pages, o que funciona bem.

Código: [Seleccione]
<?php
require "IXR_Library.inc.php";

$rpc = new IXR_Client"http://site.com/xmlrpc.php" );
$status $rpc->query(
    
"wp.getPages",    // method name
    
"1",                // blog id
    
"uername",   // username
    
"pass" // password
);

if( !
$status ) {
    print 
"Error ( " $rpc->getErrorCode( ) . " ) : ";
    print 
$rpc->getErrorMessage( ) . "\n";
    exit;
}

$data $rpc->getResponse( );
print_r$data );
?>

Offline

Nuno 
Administrador
Mensagens 7780 Gostos 216
Feedback +2

Troféus totais: 42
Trófeus: (Ver todos)
100 Poll Votes Level 7 Search Mobile User Level 6 Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes

E qual é o output disso, é as páginas completas? Será que uma página com uma template coms os posts é solução? :/


O bonsempregos sugere ir directamente buscar a info à bd, será qualquer coisa deste género, o problema é que as bd são diferentes...

Código: [Seleccione]
<?php
$fivesposts 
$wpdb->get_results("SELECT * FROM $wpdb->posts
WHERE post_status = 'published'"
);
if (
$fivesposts) :
foreach ($fivesposts as $post) :
setup_postdata($post);
?>

<h2><a href="<?php the_permalink(); ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php
endforeach;
else :
?>

    <h2> Not Found</h2>
<?php endif; ?>
 <?php $wpdb->print_error(); ?>
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

http://libertytuga.com/testes/wordpress-API/get-posts.php

É esse o resultado, depois com outras funções podes pegar apenas no que necessitas.
Offline

bonsempregos 
Membro
Mensagens 2008 Gostos 7
Feedback +3

Troféus totais: 29
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

o que precisas nao é apenas do post title, e do slug (url)?

entao o query directo a base de dados é a melhor solução, não faz sentido tares a carregar em memoria o wp todo (wp-load.php), onde vais carregar plugins, filtros etc etc para ires buscar 2 campos a 5 rows de uma base de dados.

Tens que fazer uma pagina onde incluas a ligação a base de dados do wordpress e a query, depois é so chamares essa pagina.
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Código: [Seleccione]
<?php
mysql_connect
("localhost","username","password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());


$query "SELECT * FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 5";
$result mysql_query($query) or die(mysql_error());

while(
$row mysql_fetch_array($result)){
 
    echo 
$row['post_title'];
    echo 
$row['post_content'];
   
}

mysql_close();
?>


Isso apenas pega no titulo e no conteúdo. Para mostrar mais coisas, vai ao phpMyAdmin -> database do wordpress -> wp_posts e vês as outras ids. ;)
Offline

bonsempregos 
Membro
Mensagens 2008 Gostos 7
Feedback +3

Troféus totais: 29
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

e limitares a query a 5 posts? ;) (basta meter um LIMIT)

já agora fecha a query da base de dados, senao ao fim de umas horas ficas com quantas ligaçoes abertas no mysql sem necessidade.