简体   繁体   English

在 1 页中按不同类别显示帖子 - Wordpress 开发

[英]Dispaly posts by different category in 1 page - Wordpress Development

I have created custom post type, and I am dispalying posts in archive page, but I wanna display posts by different categories in 1 single page.我创建了自定义帖子类型,我在存档页面中显示帖子,但我想在 1 个单页中显示不同类别的帖子。 Like this:像这样:
在此处输入图片说明
But my posts are currently like this但我的帖子目前是这样的
在此处输入图片说明
How can i achieve that?我怎样才能做到这一点? I have searched a lot but didn't find any solution to do this.我已经搜索了很多,但没有找到任何解决方案来做到这一点。 So, that's why I am putting question here.所以,这就是我在这里提出问题的原因。 Here is my code:这是我的代码:

<section class="careerBlogs">
    <div class="container">
        <div class="row with-gutters">
                <?php


                    $args = array (
                        'post_type'    => array( 'career' ),
                        'post_status'  => array( 'publish' ),
                        'nopaging'     => true,
                        'order'        => 'ASC',
                        'orderby'      => 'menu_order',
                    );

                    $templates = new WP_Query( $args );

                    if ( $templates->have_posts() ) {
                        while ( $templates->have_posts() ) {
                                $templates->the_post(); global $post;

                        $customVars = get_post_meta($post->ID, 'custom_vars', true);
                        if(!empty($customVars)){
                            $isRemotely = $customVars['remotely'];
                        }

                        // Categories
                        $categories = get_the_terms( $post->ID, 'career_category' );
                        foreach( $categories as $category ) { ?>
                            <div class="col-xs-12">
                                <div class="careerBlogs--title">Open Positions in <?= $category->name; ?></div>
                            </div>
                        <?php } ?>

                        <div class="col-xl-12">
                            <div class="card mb-4">
                                <a href="<?= get_the_permalink(); ?>" class="card-link">
                                    <div class="card-info">
                                        <div class="card-title"><?= get_the_title(); ?></div>
                                        <div class="location">
                                            <span>Lahore</span>/<span><?= $isRemotely ? 'Remote' : '' ?></span>
                                        </div>
                                    </div>
                                </a>
                            </div>
                        </div>

                    <?php  } } else {
                        echo 'no posts to show';
                    }

                    wp_reset_postdata();

                ?>
        </div>
    </div>
</section>

It is in my archive-career.php page.它在我的archive-career.php页面中。 Can you please help me to achieve that?你能帮我实现这个目标吗? I am stuck here我被困在这里

After struggling a lot, I have solved my problem with this:经过一番挣扎,我已经解决了这个问题:

<?php
        // I get my Categories
        $categories = get_terms('career_category' );
        $currentCatName = '';
        foreach( $categories as $category ) {  ?>

        <div class="row with-gutters">
            <div class="col-xs-12">
                <!-- Assiging category name -->
                <div class="careerBlogs--title">Open Positions in <?= $category->name; ?></div>
            </div>


    <?php

    $args = array (
        'post_type'    => array( 'career' ),
        'post_status'  => array( 'publish' ),

        // here with 'tax_query' i solved my problem to show posts 
        // by categories (not to show double category names)
        'tax_query' => array(
            array(
                'taxonomy' => 'career_category',
                'field'    => 'slug',
                'terms'    => $category->slug,
            ),
        ),
    );

    $templates = new WP_Query( $args );

    if ( $templates->have_posts() ) {
        while ( $templates->have_posts() ) {
            $templates->the_post(); global $post;

        ?>

        <div class="col-xl-12">
            <div class="card mb-4">
                <a href="<?= get_the_permalink(); ?>" class="card-link">
                    <div class="card-info">
                        <div class="card-title"><?= get_the_title(); ?></div>
                        <div class="location">
                            <span>Lahore</span>/<span>Remote</span>
                        </div>
                    </div>
                </a>
            </div>
        </div>

    <?php  } } else {
        echo 'no posts to show';
    } ?> 
    </div> 
    <?php }

    wp_reset_postdata();

?>

See:看:
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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