为了账号安全,请及时绑定邮箱和手机立即绑定

显示wordpress的第一个帖子

显示wordpress的第一个帖子

PHP
沧海一幻觉 2021-05-13 14:17:08
我才刚刚开始使用wordpress,所以对于你们中的一些人来说,我的问题听起来有点简单,但是我找不到如何使它起作用的方法。我正在wordpress上构建博客页面,我想以不同于其他文章的方式显示第一篇文章,然后显示一些静态文本,然后继续第二篇文章的帖子循环。我的第二个问题是我需要将缩略图以绝对位置放置在div内(现在,当我调整浏览器大小时,它正在怪异地缩小)。在我的引导程序原型上,我将图像放置在div中,并使用background属性导入了图像。用wordpress替换该行为的最佳方法是什么?我为此做了一个简单的草图:我已经搜索了stackoverflow并尝试了一些代码片段,但我无法使它们全部正常工作。顺便说一句,我正在使用UNDERSTRAP主题作为样板。
查看完整描述

3 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

您需要忘记默认的WordPress循环。请改用WP_Query类查询您的帖子。


$query = new WP_Query([

   ‘post_type’ => ‘post’,

   ‘posts_per_page => -1,

]);


 // Get the first post

$firstElement = array_shift($query->posts);


 // Get the other posts exect first element

$otherPosts = $query->posts;

现在,您需要在模板中循环以创建帖子网格。



查看完整回答
反对 回复 2021-05-28
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

此过程是将标签的类别更改为全角。有很多方法可以做到这一点,但我发现这很简单。首先,从content.php中取出标签并将其放在index.php中。这是为了检查并更改article标签内的类。


<div class="site-main" id="main">

<div class="row no-gutters">

<?php if ( have_posts() ) : ?>


    <?php /* Start the Loop */ ?>



    <?php 

    // Required variables

    $post = $posts[0]; $c=0; ?>


    <?php while ( have_posts() ) : the_post(); ?>



        <article class="col p-0 <?php $c++; if($c == 1) { echo 'col-12'; }else{ echo 'col-4'; } ?>" <?php post_class(); ?> id="post-<?php the_ID(); ?>"> 


            <?php


            /*

            * Include the Post-Format-specific template for the content.

            * If you want to override this in a child theme, then include a file

            * called content-___.php (where ___ is the Post Format name) and that will be used instead.

            */

            get_template_part( 'loop-templates/content', get_post_format() );

            ?>


        </article>



    <?php endwhile; ?>


<?php else : ?>


    <?php get_template_part( 'loop-templates/content', 'none' ); ?>


<?php endif; ?>


</div>



查看完整回答
反对 回复 2021-05-28
  • 3 回答
  • 0 关注
  • 137 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信