【问题标题】:Correct HTML Structure inside PHP IF ConditionPHP IF 条件中的正确 HTML 结构
【发布时间】:2014-04-10 09:56:04
【问题描述】:

这是我修改后的 Wordpress 循环,带有 PHP If 和 Else 条件代码。

    <?php if (have_posts()) : ?>

<?php
$i = 1;

while (have_posts()) {

    the_post(); 


    if ($i <= 1) {

            echo '<div class="firstp">';

                echo '<a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';
                    the_post_thumbnail('article-oneimg', array( 'id'    => "article-oneimg")); 
                echo '</a>';

                echo '<div class="thedate1">';
                    the_date();
                echo '</div>';

                echo '<h1><a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';;
                    the_title();
                echo '</a></h1>';

                echo '<p>';
                    the_excerpt();
                echo '</p>';

            echo '</div>';



    } elseif ($i <= 10) {
        echo '<div class="HOLDER-OF-secondp">';     
        include "secondp.php";
        echo '</div>';



    } else {
        // There should be a bunch of HTML mixed in here too
        the_title();
    }
    $i++;
}

?>



<?php else : ?>

<h2>No Posts Found</h2>

<?php endif; ?>

secondp.php 包含以下代码:

<div class="secondp">
    <a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
        <?php the_post_thumbnail('article-twoimg', array( 'id'  => "article-twoimg")); ?>
    </a>
    <div class="thedate2">
        <?php the_date(); ?>
    </div>
    <h1>
        <a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
</div>

我在这里要做的只是添加 secondp.php 的持有者/容器,因为它有几个帖子。但是每次我在include "second.php"; 之前和之后使用echo '&lt;div class="HOLDER-OF-secondp"&gt;'; 时,都会发生这种情况(见下图使用 Firebug 拍摄的。)

PHP IF 条件中的 HTML 结构有什么问题?我试图实现的是放置一个包含所有 div 类 secondp 的容器。

在简单的 HTML 中,它是这样的:

<div class="holderofsecondp">
<div class="secondp">
a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
        <?php the_post_thumbnail('article-twoimg', array( 'id'  => "article-twoimg")); ?>
    </a>
    <div class="thedate2">
        <?php the_date(); ?>
    </div>
    <h1>
        <a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
</div>
</div>

【问题讨论】:

  • HOLDER-OF-secondp div 代码是否在循环中?您可以发布该文件的完整代码吗?
  • 你的条件有问题 if($i == 1) { echo '
    ';回声'

    '; the_excerpt();回声'

    ';回声'
    '; } } elseif ($i 1) { echo '
    ';包括“secondp.php”;回声'
    ';
  • 让我知道你到底想要什么??可能是你的 $i 正在增加。
  • 嗨罗恩,我已经发布了我修改后的 WP 循环。您也可以参考此stackoverflow.com/questions/22963786/multiple-wordpress-loops 以供您参考该循环的目的。
  • 是的,我想你,但让我知道你想要什么,我会根据它写下代码。你不需要“HOLDER-OF-secondp”这个出现多次。告诉我你想要的结构

标签: php html wordpress


【解决方案1】:

试试这个代码:-

<?php global $wp_query;?>
<?php if (have_posts()) : ?>

<?php
$i = 1;
$start_hoder = true
$total_posts = $wp_query->found_posts;
while (have_posts())
{

    the_post();

    if ($i <= 1) {

        echo '<div class="firstp">';

            echo '<a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';
                the_post_thumbnail('article-oneimg', array( 'id'    => "article-oneimg"));
            echo '</a>';

            echo '<div class="thedate1">';
                the_date();
            echo '</div>';

            echo '<h1><a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';;
                the_title();
            echo '</a></h1>';

            echo '<p>';
                the_excerpt();
            echo '</p>';

        echo '</div>';

    } elseif ($i <= 10) {
        if($start_hoder)
        {
            echo '<div class="HOLDER-OF-secondp">';
            $start_hoder = false;
        }
            include "secondp.php";
        if($total_posts == $i && $start_hoder == false)
        {
            echo '</div>';
        }
    } else {
        // There should be a bunch of HTML mixed in here too
        the_title();
    }
    $i++;
}
?>

<?php else : ?>

<h2>No Posts Found</h2>

<?php endif; ?>

这可能会帮助您获得预期的输出。

【讨论】:

【解决方案2】:

看起来你有一个循环,在你的循环内你反复echoing 持有者。您想重新排列代码以使循环内没有持有者。

【讨论】:

  • 我知道,这确实是我的问题。我不知道怎么做。
【解决方案3】:
<div class="holderofsecondp">
<?php  while (have_posts()) { ?>

    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>

<?php } ?>

</div>

&lt;div class="holderofsecondp"&gt;这将永远不在循环中

或在您的代码中执行此操作

<?php if($i==0) {?> <div class="HOLDER-OF-secondp"><?php } ?>

并在循环之外添加 div,但这不是一个好主意。如果您需要更多帮助,请告诉我。

【讨论】:

  • 如果有人降级答案,请将您的 cmets bcos 这不是竞争,我们在这里为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 2011-02-02
  • 2021-12-21
  • 2020-09-05
  • 1970-01-01
相关资源
最近更新 更多