我使用php脚本获取一些外部数据显示在我的wordpress网站。这已经完美地工作了一年了,但是当我不得不重新排列首页上的一些元素时,我循环中的帖子缩略图突然停止显示。
经过一些研究,我发现如果我在循环之后包含我的php脚本,则post缩略图出现,但如果它在循环之前包含,则post缩略图神秘地消失了。
PHP-log没有给我任何提示,当脚本包含在循环之前时,Wordpress只是在post缩略图块中生成no html。
有人知道为什么会发生这种情况吗?我怎么才能避开呢?
(PS。我需要在循环之前包含脚本的原因是样式/css问题。我想我可以做一些css hack使它在循环后工作,但我宁愿找出导致问题的原因。
代码如下:
我的index.php应该出现的缩略图(这不起作用):
<!-- ### This includes the php script, and works if it's placed bellow the loop/#leftcontent ### -->
<div id="rightcontent">
<?php include("rightcontentreleases.php"); ?>
</div>
<!-- ### Standard wordpress loop ### -->
<div id="leftcontent">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
<h2 class="posttitle"><a
href="<?php the_permalink() ?>"
rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- ### The post thumbnail ### -->
<a href="<?php the_permalink() ?>" ><?php if (has_post_thumbnail()) {the_post_thumbnail();}?></a>
<div class="entry">
<?php the_excerpt(); ?><a class="readmore" href="<?php the_permalink() ?>">Read more</a>
</div>
</div><!-- .post -->
<?php endwhile; else: ?><p>Sorry, no posts matched your criteria.</p><?php endif; ?>
</div><!-- #leftcontent -->
这是:
<!-- ### Standard wordpress loop ### -->
<div id="leftcontent">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
<h2 class="posttitle"><a
href="<?php the_permalink() ?>"
rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- ### The post thumbnail ### -->
<a href="<?php the_permalink() ?>" ><?php if (has_post_thumbnail()) {the_post_thumbnail();}?></a>
<div class="entry">
<?php the_excerpt(); ?><a class="readmore" href="<?php the_permalink() ?>">Read more</a>
</div>
</div><!-- .post -->
<?php endwhile; else: ?><p>Sorry, no posts matched your criteria.</p><?php endif; ?>
</div><!-- #leftcontent -->
<!-- ### This includes the php script, and works if placed after the loop like this ### -->
<div id="rightcontent">
<?php include("rightcontentreleases.php"); ?>
</div>
外部脚本只是在外部数据库中循环,并显示一些标准的html:
<?php // this script connects to the external database, and defines some functions fetching the data ?>
<?php include_once("tigernet.php"); tigernetmysql(); get3upcoming(); ?>
<?php if (mysql_fetch_assoc($resultupcoming) > 0): ?>
<div class="rightcontentreleases" id="upcomingreleases">
<h2 class="head">Upcoming Releases</h2>
<?php while ($row = mysql_fetch_assoc($resultupcoming)) { ?>
<div class="onelatestrelease">
<a href="<?php bloginfo( 'wpurl' ); ?>/releases"><img class="albumartwork" src="http://media.tigernet.no/images/item/full/<?= $row["code"] ?>.jpg" /></a>
<h2 class="artist"><?= $row["artist"] ?></h2><br/>
<h3 class="title"><?= $row["title"] ?></h3><br/>
<?php if (isset($row['url'])): ?><div class="soundcloudplayer_right">
<object height='18'><param name='movie'value='http://player.soundcloud.com/player.swf?url=<?= $row['url'] ?>&auto_play=false&player_type=tiny&show_duration=false&show_user=false&show_playcount=false&font=Arial&color=92140e'>
<param name='allowscriptaccess' value='always'>
<param name='wmode' value='transparent'>
<embed wmode='transparent' allowscriptaccess='always' height='18' src='http://player.soundcloud.com/player.swf?url=<?= $row['url'] ?>&auto_play=false&player_type=tiny&show_duration=false&show_user=false&show_playcount=false&font=Arial&color=92140e' type='application/x-shockwave-flash'>
</object>
</div><?php endif; ?>
</div><!-- .onelatestrelease -->
<?php } ?>
</div><!-- #latestreleases -->
<?php endif; ?>
在某种程度上,您的rightcontentreleases.php
一定会弄乱响应have_posts()
的数据库对象。正确的做法是调查have_posts()
并验证为什么会发生这种情况以及发生在哪里。
或者你可以使用一个变通方法:
<?php ob_start(); ?>
<!-- ### Standard wordpress loop ### -->';
<div id="leftcontent">
...
</div><!-- #leftcontent -->
<?php $leftContent = ob_get_clean(); ?>
<div id="rightcontent">
<?php include("rightcontentreleases.php"); ?>
</div>
<?php print $leftContent; unset($leftContent); ?>
这基本上执行标准循环,将输出保存到$leftContent
中。所以,你是在你描述的"everything works"的情况下。
然后执行正确的内容,这也将工作,并将在其他任何内容之前输出正确的内容。
最后,输出$leftContent
,此时它只是惰性的HTML,不能与任何东西混淆。
但是记住,这是一个解决方法。它将在简单的设置中工作。更复杂的设置仍然不起作用,并且需要您调查真正的问题在哪里。
我认为你没有得到缩略图的原因是函数have_posts()
突然返回FALSE,如果它在新的内容块之后被调用。如果它触发了一个错误,那么您就必须查看应用程序的日志记录,如果没有发现错误,就应该理解为什么没有报告这个错误。但是现在让我们假设没有错误,您只需要快速找到出错的地方。
have_posts() or die("SOMETHING JUST CLEARED MY POSTS!");
…我们知道,如果你把它放在开头,它将不会触发,并且帖子将得到输出。我们还知道,如果你把这两个块颠倒,那一行将触发。
因此,通过将这一行移动到生成右侧内容的代码中,从顶部开始,您将找到它的触发位置。之前的操作显然对您的post标志做了一些操作(可能通过关闭DB连接或类似的事情)。一旦您发现了它所做的事情,您就可以开始计划如何绕过这个问题。