Shorten and optimize php custom code for wordpress page template and use shortcodes I'm pretty new to php coding, but i know wordpress for a long time. Also experimenting with bootstrap. I created a new Page Template and modified it a bit. What I'm looking for is to optimize this php code:
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col kb_landing_left&quot;&gt;
    &lt;?php
        global $post; 
        $post = get_post(56); 
        setup_postdata($post);
        get_template_part( 'template-parts/content', 'page', get_post_format() );
    ?&gt;
&lt;/div&gt;
&lt;div class=&quot;col -sm-6&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col kb_landing_right&quot;&gt;
    &lt;?php
        global $post; 
        $post = get_post(1); 
        setup_postdata($post);
        get_template_part( 'template-parts/content', 'page', get_post_format() );
    ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col kb_landing_right&quot;&gt;
    &lt;?php
        global $post; 
        $post = get_post(3); 
        setup_postdata($post);
        get_template_part( 'template-parts/content', 'page', get_post_format() );
    ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col kb_landing_right&quot;&gt;
    &lt;?php
        global $post; 
        $post = get_post(2); 
        setup_postdata($post);
        get_template_part( 'template-parts/content', 'page', get_post_format() );
    ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


As you can see, it's calling for 4 specific post IDs, since i want them to appear on my homepage in a bootsrap grid. I guess this could be done better by using an array?
Any suggestion is very much appreciated! Stay healthy!
Here is an improvement, but I would recommend a few additional changes as well.
&lt;?php global $post; ?&gt;

&lt;div class=&quot;col kb_landing_left&quot;&gt;
    &lt;?php
    $post = get_post(56);
    setup_postdata($post);
    get_template_part('template-parts/content', 'page');
    ?&gt;
&lt;/div&gt;
&lt;div class=&quot;col -sm-6&quot;&gt;
    &lt;?php foreach ([1, 3, 2] as $post_id) : ?&gt;
        &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;col kb_landing_right&quot;&gt;
                &lt;?php
                $post = get_post($post_id);
                setup_postdata($post);
                get_template_part('template-parts/content', 'page');
                ?&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;?php endforeach; ?&gt;
&lt;/div&gt;

&lt;?php
// Reset back to the original post
$post = get_queried_object();
setup_postdata($post);
?&gt;

Since you're new to PHP here are some notes on PHP specifically

you only need to get the global variable once. Once a global variable is pulled in, it exists for the entire scope (whatever that scope happens to be).
if your column code is the same it should be in a foreach loop

Some WordPress stuff

get_template_part should only accept two parameters $slug and $name, maybe you meant get_template_part( 'template-parts/content', 'page' . get_post_format() ) possibly?
changing the global $post will typically cause a lot of problems down the line, so I would not do that unless you're certain it needs to be done. I would recommend something like a WordPress shortcode or PHP function that handles your template for non-current post data. Only use get_template_part for calling templates for the currently queried post.
if you do change the global $post, you'll (typically) need to reset it back to the original value because the rest of the (WordPress) code is dependent on it.

Please let me know if you're interested in using a WordPress shortcode for your columns.
Hope that helps!
Edit: As a side note, it looks like your html is malformed and a little bit different from the init question. Can you please check that as well. Also, please let me know if you're interested in using WordPress shortcodes. We can certainly optimize more, as we can write a little extra something today to will provide us a lot of value in the future. If WordPress is something you're never going to come back to again, then these micro-optimizes are not worth while. If this is something you plan to stick we, then it would be a good idea to take things a bit further. Thanks!