簡體   English   中英

如何僅在首頁運行此代碼,而不是在Wordpress Index的子頁面上運行?

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

我想在常規帖子正下方的首頁上顯示名為“精選”的特定類別中的最后5個修改后的帖子,但不會顯示在后續子頁面中。 我使用此代碼顯示最后五個帖子。 一切似乎都運行良好,除了這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中,Front page也可以

  • 你的最新帖子或
  • 靜態頁面

如果它是您的最新帖子,則條件將是

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

除此以外

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

你只需要檢查它是否是頭版

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM