[英]Show wordpress posts from one category only
我正在嘗試將某個類別的帖子顯示到顯示縮略圖的頁面上,然后鏈接到完整的帖子。 這將是一個案例研究部分。
我的鏈接顯示出來並引導我通過,但是它顯示所有帖子,而不是我想要的特定類別。
有任何想法嗎? 我不熟悉PHP,僅使用wordpress進行過說明
我的代碼:
<?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=10");
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 $catPost = get_posts(get_cat_ID("NameOfTheCategory")); //change this foreach ($catPost as $post) : setup_postdata($post); ?> <div> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p><?php the_content(); ?></p> </div> <?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();
}
?>
這將設置特定的類別和帖子數量(使用類別標簽將其插入)
CATEGORY_NAME =服務&posts_per_page = 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.