admin管理员组文章数量:1414863
I want to show posts according to categories on the static front page (home page) only.
Any suggestions.
- can I show the contents from function.php or
- 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.
- can I show the contents from function.php or
- which file is responsible for the static front page. I am using a custom theme.
- 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
1 Answer
Reset to default 1WordPress 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.
本文标签: categoriesHow to call posts under a specific category on static front page
版权声明:本文标题:categories - How to call posts under a specific category on static front page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745197271a2647203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论