簡體   English   中英

在PHP IF條件中糾正HTML結構

[英]Correct HTML Structure inside PHP IF Condition

這是我修改的Wordpress循環,帶有PHP If和Else Condition代碼。

    <?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的容器/容器,因為它有多個帖子。 但是每次我使用echo '<div class="HOLDER-OF-secondp">'; include "second.php";之前和之后include "second.php"; 發生這種情況(請參見下面使用Firebug拍攝的圖像。)

在此處輸入圖片說明

PHP IF條件內的HTML結構有什么問題? 我試圖實現的是放置一個容納所有divs 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>

試試這個代碼:

<?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; ?>

可能這將幫助您獲得預期的輸出。

像你看上去有一個循環,一個你的循環里面卻反復echo荷蘭國際集團的持有人。 您想重新排列代碼,使循環內沒有持有人。

<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>

<div class="holderofsecondp">這將永遠不在循環中

或在您的代碼中執行此操作

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

並添加div循環外,但這不是一個好主意。 讓我知道您是否需要更多幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM