如何检查当前页面是否为WordPress博客文章?

huangapple go评论51阅读模式
英文:

How do I check if the current page is a blog post in wordpress?

问题

我正在为我的网站开发自定义主题,似乎无法检查当前页面是博客文章还是页面。我该如何做?

我的代码(post.php):

<?php get_header(); ?>

<?php if (is_front_page()) : ?>
    <div class="hero">
        <h1>你好,世界</h1>
    </div>
<?php elseif (is_page()) : ?>
    <div class="hero">
        <h1><?php the_title(); ?></h1>
    </div>
<?php elseif (is_singular()) : ?>
    <div class="hero">
        <h1><?php the_title(); ?></h1>
        <p>日期:<?php echo get_the_date(); ?></p>
        <p>分类:</p>
    </div>
<?php endif; ?>

<?php get_footer(); ?>
英文:

I'm developing a custom theme for my website and I can't seem to check if the current page is a blog post or a page. How would I do this?

My code so far (post.php):

&lt;?php get_header(); ?&gt;

&lt;?php if (is_front_page()) : ?&gt;
    &lt;div class=&quot;hero&quot;&gt;
        &lt;h1&gt;hello world&lt;/h1&gt;
    &lt;/div&gt;
&lt;? elseif (is_page()) : ?&gt;
    &lt;div class=&quot;hero&quot;&gt;
        &lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
    &lt;/div&gt;
&lt;? elseif (is_singular()) : ?&gt;
    &lt;div class=&quot;hero&quot;&gt;
        &lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
        &lt;p&gt;Date: &lt;?php echo get_the_date(); ?&gt;&lt;/p&gt;
        &lt;p&gt;Categories:&lt;/p&gt;
    &lt;/div&gt;
&lt;?php endif; ?&gt;

&lt;?php get_footer(); ?&gt;

答案1

得分: 1

The WordPress helper function is_singular() checks if the current request "is for an existing single post of any post type".

if ( is_singular( 'post' ) ) {
    // 请求是针对单个文章的。
}
if ( is_singular( 'page' ) ) {
    // 请求是针对单个页面的。
}

还有其他一些辅助函数,如 is_single()is_page(),与 is_singular() 稍有不同。

英文:

The WordPress helper function is_singular() checks if the current request "is for an existing single post of any post type".

if ( is_singular( &#39;post&#39; ) ) {
    // request is for a single post.
}
if ( is_singular( &#39;page&#39; ) ) {
    // Request is for a single page.
}

There are additional helper functions, like is_single() and is_page() that vary slightly from is_singular().

答案2

得分: 0

这是您提供的代码片段的翻译:

<?php
  get_header(); 
  if(get_post()->post_type === 'page') { 
?>

<div class="hero">
    <h1>你好,世界</h1>
</div>
<? 
  } elseif (get_post()->post_type === 'post') { 
?>
<div class="hero">
    <h1><?php the_title(); ?></h1>
</div>
<? 
   } else (is_singular()) { 
 ?>
<div class="hero">
    <h1><?php the_title(); ?></h1>
    <p>日期: <?php echo get_the_date(); ?></p>
    <p>分类:</p>
</div>
<?php } ?>

<?php get_footer(); ?>

希望这可以帮助您。

英文:

Hi you can use something like this....

&lt;?php
  get_header(); 
  if(get_post()-&gt;post_type === &#39;page&#39;) { 
?&gt;

&lt;div class=&quot;hero&quot;&gt;
    &lt;h1&gt;hello world&lt;/h1&gt;
&lt;/div&gt;
&lt;? 
  }elseif (get_post()-&gt;post_type === &#39;post&#39;) { 
?&gt;
&lt;div class=&quot;hero&quot;&gt;
    &lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
&lt;/div&gt;
 &lt;? 
   }else (is_singular()) { 
 ?&gt;
&lt;div class=&quot;hero&quot;&gt;
    &lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
    &lt;p&gt;Date: &lt;?php echo get_the_date(); ?&gt;&lt;/p&gt;
    &lt;p&gt;Categories:&lt;/p&gt;
&lt;/div&gt;
&lt;?php } ?&gt;

&lt;?php get_footer(); ?&gt;

Hopefully this one can help you...

huangapple
  • 本文由 发表于 2023年6月15日 01:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定