1
<?php  
if (is_single()) { ?>
<?php $imvi = get_post_meta(get_the_ID(), 't_puck_imvi', true); ?>
<div class="class1" <?php if( 'none' === $imvi) { ?> style="display:none;" <?php } ?>>
    <div class="<?php if( 'show_vi' === $imvi  ||  'show_im' === $imvi) {echo 'video';} ?>">
        <?php if( 'show_vi' === $imvi || 'show_vint' === $imvi ) { ?>
            <div>
            <?php
                    // some code here
             ?>
        </div>
        <?php } ?>
        <?php if( 'show_image' === $imvi || 'show_image_notext' === $imvi ) {
            the_post_thumbnail( 'large');
        }
        ?>
    </div>  
</div>
<?php } ?>

Above is a code that is executed in single.php within a WordPress loop. All good No Issues.

But I want to achieve something else.

Instead, I want to make this code executed somewhere on the front page, but not within a loop but based on some particular post. so I believe we need to use the ID of that post, Right?

so I believe the secret lies somewhere here:

<?php $imvi = get_post_meta(get_the_ID(), 't_puck_imvi', true); ?>

can you guide me how to achieve this w/o loop, but by some specific Post ID.

1

Converting a dynamic piece of code into a static one is a pretty straight forward task. It's the other way that is complex.

Any WordPress function that can accept get_the_ID(), can also accept a string as an ID. Some even accept arrays.

So, simply turning

get_post_meta(get_the_ID(), 't_puck_imvi', true);

to

get_post_meta( 123 , 't_puck_imvi', true);

Will do the job.

Note

Not all the functions that can be used withing a loop accept a post ID. An example would be the_post_thumbnail_url() function that only takes a size as its input. In such cases, there are most likely an equal function to use, such as get_the_post_thumbnail_url().

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.