【问题标题】:(wordpress) Show title & content of posts instead of the current page title & content?(wordpress)显示帖子的标题和内容而不是当前页面的标题和内容?
【发布时间】:2018-10-12 22:55:26
【问题描述】:

我正在尝试将我的帖子加载到#portfolio div 中。我想显示标题、内容和缩略图。代码本身似乎有效,但它向我显示了“主页”的标题和内容。这是一个单页设计。有没有办法获取此页面上的帖子?下面是代码的具体部分。

<div id="portfolio">
  <div class="head-title col-md-12">
      <h1 class="h1b">Portfolio</h1>
  </div>
  <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div class="col-md-3">
       <?php the_title(); ?>
       <?php the_content(); ?>
       <?php the_post_thumbnail(); ?>
    </div>
    <?php endwhile; ?>
  <?php endif; ?>
</div>

由于能够查看此页面的其余代码可能也很有用,因此我将其发布在下面。希望你能帮帮我!

<?php /* Template Name: Homepage */ ?>

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style.css">
</head>
<body>
<div id="navbar">
  <nav>
    <ul>
      <li><a href="#navbar">home</a></li>
      <li><a href="#oversuus">over suus</a></li>
      <li><a href="#portfolio">portfolio</a></li>
      <li><a href="#contact">contact</a></li>
    </ul>
  </nav>
</div>

<div id="header">
  <div id="logo"><img src="<?php bloginfo('template_directory'); ?>/images/logo.svg" /></div>
</div>

<div id="oversuus">
  <div class="head-title col-md-12">
      <h1 class="h1a">Over Suus</h1>
  </div>
  <div class="oversuus-text">
    <?php
    $page_id = 32; //id example
    $page_data = get_page( $page_id );
    $content = apply_filters('the_content', $page_data->post_content);
    $title = $page_data->post_title;
    echo $content;
    ?>
  </div>
  <div class="oversuus-img">

  </div>
</div>

<div id="portfolio">
  <div class="head-title col-md-12">
      <h1 class="h1b">Portfolio</h1>
  </div>
  <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div class="col-md-3">
       <?php the_title(); ?>
       <?php the_content(); ?>
       <?php the_post_thumbnail(); ?>
    </div>
    <?php endwhile; ?>
  <?php endif; ?>
</div>

<div id="contact">
  <div class="head-title col-md-12">
      <h1 class="h1c">Contact</h1>
  </div>
  <?php
  $page_id = 34; //id example
  $page_data = get_page( $page_id );
  $content = apply_filters('the_content', $page_data->post_content);
  $title = $page_data->post_title;
  echo $content;
  ?>
</div>

<div id="questions">
  <p>
    x
  </p>
  <div class="mailbt">
    MAIL
  </div>
</div>

<div id="footer">

</div>
</body>
</html>

【问题讨论】:

  • 你想查看WP_Query类。
  • @cabrerahector 谢谢!当您知道在哪里/如何看时,会更容易弄清楚哈哈。

标签: wordpress get posts


【解决方案1】:

虽然我不知道我是否使用了最好的方法,但由于@cabrerahector 的建议,我设法解决了我的问题。正如你可能看到的那样,我仍然是一个菜鸟,所以我显然还没有充分考虑它。只是发布这个,以防有另一个菜鸟可以使用它。

  <?php
          $query = new WP_Query( array(
         'post_type' => 'post',
         'orderby' => 'date',
         'order' => 'ASC',
         'posts_per_page' => 4
      ) );

      if( $query->have_posts() ) {
          while( $query->have_posts() ) {
              $query->the_post();
      ?>

          <?php the_title(); ?>
          <?php if(has_post_thumbnail()) {
              $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
               echo '<img src="' . $image_src[0]  . '" width="200px" height="200px"  />';
          } ?>

          <?php

          }
      }
  ?>

【讨论】:

  • 看起来差不多,除了你应该在while 语句之后调用wp_reset_postdata();。干得好:)
猜你喜欢
  • 2011-11-17
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 2020-04-15
  • 1970-01-01
  • 2013-04-06
相关资源
最近更新 更多