index.php 的第一部分,目前有很大的帖子显示,包含以下条目:
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="p-heading"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
和 archive.php 的第一部分包含这些条目:
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content-a">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="imgdiv"><a href="<?php the_permalink() ?>"><img src="<?php echo catch_that_image() ?>" width="250"></a></div>
<div class="p-heading"><h1><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content">
<?php the_excerpt(); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12"><?php $totalcomments = get_comments_number(); echo $totalcomments; ?></div>
</div>
<?php endwhile; ?>
我使用不同样式的 div id="content" 用于大帖子,div id="content-a" 用于较小的帖子显示,连续 3 个。
现在,我希望只有最新的帖子会以大格式显示,如 css 中的 #content 所定义的那样,其余的就像他们在带有 #content-a 的 archive.php 上一样。我怎样才能做到这一点?
我的网站主索引页面是http://www.virmodrosti.com/,存档在这里http://www.virmodrosti.com/zdravje/
请让我知道,谢谢。
您的 css 选择器有两个问题。
1 - 您不能使用 divid='content-a'
为多个帖子设置样式,因为 id 是唯一的。您必须使用class=content-a
.
2 -id='content'
你的帖子循环里面没有if (have_posts()) : while (have_posts()) : the_post();
。唯一的id=content
就是在循环之外。无论如何,它将应用于所有帖子。
解决方法是使用循环内的类。在您的代码中,最好的代码post
是更高的 div 类。
然后你需要使用while (have_posts())
循环来标记第一篇文章......
索引.php
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php
$first_post = true;
while (have_posts()) : the_post(); ?>
<div class="<?php
if ($first_post){
$first_post = false;
echo 'post-first';
}else{
echo 'post';
}
?>">
<div class="p-heading"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
在 archive.php 中,我不明白为什么要在 .phpWP_Query
之后创建一个新对象if (have_posts())
。但既然这不是问题的一部分,也不是问题的一部分,我就这样离开......
档案.php
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php
$query = new WP_Query(array('posts_per_page'=> 2,));
$first_post = true;
while ($query->have_posts()) : $query->the_post(); ?>
<div class="<?php
if ($first_post){
$first_post = false;
echo 'post-first';
}else{
echo 'post';
}
?>"> <div class="p-heading"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句