wordpress 显示所有帖子而不是分类
wordpress displays all posts instead of categorised
我正在尝试让不同类别的 post 显示在不同的页面上,以便我可以将它们用于 "services" 和 "case studies" 页面。
我设置了两个页面,它们显示每个人的链接 post,但是显示所有 post,而不仅仅是特定类别。
除 post 类别 ID 外,每个页面上的代码都相同。
如何在不同的页面上显示不同的类别?
服务页面:
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=4");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
<?php endforeach;
}
}
?>
案例研究页面:
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=5");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
<?php endforeach;
}
}
?>
回答
没有具体的类别设置,只有要显示的帖子数量,在本例中为 10。
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=service&posts_per_page=10"); //CHANGE CODE AND ADD THIS LINE***************************
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
这会设置特定的类别和帖子数量(使用类别 slug 将其拉入)
category_name=service&posts_per_page=10
我正在尝试让不同类别的 post 显示在不同的页面上,以便我可以将它们用于 "services" 和 "case studies" 页面。
我设置了两个页面,它们显示每个人的链接 post,但是显示所有 post,而不仅仅是特定类别。
除 post 类别 ID 外,每个页面上的代码都相同。
如何在不同的页面上显示不同的类别?
服务页面:
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=4");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
<?php endforeach;
}
}
?>
案例研究页面:
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=5");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
<?php endforeach;
}
}
?>
回答
没有具体的类别设置,只有要显示的帖子数量,在本例中为 10。
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=service&posts_per_page=10"); //CHANGE CODE AND ADD THIS LINE***************************
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
这会设置特定的类别和帖子数量(使用类别 slug 将其拉入)
category_name=service&posts_per_page=10