Avatar of Tammu
Tammu

asked on 

Wordpress echo a variable from a function that is inside a class

Hi There,

I have created a class which hold multiple functions. One of the function gets all the details about the author of a custom post type. I am trying to display the variables from this function in my author template.

Below is the code for my class that is in the functions.php

class pc_npPosts
{

function pc_get_np_info(){

       $pc_np_author_id    = get_query_var( 'author' );
        $pc_np_name = get_field('np_organization_name', 'user_'.  $pc_np_author_id);
        $pc_np_bio  = apply_filters('avf_author_description', get_the_author_meta('description',  $pc_np_author_id),  $pc_np_author_id);
        $pc_np_whatWeDo = get_field('np_what_we_do', 'user_'.  $pc_np_author_id);
        $np_organization_logo = get_field( 'np_organization_logo', 'user_'. $pc_np_author_id  ); 
        $np_profile_link = get_author_posts_url($pc_np_author_id);
        $np_organization_address = get_field( 'np_organization_address', 'user_'. $pc_np_author_id  ); 
        $np_organization_city = get_field( 'np_organization_city', 'user_'. $pc_np_author_id  ); 
        $np_organization_state = get_field( 'np_organization_state', 'user_'. $pc_np_author_id  );
        $np_organization_zipcode = get_field( 'np_organization_zipcode', 'user_'. $pc_np_author_id  );  
        $np_causes_interests = get_field( 'np_causes_interests', 'user_'. $pc_np_author_id );
}


}

Open in new window


I would like to display the above variable in author template like below

 $pc_np_author = New pc_npPosts;

 echo $pc_np_class->pc_get_np_info('pc_np_name');

Open in new window


but nothing displays. What am I missing or doing wrong.

Thanks
PHPWordPressWeb Development

Avatar of undefined
Last Comment
Tammu
Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

Do you have to return it instead or echo?
Avatar of Tammu
Tammu

ASKER

Hi Robert,

Thanks for replying. Appreciate it.

I am basically looking to display value of that variable on the template so it can be viewed on the webpage
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi there,

All the values you're setting are locked away inside a method, so they're never going to be accessible - you need to move them outside of your method, so they become properties of the class :

class pc_npPosts
{
        $pc_np_author_id    = get_query_var( 'author' );
        $pc_np_name = get_field('np_organization_name', 'user_'.  $pc_np_author_id);
        $pc_np_bio  = apply_filters('avf_author_description', get_the_author_meta('description',  $pc_np_author_id),  $pc_np_author_id);
        $pc_np_whatWeDo = get_field('np_what_we_do', 'user_'.  $pc_np_author_id);
        $np_organization_logo = get_field( 'np_organization_logo', 'user_'. $pc_np_author_id  ); 
        $np_profile_link = get_author_posts_url($pc_np_author_id);
        $np_organization_address = get_field( 'np_organization_address', 'user_'. $pc_np_author_id  ); 
        $np_organization_city = get_field( 'np_organization_city', 'user_'. $pc_np_author_id  ); 
        $np_organization_state = get_field( 'np_organization_state', 'user_'. $pc_np_author_id  );
        $np_organization_zipcode = get_field( 'np_organization_zipcode', 'user_'. $pc_np_author_id  );  
        $np_causes_interests = get_field( 'np_causes_interests', 'user_'. $pc_np_author_id );
}

Open in new window

Now they're accessible as properties so you can echo them out:

$posts = new pc_npPosts();
echo $posts->pc_np_author_id;
echo $posts->$np_organization_logo;

Open in new window

Depending on your needs, there's probably a much more efficient way of doing this. As it stands, everytime you instantiate the class, you're call all those get_field() methods, whether you need them or not.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Tammu
Tammu

ASKER

Thank you for both replying. I changed my class according to what gr8gonzo said. I do have one question,

Is it possible to pass the author_id dynamically based on where the function is being called. What I mean is for example I am trying to display author details on a each post page of the author. So I would like to pass the post id of the author to get the details of the author

basically

$this->pc_np_author_id  needs to be changed dynamically

Thanks again
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Tammu
Tammu

ASKER

Thank you to both of you. This really helped me. Appreciate it
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo