• On my index.php I’m displaying posts by category.

    <?php
        $i = 0;
        $cat1 = $cat2 = $cat3 = $cat4 = $cat5 = $cat6 = $cat7 = Array();
        if (have_posts()) : while (have_posts()) : the_post();
    
        if ( in_category( 'neuigkeit' ) ) {
            $cat1[ $i ]['the_title'] = get_the_title();
            $cat1[ $i ]['the_content'] = get_the_content();
            $cat1[ $i ]['the_permalink'] = get_permalink();
            $cat1[ $i ]['the_ID'] = get_the_ID();
        }
        if ( in_category( 'geschichte' ) ) {
            $cat2[ $i ]['the_title'] = get_the_title();
            $cat2[ $i ]['the_excerpt'] = get_the_excerpt();
            $cat2[ $i ]['the_permalink'] = get_permalink();
            $cat2[ $i ]['the_ID'] = get_the_ID();
        } // ... more categories...

    My first category with News is called with this code:

    foreach( $cat1 as $post ) { ?>
        <article class="entry-content">
            <h2><?php _e("<!--:rm-->Eveniments<!--:--><!--:de-->Ereignisse<!--:-->"); ?></h2>
            <h3><?php echo $post['the_title']; ?></h3>
            <?php echo get_the_post_thumbnail( $post['the_ID'], 'medium' ); ?>
            <p><?php echo $post['the_content']; ?></p>
            <p class="byline vcard">
            <?php printf( __( '<time class="updated" datetime="%1$s" pubdate>%2$s</time>', 'bonestheme' ), get_the_time('Y-m-j'), get_the_time(get_option('date_format')), get_the_author_link( get_the_author_meta( 'ID' ) )); ?>
            </p>
        </article>

    The problem is, that only the oldest article is display, but I just want to display the newest article. How can I change my code?

    My entire index.php can be found here: https://gist.github.com/anonymous/aabbc83663408ce0cf2f24ce2f820b70

Viewing 5 replies - 1 through 5 (of 5 total)
  • By checking code, it seems like you are getting only 1 post per category ?

    Thread Starter irenehofer

    (@irenehofer)

    Yes. I do only need 1 post per category. But the newest one, not the oldest one…

    just replace this code

    if ( in_category( 'neuigkeit' ) ) {
            $cat1[ $i ]['the_title'] = get_the_title();
            $cat1[ $i ]['the_content'] = get_the_content();
            $cat1[ $i ]['the_permalink'] = get_permalink();
            $cat1[ $i ]['the_ID'] = get_the_ID();
        }

    with

    if ( in_category( 'neuigkeit' ) ) {
            if(!empty($cat1)){
            $cat1[ $i ]['the_title'] = get_the_title();
            $cat1[ $i ]['the_content'] = get_the_content();
            $cat1[ $i ]['the_permalink'] = get_permalink();
            $cat1[ $i ]['the_ID'] = get_the_ID();
        }
    }

    and check if it solves your issue.

    sorry with

    if(empty($cat1))

    Thread Starter irenehofer

    (@irenehofer)

    Pefect! Thanks a lot for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Last post by category’ is closed to new replies.