简体   繁体   中英

How to display each category most recent post and change the order of categories whenever there is a new post in each category?

I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.

    <?php 
//Get the desired categories and order by ID
$cat_args = array(
    'orderby'    => 'id'
);

//For each category show a random post
$categories =   get_categories($cat_args);
foreach ($categories as $category) {
?>



    <?php
    $post_args = array(
        'numberposts'   => 1,
        'category'  => $category->term_id,
    );

    $posts = get_posts($post_args);
    foreach ($posts as $post) {
    ?>
        <article <?php post_class('post-list animated fadeIn'); ?> role="article">
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
                    <?php the_post_thumbnail('home-thum'); ?>
                    <?php archivecatname(); ?>
                </figure>

                <section class="entry-content cf">
                    <h1 class="h2 entry-title"><?php the_title(); ?></h1>
                    <div class="byline entry-meta vcard">
                        <?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
                    </div>
                    <div class="description"><?php the_excerpt(); ?></div>
                </section>
            </a>
        </article>

        <?php get_template_part('loop'); ?>

<?php
    }
}
 ?>

Query Arguments

$args = array(
    'cat' => $category->term_id,
    'post_type' => 'post',
    'posts_per_page' => '1',
);

Running the Query


$query = new WP_Query( $args );

if ( $query->have_posts() ) { ?>

    <section class="<?php echo $category->name; ?> listing">
        <h2>Latest in <?php echo $category->name; ?>:</h2>

        <?php while ( $query->have_posts() ) {

            $query->the_post();
            ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
                <?php if ( has_post_thumbnail() ) { ?>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail( 'thumbnail' ); ?>
                    </a>
                <?php } ?>

                <h3 class="entry-title">
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </h3>

            </article>

        <?php } // end while ?>

    </section>

<?php } // end if

// Use reset to restore original query.
wp_reset_postdata();

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