Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Class - getting property of non-object & undefined Notices
    primarykey
    data
    text
    <p>*<strong><em>edit have fixed this code - see inline comments marked EDIT *</em></strong></p> <p>I'm getting a few PHP debug notices when using the following code in a wordpress theme. It's my first attempt at using a PHP class, and it does work ok, but the debug notices concern me. I am calling the method in my PHP template thus:</p> <p><code>&lt;?php $getjobmeta-&gt;job_type(); ?&gt;</code> &amp; <code>&lt;?php $getjobmeta-&gt;post_types(); ?&gt;</code></p> <p>The debug notices are as follows:</p> <p><code>Notice: Undefined variable: post</code></p> <p><code>Notice: Trying to get property of non-object</code></p> <p>From the research I have done this looks like an issue with the class sometimes returning a non object, so i tried wrapping the 'echo' in <code>isset</code> and <code>is_object</code> but I just can't fix those notices.</p> <p>Here is the simplified code. For the sake of my sanity I could really use some help.</p> <pre><code> // define the class class getJobMeta { var $jobmeta_echo; // set a class variable to store our echo public function job_type() { // define a class function and make it public global $post // EDIT IN FIX if ( 'post' == get_post_type() ) { $jobtype = get_the_term_list( $post-&gt;ID, 'job-type', '&lt;span itemprop="employmentType"&gt;', ', ', '&lt;/span&gt;', 0 ); // 0 at end of arg signifies that we don't want links outputted } else { return ''; } echo $jobtype.$this-&gt;jobmeta_echo; } // end function job_type // define post type public function post_types() { global $post // EDIT IN FIX if ('post' == get_post_type()) { $posttype = get_the_term_list( $post-&gt;ID, 'channel', '&lt;strong class="channel-links clearfix"&gt;', ', ', '&lt;/strong&gt;', 1); // 1 means output as link } elseif ( 'blog' == get_post_type() ) { $posttype = '&lt;a href="/blog/" class="post-type-label"&gt;Blog&lt;/a&gt;'; } elseif ( 'type2' == get_post_type() ) { $posttype = '&lt;a href="/type2/" class="post-type-label"&gt;Type2&lt;/a&gt;'; } else { $posttype = ''; } echo $posttype.$this-&gt;jobmeta_echo; // EDIT TYPO } // end function posttype } // end getJobMeta class // set the class into a variable $getjobmeta = new getJobMeta; </code></pre> <p>Thanks Ben</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload