【问题标题】:Looking to make archive.php display in two columns for Wordpress site希望将 archive.php 显示在 Wordpress 网站的两列中
【发布时间】:2021-10-10 15:04:11
【问题描述】:

这里是非常新的 Wordpress 学徒。试图让我的存档页面在两列中显示帖子,如下所示:

发布 1 发布 2

发布 3 发布 4

这是我们正在开发的 figma 的一个示例:https://ibb.co/N3XwtwD

我的问题是,我可以在我的文件中添加什么代码来允许这样做?我目前在此处插入的 html 部分上有一些引导类,它显示为一列,所以我不知道这些类是否会干扰任何东西。下面是我的archive.php代码:

<?php 

get_header();
?>

<div class="container mt-5 mb-5">
        <p class="font-size"><?php the_archive_title(); ?></p>
        <hr>
    </div>

<div class="container">
<?php 
  while(have_posts()) {

    the_post(); ?>

    <div class="row">

        <div class="col-lg-6">
            <p class="font-size text-center"><a href="<?php the_permalink();?>"><?php the_title();?></a></p>
            <img class="img-fluid mb-3"<?php
                        the_post_thumbnail(); 
                                ?>
            <p class="category-font">Published on <?php the_time('n.j.y');?></p>
            <p>Posted by <?php the_author_posts_link() ?></p>
        </div>
      </div>
<?php }
echo paginate_links();
?>


  </div>

  <?php
  get_footer();
  ?>

第一次在这里发帖,如果我遗漏了任何重要的内容,敬请见谅。真的很感激这个地方!

谢谢!

【问题讨论】:

    标签: php wordpress archive


    【解决方案1】:

    请替换为以下对您有帮助的代码。

    <?php
    get_header();
    ?>
    
    <div class="container mt-5 mb-5">
            <p class="font-size"><?php the_archive_title(); ?></p>
            <hr>
        </div>
    
    <div class="container">
    
    <?php 
    $counterval = 0;
      while(have_posts()) {
        the_post(); 
    
        $counterval++;
        if($counterval % 2 != 0)
        { ?>
            <div class="row">
        <?php } ?>
        <div class="col-lg-6">
                <p class="font-size text-center"><a href="<?php the_permalink();?>"><?php the_title();?></a></p>
                <?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
                if(!empty($featured_img_url)){ ?>
                    <img class="img-fluid mb-3" src="<?php echo esc_url($featured_img_url); ?>" />
                <?php } ?>            
                <p class="category-font">Published on <?php the_time('n.j.y');?></p>
                <p>Posted by <?php the_author_posts_link() ?></p>
            </div>
        <?php 
        if($counterval % 2 == 0)
        { ?>
        </div>
        <?php } ?>
        
    <?php } ?>
    
    <?php echo paginate_links();
    ?>
    
    
      </div>
    
      <?php
      get_footer();
      ?>
    

    【讨论】:

      【解决方案2】:

      您需要每两个帖子创建一行,并且您必须连续添加两次&lt;div class="col-lg-6"&gt;。检查下面的代码。

      <?php get_header(); ?>
      
      <div class="container mt-5 mb-5">
          <p class="font-size"><?php the_archive_title(); ?></p>
          <hr>
      </div>
      
      <div class="container">
          <?php 
          $count = 1;
          while( have_posts() ) { the_post(); 
              if ( $count % 2 == 1){ ?>
                  <div class="row">
              <?php } ?>
                  <div class="col-lg-6">
                      <p class="font-size text-center"><a href="<?php the_permalink();?>"><?php the_title();?></a></p>
                      <?php the_post_thumbnail(); ?>
                      <p class="category-font">Published on <?php the_time('n.j.y');?></p>
                      <p>Posted by <?php the_author_posts_link() ?></p>
                  </div>
              <?php if ( $count % 2 == 0 ){ ?>
                  </div>
              <?php } $count++; ?>
          <?php } 
          if ( $count % 2 != 1 ) echo "</div>";
          echo paginate_links(); ?>
      </div>
      

      【讨论】:

      • 啊,我想我明白你在说什么了。可悲的是,在复制代码后,它没有用。 :(
      • 你这个枪的儿子!它现在显示为两列!我需要做的就是添加一些其他的 css 样式,我想我很高兴。非常感谢!
      • 我正在寻找一种方法来做到这一点!能够接受答案,但我需要更多的声誉来支持哈哈。再次感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2017-02-25
      相关资源
      最近更新 更多