获取特定类别以及该类别内的所有帖子

问题描述 投票:0回答:1

我创建了一个博客,顶部部分是最新帖子,第二部分是新闻通讯,第三部分是报告部分,我需要报告类别以及报告类别中的所有帖子。

报告类别中的帖子应该有缩略图和看起来像卡片的标题。

结果下方列出了报告类别中的帖子列表。但用字符串来设计它是多余的。

 <?php 
                $post_args = array(
                    'post_type' => 'post', //get post by 'post' (default post type).
                    'posts_per_page' => -1, //show all posts.
                    'tax_query' => array( 
                        array( 
                            'taxonomy' => 'category', 
                            'field' => 'slug', 
                            'terms' => array('Report') 
                        )
                    ) 
                );
                $the_query = new WP_Query($post_args);
                if ( $the_query->have_posts() ) {
                    while ( $the_query->have_posts() ) {
                        $the_query->the_post();

                        //show all list the post.
                        echo '<li>
                                <a href="'.get_permalink().'">'.get_the_title().'</a>
                            </li>';
                    }
                    wp_reset_postdata();
                }
            ?>

这就是我到目前为止列出的帖子,现在我需要一个“foreach”循环来正确设置它的样式。这就是我所拥有的,但它没有显示任何内容。

<?php
    $post_args = array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'feild' => 'slug',
                'terms' => array('Report')
            )
        )
    );
    $the_query = new WP_Query($post_args);

if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
    foreach($the_query as $query) : ?>
        <a href="<?php echo get_permalink() ;?>">
            <ul>
                <?php get_template_part('includes/section', 'blogsmall') ;?> 
                <?php get_the_title() ;?>
            </ul>
        </a>

    
        
    <?php endforeach ;?>
<?php endwhile; else: endif;?>

感觉自己做错了什么。没有错误并且根本不显示任何内容。这里的区别是我可以放入正确的代码而不是将 WordPress 代码放入字符串中。

我更喜欢我实现的第二个适配代码,但不显示任何内容。如果您能提供帮助,将不胜感激。

提前致谢!

php wordpress post categories blogs
1个回答
0
投票

感谢@ChrisHaas 花时间做出实际回应,让我思考而不是白白给我。事实上,我必须好好睡一觉,然后以全新的心态、眼神和心情重新开始。

以下是我的问题的最终结果。

<?php 
    $post_args = array(
        'post_type' => 'post', //get post by 'post' (default post type).
        'posts_per_page' => -1, //show all posts.
        'tax_query' => array( 
            array( 
                'taxonomy' => 'category', 
                'field' => 'slug', 
                'terms' => array('Report') 
            )
        ) 
    );
$the_query = new WP_Query($post_args);
    // wp_reset_postdata() This has a long standing issue for 12 years which causes some problem.

    if ( $the_query -> have_posts() ) : while ( $the_query -> have_posts() ) : $the_query -> the_post(); ?>

        <a href="<?php the_permalink() ;?>">
            <li>
                <div class="featured-images">
                    <?php get_template_part('includes/section', 'blogsmall') ;?>
                </div>
                <div class="post-content">
                    <?php the_title() ;?>
                </div>
            </li>
        </a>

<?php endwhile; else : endif ;?>

以上是最终结果,谢谢@ChrisHaas。事实证明,问题是

wp_reset_postdata()
并且还刮掉了 @ChrisHaas 提到的
foreach
循环的想法,谢谢你,伙计!

我刚刚开始使用 PHP 和 Wordpress 一周了。所以一切对我来说都是新的。请原谅我的逻辑@ChrisHaas

© www.soinside.com 2019 - 2024. All rights reserved.