最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to call posts under a specific category on static front page?

programmeradmin1浏览0评论

I want to show posts according to categories on the static front page (home page) only.

Any suggestions.

  1. can I show the contents from function.php or
  2. which file is responsible for the static front page. I am using a custom theme.

I want to show posts according to categories on the static front page (home page) only.

Any suggestions.

  1. can I show the contents from function.php or
  2. which file is responsible for the static front page. I am using a custom theme.
Share Improve this question edited May 24, 2017 at 14:31 user9447 1,7927 gold badges30 silver badges55 bronze badges asked May 24, 2017 at 10:33 inrsaurabhinrsaurabh 2033 silver badges14 bronze badges 2
  • Have you tried some plugin like Content Blocks (Custom Post Widget) And how can you show the content bock on a specific page? It is recommended to install the Widget Logic plugin, this will give you complete flexibility on widget placement. – André A. Commented May 24, 2017 at 13:39
  • 1 No I didn't tried any such plugin,thanks for suggestions. – inrsaurabh Commented May 24, 2017 at 15:42
Add a comment  | 

1 Answer 1

Reset to default 1

WordPress by default will look for front-page.php as the custom home page. For future reference, you will want to bookmark the WordPress Theme Hierarchy page. It includes a flow chart of what files WordPress will look for in order.

In the code of your front-page.php file, or an include you can conditionally call in page.php, you will want to insert a loop with a custom query using the WP_Query class. Say the category you want has the slug news:

$args = array (
   'category_name' => 'news'
);

$front_page_query = new WP_Query( $args );

What this code will do is query the WordPress database and grab all posts that are categorized under news. You can do this by using the category's ID, but slug is probably the best way IMO because it doesn't involve having to look up the IDs in the table all the time and is more meaningful if someone else works with this code as well.

From there you just use the loop as you would any other page with one addition. Since you used a custom query, you have changed the $post global variable from the default so you will need to reset it. This is accomplished by using the wp_reset_postdata() function just before the else in your if/then statement. So your loop would look like this...

<?php if ( $front_page_query->have_posts() ) : ?>
   <?php while ( $front_page_query->have_posts() ) : $front_page_query->the_post(); ?>
      // Code for displaying the post
   <?php endwhile; ?>

   <?php wp_reset_postdata(); ?>

<?php else: ?>

   // No Posts Found Code

<?php endif; ?>

You can add other arguments to refine your query such as limiting the number of posts it returns, skipping any password-protected posts, etc. Just add them to the $args array.

发布评论

评论列表(0)

  1. 暂无评论