在PHP IF条件下更正HTML结构
这是我修改的Wordpress循环w/PHP If和Else条件代码在PHP IF条件下更正HTML结构,php,html,wordpress,Php,Html,Wordpress,这是我修改的Wordpress循环w/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_pe
<?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; ?>
看起来你有一个循环,在你的循环中你重复地回响着支架。您希望重新排列代码,使保持器不在循环中
<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>
这永远是不可能的
或者在代码中这样做
<?php if($i==0) {?> <div class="HOLDER-OF-secondp"><?php } ?>
并将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; ?>
这可能会帮助您获得预期的输出。我知道,这确实是我的问题。我不知道怎么做。secondp div代码的持有者在循环中吗?您可以发布该文件的完整代码吗?如果$i==1{echo;echo'';the_摘录;echo'';echo;}}elseif$i 1,您的条件有问题{echo;include secondp.php;echo;让我知道你到底想要什么??可能是你的$i在增加。嗨,Ron,我已经发布了我修改过的WP循环。你也可以参考这个循环的目的。是的,我告诉你,但是让我知道你想要什么,我会根据这个写下代码。你知道吗不需要secondp的持有者这会出现多次。只要告诉我你想要的结构$total_posts=$wp_query->found_posts上有错误;你也可以尝试全局$wp_query;$total_posts=$wp_query->post_count;。请在此处查看接受的答案以了解计数:-如果有人对答案进行降级,请发表你的评论,因为这不是竞争,并且我们在此帮助:
<?php if($i==0) {?> <div class="HOLDER-OF-secondp"><?php } ?>
<?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; ?>