[英]WordPress: How to display only posts that are in a certain category?
我是WordPress的新手,但花了大約50個小時的時間來研究它,嘗試一下這樣的事情,感覺我現在已經有了很好的處理能力。
然而,我根本無法工作的一件事是讓一個頁面吐出某個類別的帖子列表。
這是我的例子: http : //dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters
我有一個帖子,如果我直接使用它可以正常工作,但不會顯示在此頁面上。
類別ID為“3”,而類別名稱為“印刷海報”。
我有一個印刷海報頁面的自定義頁面模板,如下所示:
<?php
/*
Template Name: Typographic Posters
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if (in_category('3')): ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?=get_image('flutter-image');?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
然而,使用此代碼只顯示頁面獲取標題,側邊欄和其他內容。
如果有人可以幫助我,那將真正幫助我處理wordpress類別的過濾。
謝謝閱讀,
Jannis
in_category
只能在單個頁面上的循環外工作。 我建議使用query_posts
函數來解決這個問題。 您可以使用query_posts('cat=3')
或query_posts('category_name=typographic-posters')
來獲取您要查找的帖子。
獲得后,只需使用普通的WordPress循環即可訪問這些帖子。
in_category只能在單個頁面上的循環外工作。 我建議使用query_posts函數來解決這個問題。 您可以使用query_posts('cat = 3')或query_posts('category_name = typographic-posters')來獲取您要查找的帖子。
獲得后,只需使用普通的WordPress循環即可訪問這些帖子。
這非常有效,但請確保您進入設置>閱讀並將帖子頁面設置為 - 選擇 - 選項,否則它將覆蓋此查詢並轉儲所有最近的帖子,無論其類別如何。
最簡單的方法是創建一個名為category-3.php
的文件,並使用普通index.php
或category.php
文件中的標准代碼。 Wordpress將負責僅從id = 3的類別及其子類別中獲取帖子。
我已使用以下方法按類別ID過濾了帖子:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* The default post formatting from the post.php template file will be used.
* If you want to customize the post formatting for your homepage:
*
* - Create a new file: post-homepage.php
* - Copy/Paste the content of post.php to post-homepage.php
* - Edit and customize the post-homepage.php file for your needs.
*
* Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
*/
$is_post_wrap++;
if($is_post_wrap == '1') {
?><div class="post-wrap clearfix"><?php
}
get_template_part('post', 'homepage');
if($is_post_wrap == '3') {
$is_post_wrap = 0;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
只需在循環之前添加:
<?php query_posts="cat=3&showposts=5">
這將強制循環顯示來自類別3(cat = 3)的5個帖子(showposts = 5)。
http://codex.wordpress.org/Template_Tags/query_posts
只是讓你知道這些答案來自哪里......你可以用query_posts做更多有趣的功能。
如果您希望能夠在不通過代碼的情況下更改顯示的類別,此插件也可以幫助您: http : //wordpress.org/extend/plugins/advanced-category-excluder/
謝謝你分享你的想法是一個偉大的想法。 通常,您只需將主題的index.php或category.php復制到category-3.php,並調整該模板以進行所需的任何自定義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.