如何在首页上运行此代码,而不是在Wordpress索引的子页面上


How to Run this Code on front page only, not on subpages of Wordpress Index?

我想显示最近5篇被修改的文章,这些文章来自一个名为"Featured"的特定类别,位于首页常规文章的正下方,但不显示随后的子页面。我使用这个代码来显示这最后五个帖子。一切似乎都很好,除了这5个帖子也运行在子页面上。

function modified_postsbycategory() {
  // the query
  $the_query = new WP_Query( array( 'category_name' => 'featured', 'orderby' => 'modified', 'posts_per_page' => 5 ) ); 
  // The Loop
  if ( $the_query->have_posts() ) : ?>
  <?php
  while ( $the_query->have_posts() ) :
    $the_query->the_post();
            get_template_part( 'template-parts/content', 'modified' );
        // End the loop.
        endwhile;
    // If no content, include the "No posts found" template.
    else :
        get_template_part( 'template-parts/content', 'none' );
    endif;
    ?>
  <?php wp_reset_postdata(); ?>
  <?php
}
  // Add a shortcode
  add_shortcode('categoryposts', 'modified_postsbycategory');

我在函数开始后使用这个条件标签,但我认为我没有做正确的方式。

<?php if (is_front_page() && !is_paged() ){ ?>

你能帮帮我吗?

PS:请检查代码,如果发现任何错误,请纠正我。我只是个初学者,不是专家。

在Wordpress首页可以是

  • 您的最新帖子或
  • 静态页面

如果是你最近的帖子那么条件将是

<?php if (is_home()){ ..... } ?>
否则

<?php if (is_front_page()){ ..... } ?>

你只需要检查它是否是首页

<?php if (is_front_page()){ ?>