function some_function() {
    $template_options = get_post_meta(get_the_ID(), 'the_s_t_l_id', true);
    if ('layout_7' === $template_options || 'layout_8' === $template_options || 'layout_9' === $template_options){
        echo 'displaynone';

Class →

.displaynone {display:none;}

But when I am calling function it doesnt work here →

<aside class="sidebar <?php some_function(); ?>">

However, if I call this function somewhere else, and if the logic is true then it does print the anticipated class.

what is the reason that it is not working in the desirable place then?

1 Answer 1


This is because you have functions that work only inside of the WordPress Loop like get_the_id(). This will return the correct value inside the WP Loop in template files, but when called in other places does not return the correct value and check fails.

See this for more information on the WordPress loop


When outside the loop you can either use the global $post object to get the id like this

global $post
$id = $post->ID;

Alternately you can try this

$id = get_queried_object_id();
  • Actually, when I write the whole loop within the function it worked for me. I don't know if writing the loop within the function is a great/correct method or not.
    – WordCent
    Dec 1, 2017 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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