简体   繁体   中英

Wordpress category, get all its posts

I am currently finish of my theme for a wordpress website that I am building, I am having a problem getting my categories to work though.

I am lost as to how I can loop through the current category if I am at a URL of say "/category/category-name" how would I loop the posts that belong to the category "category name".

II do the following on my template

<p>Category: <?php single_cat_title(); ?></p> <p>Category: <?php single_cat_title(); ?></p> I get the following output

Category Name

So how do I loop through that categories posts?

Category.php

<?php get_header(); ?> 
    <article class="content">   
        <section class="top">       
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>         

            <?php endwhile; else: ?>        
                 <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>      
            <?php endif; ?>
        </section>  
        <section class="middle">        
        </section> 
     </article> 
     <?php get_footer(); ?>

page-news.php

<?php get_header(); ?>
<article id="content">
    <section class="top">
        <?php query_posts('post_type=news'); ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <section class="news">
                <?php the_post_thumbnail(); ?>
                <h2><?php echo strtolower(the_title()); ?></h2>
                <span class="posted"><?php the_date("l jS M Y"); ?></span>
                <?php 
                    $content = get_the_content($more_link_text, $stripteaser, $more_file);
                    $content = apply_filters('the_content', $content);
                    $content = str_replace(']]>', ']]>', $content);
                ?>
                <p><?php echo substr(strip_tags($content), 0, 400).'…' ; ?><a href="<?php the_permalink(); ?>" class="read_more">Read More</a></p>
                <p class="tags">
                    <strong>Categories:</strong>
                    <?php
                    $post_categories = wp_get_post_categories( $post->ID );
                    $cats = array();

                    foreach($post_categories as $c){
                        $cat = get_category( $c );
                        $cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
                    }
                    ?>
                    <?php foreach($cats as $k => $v) : ?>
                        <a href="<?php echo $cats[$k]['slug']; ?>"><?php echo $cats[$k]['name']; ?></a>
                    <?php endforeach; ?>                    
                </p>
                <p class="tags">
                    <strong>Tags: </strong>
                    <?php $tags = wp_get_post_tags($post->ID); ?>
                    <?php foreach ($tags as $tag) : ?>
                        <a href="<?php echo $tag->slug; ?>"><?php echo $tag->name; ?></a>
                    <?php endforeach; ?>
                </p>
            </section>
        <?php endwhile; endif; ?>
        <?php get_sidebar('news'); ?>
    </section>
    <section class="middle">

    </section>
</article>

So page-news.php are where my news article reside, and the user can get the category using the link generated by <a href="<?php echo $cats[$k]['slug']; ?>"><?php echo $cats[$k]['name']; ?></a> <a href="<?php echo $cats[$k]['slug']; ?>"><?php echo $cats[$k]['name']; ?></a>

I don't think you're outputting anything in category.php based on the code above. the_post() doesn't echo anything. It just puts the next record into $post . Try this...

<?php
get_header();
$cat = get_category(get_query_var("cat"));
var_dump($cat); //make sure that there really is a valid category and it's the right one
?>

<h1 class="page-title"><?php echo $cat->name?></h1>
<?php 
if (have_posts()) {
    while (have_posts()){
        the_post();
        <h2><?php the_title()?></h2>
        <?php the_content()?>
      <?php
      }
} else {
    ?>
    <p>No posts found!</p>
   <?php
}
?>


<?php
get_footer();
?>

In page-news.php you should probably consider using the_category() to echo out the category links rather than constructing the links yourself -- that way you'll know that the links are valid....

<p class="tags"><?php the_category()?></p>

您可以在页面模板中使用此功能来循环帖子

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM