php - 如何显示彼此相邻的四个类别帖子? - WordPress的

标签 php css wordpress categories posts

我正在尝试从类别“3”中提取 4 个最新帖子以显示在 1000 像素的 div 中,但将每个帖子显示在 280 像素的 div 中并内联显示。

现在,我的代码提取最近的帖子,但只为最近的帖子设置样式。

这是当前的 URL:http://dylanpolniak.com/clients/c&c/ 这是我的代码:

<div id="music">
    <h1>Music</h1>
    <div class="hr_dark"></div>
    <div id="posts">

        <?php query_posts('showposts=4&cat=3'); ?>

            <div class="single_post">
                <?php the_content(); if (have_posts()) : while (have_posts()) : the_post(); if ( has_post_thumbnail() ) {
                // check if the post has a Post Thumbnail (featured image) assigned to it.
                    the_post_thumbnail(); the_title('<h4>', '</h4>');
                } ?>
            </div>

        <?php endwhile; else : endif; ?>

    <div class="hr_dark"></div>
    <a href="music.php"><h2>View All</h2></a>

<style>
    #posts { width: 1000px; margin: auto; }
    .single_post { width: 280px; height: 400px; position: relative; display: inline-block; float: left; }
    h4 { font-size: 20px; font-family: 'Satisfy', cursive; color: #202e31; text-align: center; font-weight: 400; }
</style>

最佳答案

尝试将类 div 置于 while 循环内,然后您的#posts div 未关闭。

像这样更改您的代码:

<div id="posts">

<?php query_posts('showposts=4&cat=3'); 
the_content(); if (have_posts()) : while (have_posts()) : the_post(); 
if(has_post_thumbnail() ) { ?>
    <div class="single_post"> <?php
        // check if the post has a Post Thumbnail (featured image) assigned to it.
        the_post_thumbnail(); the_title('<h4>', '</h4>'); ?>
    </div>
<?php
} ?>   

</div> //add this closing div tag!

关于php - 如何显示彼此相邻的四个类别帖子? - WordPress的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119308/

相关文章:

php - 如何通过 PHP 创建由点组成的文本图像?

css - 如何排列两个表格而不让它们的内容重叠?

php - WordPress。使用 substr 对类别标题进行字符限制

php - 移动 wordpress 网站后链接断开

php - MYSQL 使用 AND 和 OR 从数据库中选择数据

javascript - 在从提交按钮提交之前使用 ajax 发布一些字段值

html - 带有滚动条的父 TD BUT 的整个高度中的 div

html - CSS 格式无法使文章在移动设备上可读

php - SQL 计数在 PHP 代码内部不起作用

html - 使用 CSS 将 Div 垂直向下或向上定位