2016-08-12 120 views
0

我試圖在不使用WordPress的網站的主頁上顯示最近的博客帖子,使用php。用php顯示非WordPress網站上的博客文章

這是我的代碼:

<?php require('../../../../../../../blog/wp-blog-header.php'); 

?> 

<?php 
$args = array('numberposts' => 3, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date"); 
$postslist = get_posts($args); 
echo '<ul id="latest_posts">'; 
foreach ($postslist as $post) : setup_postdata($post); ?> 
    <li><strong><?php the_date(); ?></strong><br /> 
    <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"> <?php the_title(); ?></a> 
</li> 
<?php endforeach; ?> 
</ul> 

但我發現了這個錯誤:

Fatal error: require(): Failed opening required '../../../../../../../blog/wp-blog-header.php' (include_path='/home/devbh/public_html/app/code/local:/home/devbh/public_html/app/code/community:/home/devbh/public_html/app/code/core:/home/devbh/public_html/lib:.:/usr/lib/php:/usr/local/lib/php') in /home/devbh/public_html/app/design/frontend/test1/default/template/page/hpBlog.phtml on line 35

我已確認的文件路徑是正確的,這樣不應該成爲問題,但真的不能想到它會是什麼?有任何想法嗎?

+0

閱讀[這裏](https://wordpress.org/support/topic/integrating-wp-in-external-php -pages) –

+0

不能使用[xml-rpc](https://codex.wordpress.org/XML-RPC)? –

回答

0

一些示例代碼,它應該如何工作的:

<?php 
// those two lines at the very top 
define('WP_USE_THEMES', false); 
require('/path/to/your/wordpress/wp-load.php'); 

// ... rest of your code like for example ... 

query_posts('showposts=1'); 
while (have_posts()): the_post(); ?> 

<h2><?php the_title(); ?></h2> 
<?php the_excerpt(); ?> 
<p><a href="<?php the_permalink(); ?>">Read more...</a></p> 

<?php endwhile; ?> 

下面是一些更細節的文章:

https://wordpress.org/support/topic/display-wordpress-content-outside-of-your-blog-corvid-works

如果你的錯誤仍然存​​在,而這兩個網站上不同的虛擬運行主機,然後檢查你的apache配置。確保你的虛擬主機可以訪問你的其他目錄(從安全的角度來看,它的惡意做到這一點)