我正在尝试将不同类别的帖子显示在不同的页面上,以便我可以将它们用于“服务”和“案例研究”页面。
我设置了两个页面,它们显示每个帖子的链接,但是会显示所有帖子,而不仅仅是特定的类别。
除了帖子类别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;
}
}
?>
答案 0 :(得分:0)
<强> ANSWER 强>
没有特定的类别集,只有要显示的帖子数量,在本例中为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