如何显示与其他人不同的wordpress第一篇文章?

技术用户

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 &raquo;'); ?>
</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/

请让我知道,谢谢。

里卡多 BRGWeb

您的 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 &raquo;'); ?>
</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 &raquo;'); ?>
</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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

显示wordpress的第一篇文章

WordPress的第一篇文章只在首页上有所不同

如何显示每个标签的第一篇文章

WordPress的:第一篇,专栏;其他文章,2栏

获取Instagram用户的第一篇文章

删除第一篇文章并在PHP中显示第二篇文章

在 Wordpress 循环中的第一篇文章后添加一个 div

wordpress:为第一篇文章设置自定义类,然后是下 2 篇文章,其余的则不同

在顶部显示模型中的第一篇文章

如何用octopress可视化我的第一篇文章?

如何自定义博客作者的第一篇文章

如何自定义仅存档/主页上的第一篇文章的CSS?

WordPress循环会忽略三个单独的meta_values的第一篇文章

Wordpress 只显示一篇文章

为什么我循环中的第一篇文章意外缩进

使用wp_query检查第一篇文章

仅在 Vanilla JavaScript 中附加在第一篇文章上的评论

PHP - Wordpress 上一篇文章链接显示在最后一篇文章中

jQuery UI手风琴仅适用于第一篇文章:WordPress自定义文章类型

单击自定义分类法时,如何使用第一篇文章数据在同一页面上重定向?

WP posts_per_page 和偏移量跳过第一篇文章

Joomla显示内容的另一篇文章

如何在静态页面中显示最后一篇文章的标题?

如何在 ruby on rails 中显示每个用户的最后一篇文章?

如何在其他情况下与其他人一起使用variabels

RoR-具有“查看更多信息”的博客条目会展开和折叠,仅指向第一篇文章

如何与Temple jinjia2或其他人一起显示结果?

我的 API 网关 UI 与其他人不同

MemoryCacheClient的工作原理与其他人不同-保留引用