WordPress 类别,获取其所有帖子

发布于 2024-11-27 12:08:03 字数 3534 浏览 0 评论 0 原文

我目前已经完成了我正在构建的 WordPress 网站的主题,但我在让我的类别正常工作时遇到了问题。

我不知道如何循环遍历当前类别,如果我位于“/category/category-name”的 URL,我将如何循环属于类别“类别名称”的帖子。

上执行以下操作

我在我的模板

Category:

我得到以下输出

类别名称

那么我如何循环浏览该类别的帖子呢?

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>

所以 page-news.php 是我的新闻文章所在的位置,用户可以使用

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".

I I do the following on my template

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-12-04 12:08:03

我认为您没有根据上面的代码在 category.php 中输出任何内容。 the_post() 不回显任何内容。它只是将下一条记录放入 $post 中。试试这个...

<?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();
?>

page-news.php 中,您可能应该考虑使用 the_category() 来回显类别链接,而不是自己构建链接 - 这样您就可以就会知道链接是有效的......

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

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>
无人问我粥可暖 2024-12-04 12:08:03

您可以在此函数内使用页面模板 循环帖子

You can use this function inside your page template to loop the posts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文