Site logo

Archived topic

Generate_do_template_part ( 'archive' ) custom post

3 replies · Started by Matic on March 9, 2022

Viewing posts 1–4 of 4

Hi,

I'm creating a custom homepage that would look something like this:

HEADER

#Some text
Custom_post_1 Custom_post_2 Custom_post_3

#Some text
Custom_post_4 Custom_post_5 Custom_post_6

#Some text
Custom_post_7 Custom_post_8 Custom_post_9

But to do that, I can't just use custom hooks, but actually create a home.php, which I have.

For a certain Custom_post_1 Custom_post_2 Custom_post_3, I wrote the following code:

<?php

if (generate_has_default_loop())
{
    if (have_posts()):

        do_action('generate_before_loop', 'archive');

        $thePostIdArray = array(
            "1",
            "2",
            "3"
        ); /* Some custom post IDs */
        $limit = 3;

        while (have_posts()):

            the_post();
            $counter++;

            if ($counter < $limit + 1):

                $post_id = $thePostIdArray[$counter - 1];
                generate_do_template_part('archive', get_post_format($post_id));
            endif;
        endwhile;
    else:
    endif;
} ?>

This code still outputs the most recent 3 posts and not the "1", "2", and "3". I realize that I need to somehow convey these post IDs, probably through generate_do_template_part, but I don't know how.

I couldn't find any useful documentation either (this didn't help). I tried reverse engineering via theme-fuctions.php lines 560-586, but I couldn't figure it out.

Could you please help me with that?

Hi there,

if you did not have the text between each row of 3 posts, would we just see the 9 latest posts ?

Hi David,

No these would be some custom (hand-picked) posts. I chose post ID examples 1-9 for readability

EDIT: I realize I could create the same static HTML as the archive has and then manually add various attributes, like thumbnail, title, link, etc.

EDIT #2: I found a solution using foreach. I don't if it's the best out there, but it gets the job done.

If it helps anyone in the future, here's the code:

$ids = array("1", "2", "3"); /* Use your actual post IDs */

foreach ($ids as $id ) {
    $post = get_post ( $id );
    setup_postdata( $post );
    generate_do_template_part(  'archive' );
}

Cheers,
Matic

Glad to hear that and thanks for sharing

This archived topic is closed to new replies.