简体   繁体   English

在博客外显示wordpress帖子

[英]Show wordpress posts outside of the blog

I have two wordpress blogs on my site and now I want to show the posts of categories of both blogs on the home page. 我的网站上有两个wordpress博客,现在我想在主页上显示两个博客类别的帖子。 I am using the following code. 我正在使用以下代码。

<?php require($_SERVER['DOCUMENT_ROOT'] . '/projects/wp-load.php'); 
 query_posts('cat=9& showposts=8');  if (have_posts()) : while (have_posts()) :   
the_post(); ?>
<ul><li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php 
$shorttitle4 = substr(the_title('','',FALSE),0,25)."...";

echo $shorttitle4;
?></a>

</li></ul>
<?php endwhile; else: echo "no posts"; endif; ?>
<?php wp_reset_query(); ?>

One of my blogs is placed in the "projects" folder and another one is placed in "technology folder". 我的一个博客放置在“项目”文件夹中,另一个博客放置在“技术文件夹”中。 but this code is showing the posts only from projects blogs if I changed the folder in as "technology" in the above code. 但是如果我在以上代码中将文件夹更改为“ technology”,则此代码仅显示来自项目博客的帖子。 I think this is because of wp-load.php file. 我认为这是因为wp-load.php文件。 Can you help me? 你能帮助我吗?

Thanks 谢谢

There is two or more ways, you can do it. 有两种或两种以上的方法,您可以做到。

  1. Using PHP Pear packages, like XML_RSS, XML_Tree and XML_Parser 使用PHP Pear包,例如XML_RSS,XML_Tree和XML_Parser
  2. Using RSS feed 使用RSS提要

Try RSS feed. 尝试RSS feed。 Because most of the hosting provider company will not provide packages installed ready server. 因为大多数托管服务提供商公司不会提供已安装软件包的现成服务器。

write on your theme function file (functions.php) 在主题函数文件(functions.php)上写

function read_rss($display=0,$url='') {
    $rss = new XML_RSS($url);
    $rss->parse();
    $itemArr = array_slice ( $rss->getItems(), 0, $display );
    return $itemArr;
}

Now write on your template file 现在写在您的模板文件上

<ul>
< ?php
    $items = read_rss(2,'http://sivel.net/feed');
    foreach ( $items as $item ) {
        echo '<li><a href="' . $item['link'] . '">' . $item['title'] . '</a>';
    }
?>
</ul>

You can do something more than it, follow this link or check this stackoverflow answer 您可以做更多的事情,点击此链接或检查此stackoverflow答案

You really need to give more information, but I think it might be because the following query might not be successful in the other folder 您确实需要提供更多信息,但是我认为这可能是因为以下查询在其他文件夹中可能无法成功执行

query_posts('cat=9& showposts=8'); 

Does category 9 exist in the technology site? 技术站点中是否存在类别9? Perhaps you need to do another query for the other folder? 也许您需要对另一个文件夹进行另一个查询?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM