【问题标题】:How to get a post's title in WordPress如何在 WordPress 中获取帖子的标题
【发布时间】:2017-10-18 19:05:00
【问题描述】:

我在 WordPress 中工作,我需要循环显示帖子的标题和描述。

我不知道如何获取帖子的标题及其描述。

我当前的html代码如下:

<div class="col-md-4 col-sm-6">
    <div class="single-service-home">
        <div class="icon-box">
            <div class="inner-box">
                <i class="flaticon-gesture-1"></i>
            </div>
        </div>
        <div class="content">
            <h3>Charity For Education</h3>
            <p>There are many variations of lorem <br>passagei of Lorem Ipsum available <br> but the majority have </p>
            <a href="service-details.html">Read More</a>
        </div>
    </div>
</div>

在&lt;h3&gt;标签内的上述代码中,我需要显示标题,在&lt;p&gt;标签中我需要显示描述。

另外,在阅读更多内容时,我需要提供该帖子的详细信息页面链接。

【问题讨论】:

标签: php wordpress


【解决方案1】:

您需要以the loop 开头。 WordPress 文档有大量的示例信息。该循环是标准 PHP 代码的 sn-p,它在模板中设置当前帖子数据,并允许您为每个帖子(或页面)使用所有标准模板标签。

循环基本上是这样的:

<?php 
if (have_posts()): while (have_posts()) : the_post(); 
    //
    // HTML & any template tags go here.
    //
endwhile; endif;
?>

一旦您设置了“循环”,您就可以轻松地回显诸如the title、the permalink 之类的内容,以及大量其他模板标签。

例如,这是常见的:

<?php // Open the loop
if (have_posts()): while (have_posts()) : the_post(); ?>

  <h1>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </h1>
  <div class="content"><?php the_content();?></div>
  <a class="read-more" href="<?php the_permalink(); ?>">Read more</a>

<?php // End the loop
endwhile; endif; ?>

您还需要确保使用get header 和get footer 正确设置模板的其余部分。

说到 WordPress,只需谷歌一下。它已经在某个地方得到了回答,而 WordPress 文档是开始的地方。例如,搜索“Wordpress 节目标题”,您将获得所需的信息。

【讨论】:

  • 非常感谢 t-jam.. 我也尝试搜索解决方案.. 但是使用代码时它只返回页面标题而不是帖子标题。请帮忙
  • 我用这个代码&lt;h3&gt;&lt;?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?&gt; &lt;h2&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt; &lt;?php endwhile; else : ?&gt; &lt;p&gt;&lt;?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?&gt;&lt;/p&gt; &lt;?php endif; ?&gt;
  • 所以你有一个页面,你想在上面显示其他帖子,比如索引页面?
  • 是的..我已经为主页设置了一个模板,并在其中包含了页眉和页脚...现在我只想显示该页面中的帖子..无论我使用了the_title。 . 它只返回页面的标题而不是帖子的标题..
  • 好的,这与您提出的问题不同。在这种情况下,您需要一个辅助循环。您不仅想显示您所在页面的标题,还想从其他帖子中获取内容 - 这超出了循环的功能。您可以设置一个新的 wp_query 来定位您想要循环的帖子类型,或者您可以将您的主页设置为 WordPress 管理员中的“帖子页面”,允许您将其用作帖子索引。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
  • 2013-10-10
相关资源
最近更新 更多