我正在尝试将我的WordPress主页设置为某个类别,但它只允许我将其设置为最新帖子或静态页面。
是否可以将您的主页设置为帖子类别?
答案 0 :(得分:3)
我希望你知道如何设置静态页面。因此,首先创建一个空的.php文件,并将其命名为任何你喜欢的名称,然后将其放在其他文件中(index.php,arhive.php等)。
然后输入以下代码
<?php
/*
* Template Name: Category based Homepage
*/
?>
<?php get_header(); ?>
<div class="main">
<?php
$cat_ID = '1'; //it should be your category ID, you can get the id of the category by going to categories and edit and then in url you can find the tag_ID.
$posts_to_show = '10'; // number of posts from the category you want to show on homepage
//query_posts("cat=$cat_ID&showposts=$posts_to_show");
$category_posts = new WP_Query("cat=$cat_ID&showposts=$posts_to_show");
//if (have_posts())
if ($category_posts->have_posts())
: $first = true;
?>
<ul class="post-list">
<?php
//while (have_posts()) : the_post();
while ($category_posts->have_posts()) : $category_posts->the_post();
if ($first)
$class = "first-in-row";
else
$class = "";
$first = !$first;
?>
<!-- Start: Post -->
<li <?php post_class($class); ?>>
<?php the_post_thumbnail(); ?>
<p class="categories"><?php the_category(", "); ?></p>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php edit_post_link(__('Edit', 'your_theme_text_domain'), '', ''); ?></h2>
<p class="post-meta"><span class="date"><?php the_time(get_option('date_format')) ?></span> <?php if (comments_open()) : ?>, <span class="comments"><?php comments_popup_link(_x('0', 'comments number', 'your_theme_text_domain'), _x('1', 'comments number', 'your_theme_text_domain'), _x('%', 'comments number', 'your_theme_text_domain')); ?></span> <?php endif; ?> <span class="author"><?php the_author() ?></span></p>
<?php the_excerpt(); ?>
<p class="more"><a href="<?php the_permalink() ?>"><?php _e('Read More »» ', 'your_theme_text_domain'); ?></a></p>
<?php if (has_tag()): ?><p class="tags"><span><?php the_tags(""); ?></span></p><?php endif; ?>
</li>
<!-- End: Post -->
<?php endwhile; ?>
</ul>
<?php else : ?>
<h2 class="center"><?php _e('Not found', 'your_theme_text_domain'); ?></h2>
<p class="center"><?php _e('Sorry, but you are looking for something that isn\'t here.', 'your_theme_text_domain'); ?></p>
<?php
endif;
//wp_reset_query();
wp_reset_postdata();
?>
</div>
<?php get_sidebar(); //optional?>
<?php get_footer(); ?>
并根据自己的喜好替换$ cat_ID和$ posts_to_show。我已经使用了两种查询方法来根据您的需要进行调整。
希望能帮助那些正在寻找类似解决方案的人。
答案 1 :(得分:0)
您可以使用get_posts
创建一个模仿类别页面的自定义模板,并使用该模板将页面设置为主页,但在必须对类别slug进行硬编码的意义上,它不是完全动态的或该ID中的ID。假设您不想经常更改该类别,那应该不是问题。或者,您可以在模板中使用wp_safe_redirect重定向到类别页面 - 如果您希望将用户直接放在真实的类别页面,URL和所有类别页面上。
答案 2 :(得分:0)
我不确定你的主页是一个类别是什么意思,你的意思是在你的主页上显示的帖子只会来自某个类别吗?
答案 3 :(得分:0)
您只需要在循环之前执行WP_Query;
$query = new WP_Query("cat=10, paged=".get_query_var('paged'));
然后使用WP_Query对象来执行循环;
if($the_query->have_posts()):
while($the_query->have_posts()):
the_title();
the_content();
//Use all the loop function normally
endwhile;
endif;
如果您需要paginantion,分页参数用于确定您所在的页面。
不是使用类别ID,而是通过slug检索id。
$home = get_category_by_slug('home-category-slug');
然后你的查询将是这样的
$the_query = new WP_Query("cat=".$home->cat_ID.", paged=".get_query_var('paged'));
答案 4 :(得分:-1)
是的,可以转到信息中心&gt;&gt;设置&gt;&gt;阅读&gt;&gt;静态页面从下拉菜单中选择页面并保存。在那个页面上你可以创建自己的东西......