Global variables within a function - Please Help

tips-and-tricks

#1

Hi all,

I understand that all global variables have to be within a function now.

However, I’m struggling to think of a way to retrieve the ID of the “Page Template” that contains posts and each post makes a call to check an option of the “Page Template”.

Usually I would do the following e.g. blog.php ( Blog Template )

global $myvar;

$myvar 	= ( get_post_meta( $post->ID, '_'. THEME_PREFIX .'_someoption', true ) !='' ) ? get_post_meta( $post->ID, '_'. THEME_PREFIX .'__someoption', true ) : '';

Within the post I’d then use that variable.

Since it now has to be within within a function e.g.

function myvar() 
{
	global $wp_query;
	$post = $wp_query->post;

        $myvar = ( get_post_meta( $post->ID, '_'. THEME_PREFIX .'_someoption', true ) !='' ) ? get_post_meta( $post->ID, '_'. THEME_PREFIX .'__someoption', true ) : '';
    
       return  $myvar;

}

The issue is, that now tries to use the ID of the post that called it, not the ID of the “Page Template”.

I’m desperate for some help with this, thank you. It’s probably something simple, but my Friday brain is missing it if it is.


All global variables should be within a function or class
How to fix a WordPress theme soft reject Issues?
How to fix a WordPress theme soft reject Issues?
#2

Maybe something like this is what you’re after:

function myvar( $postid = NULL ) {

if ( $postid == NULL ) {
global $wp_query;
$post = $wp_query->post;
$postid = $post->ID;
}
$myvar = ( get_post_meta( $postid, ‘_’. THEME_PREFIX .‘someoption’, true ) !=’’ ) ? get_post_meta( $postid, '’. THEME_PREFIX .’__someoption’, true ) : ‘’;

return $myvar;
}

and then whenever you need a specific post id, you pass it in:

$myvar = myvar( ‘your-id’ );

Hope it helps!


#3

just need to add an ID parameter, like this:
function myvar($page_id){


#4

Hi,

Thanks so much for the reply. However, that’s not the issue - the issue is the $postid becomes the ID of the post calling the function and not the ID of the “Page Template” that’s displaying the posts. I hope that makes sense?

I’ve found one way to guarantee I’m always retrieving the “Page Template” id and not the ID of one of the posts within that page, by using the following:

		$url = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
		$post_id = url_to_postid($url[0]);	

#5

Actually, that only works if permalinks are enabled.


#6

This is a similar issue - it used to be easy when using Globals. But as soon as the loop occurs, there doesn’t seem to be an easy way to get the ID of the original page. http://stackoverflow.com/questions/2054319/how-to-get-wordpress-page-id-after-looping-posts


#7

Also, WTH are we supposed to do with the $more tag?

Even WordPress doesn’t use it within a function -
https://codex.wordpress.org/Customizing_the_Read_More#When_to_set_.24more


#8

I’m probably misunderstanding something, but if you simply want the page id, have you tried $myvar = get_the_ID(); before manipulating the loop inside the page template? No need for globals with that one.