0

我正在使用 php 脚本来获取一些外部数据以显示在我的 wordpress 网站上。这已经完美运行了一年,但是当我不得不重新排列首页上的 som 元素时,循环中的帖子缩略图突然停止显示。

经过一些研究,我发现如果我在循环之后包含我的 php 脚本,则会出现帖子缩略图,但如果它包含循环之前,则帖子缩略图会神秘地消失。

PHP 日志没有给我任何提示,当循环之前包含脚本时,Wordpress 只会在后缩略图块中生成 NO html。

任何人有任何想法为什么会发生这种情况?我怎么能绕过它?

(PS。我需要在循环之前包含脚本的原因是样式/ css 的问题。我想我可以做一些 CSS-hacking 以使其在循环之后工作,但我宁愿找出导致问题的原因。)

这是这样的代码:

我的 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; ?>
4

1 回答 1

0

在某种程度上,您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. 因此,您处于“一切正常”所描述的情况。

然后您执行正确的内容,这也将起作用,并将在其他任何事情之前输出正确的内容。

最后,您输出$leftContent,此时它只是惰性的 HTML,不能与任何东西混淆。

但请记住,这是一种解决方法。它将在简单的设置中工作。更复杂的设置仍然不起作用,并且需要您调查真正的问题在哪里。

我假设你没有得到缩略图的原因是如果函数have_posts()在新的内容块之后被调用,它会突然返回 FALSE。如果它触发了错误,则必须查看应用程序的日志记录,如果没有发现错误,请了解为什么未报告此错误。但是现在让我们假设没有错误,并且您只需要快速找到出错的地方。

为此,您可以尝试不同的技巧,方法是在代码中包含这样的行

 have_posts() or die("SOMETHING JUST CLEARED MY POSTS!");

...我们知道,如果您将其放在开头,它将不会触发,并且帖子将得到输出。我们也知道,如果你反转这两个块,那条线就会触发。

因此,通过在生成右侧内容的代码中移动该行,从顶部开始,您将找到它触发的位置。之前的操作显然对您的帖子标志做了一些事情(可能是通过关闭数据库连接或类似的事情)。一旦你发现它做了什么,你就可以开始计划如何规避这个问题。

于 2013-02-27T10:47:35.800 回答