【问题标题】:PHP code to display all posts based on category基于类别显示所有帖子的 PHP 代码
【发布时间】:2012-12-19 19:46:13
【问题描述】:

我希望在我的 wordpress 主页 index.php 中插入一行代码,它将显示除“日记”类别的帖子之外的所有帖子。

原代码行如下

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <div <?php post_class(); ?>>
    <span class="postlabel"><a href="<?php the_permalink() ?>"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></a></span>
        <h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
            <p class="info">
                <span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span>
                <span class="cmtinfo"><?php comments_popup_link( __( 'Leave a comment', 'Mflat' ), __( '1 Comment', 'Mflat' ), __( '% Comments', 'Mflat' ) ); ?><?php edit_post_link(' Edit', ' &#124; ', ''); ?></span>
            </p>
        <div class="entry">
            <?php $Mflat_options = get_option('Mflat_theme_options'); ?>
            <?php if ( $Mflat_options['home_excerpt_check']== 1 ) { the_excerpt(__('&raquo; Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?>
         </div>
        <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
            <p class="infobottom">
                <?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?>
                <?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?>
                </p>
    </div>
        <?php endwhile; ?>
    <div class="flip">
        <div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div>
        <div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div>
    </div>
<?php else : ?>
<div id="main">
 <h2><?php _e('Not Found', 'Mflat'); ?></h2>
</div>
    <?php endif; ?>

我尝试了以下代码但无济于事

<?php if(have_posts()) : ?><?php while(have_posts()){
    <if(!in_category('Diary')){
        : the_post(); 
        }
    }?>

有没有更好的方法可以完成这项工作? 我是 PHP 新手,虽然我知道必须做什么的逻辑,但我不能完全生成代码来完成这项工作。我认为布尔值的工作量太大了,并且在明显溢出时发现了一个类似的(http://stackoverflow.com/questions/13587958/display-recent-posts-based-on-their-category-in-wordpress)线程但是它不太适合我的需要。

我们非常感谢每一个贡献。谢谢

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    只需在循环之前运行 query_posts() 函数 - 它会改变循环

    <?php
    query_posts(array(
        'category_name' => 'my-category-slug', // get posts by category name
        'posts_per_page' => -1 // all posts
    ));
    ?>
    
    <?php while(have_posts()): the_post(); ?>
        <h1><?php the_title(); ?></h1>
    
        <?php the_content(); ?>
    <?php endwhile; ?>
    

    我的错,我刚刚注意到您想要排除一个类别。相同的功能,不同的参数:

    $category = get_term_by('name', $cat_name, 'category'); // get category
    $id = $category->term_id; // get the category ID
    
    query_posts('cat=-' . $id); // exclude the found ID
    

    <?php
    $category = get_term_by('name', 'Diary', 'category'); // get category data
    $id = $category->term_id; // get the category ID
    
    // USE THIS IF YOU WANT TO _EXCLUDE_ ALL POSTS FROM "DIARY"
    query_posts($query_string . '&cat=-' . $id);
    
    // USE THIS IF YOU WANT TO GET ALL POSTS FROM "DIARY" ONLY
    // query_posts($query_string . '&cat=' . $id);
    ?>
    
    <?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
            <div <?php post_class(); ?>>
                <span class="postlabel"><a href="<?php the_permalink() ?>"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></a></span>
    
                <h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
    
                <p class="info">
                    <span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span>
                    <span class="cmtinfo">
                        <?php comments_popup_link( __( 'Leave a comment', 'Mflat' ), __( '1 Comment', 'Mflat' ), __( '% Comments', 'Mflat' ) ); ?>
                        <?php edit_post_link(' Edit', ' &#124; ', ''); ?>
                    </span>
                </p>
    
                <div class="entry">
                    <?php $Mflat_options = get_option('Mflat_theme_options'); ?>
                    <?php if ( $Mflat_options['home_excerpt_check']== 1 ) { the_excerpt(__('&raquo; Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?>
                 </div>
    
                <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
                <p class="infobottom">
                    <?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?>
                    <?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?>
                </p>
            </div>
        <?php endwhile; ?>
    
        <div class="flip">
            <div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div>
            <div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div>
        </div>
    <?php else : ?>
        <div id="main">
            <h2><?php _e('Not Found', 'Mflat'); ?></h2>
        </div>
    <?php endif; ?>
    

    【讨论】:

    • 如何隔离属于“日记”类别的帖子?这是否意味着代码将如下所示? 'Diary', // 按类别名称获取帖子 'posts_per_page' => -1 // 所有帖子 )); ?>
    • @Tim:非常感谢。我试过了,它和我想要的完全相反。它只显示以“日记”为类别的帖子,而不是相反
    • 在这种情况下,删除-,如下所示:query_posts('cat=' . $id);
    • 我只是无法让它与您添加的内容一起使用。我不知道“日记”类别应该放在哪里。我会用索引页面的整个代码更新我的问题,看看是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多