chaseman Posted April 10, 2011 Share Posted April 10, 2011 I'm trying to build a Wordpress plugin where all the archives are nicely listed in a tabular form. I'm reading the book Wordpress Plugin Development - A Beginner's Guide And I'm having an error when trying to call the function to display the archives on a separate page. Here's the function: function display() { global $wpdb; // these variables store the current year, month and date // processed $curyear=''; $curmonth=''; $curday=''; // the beginning of our output $result=' <div class="snazzy"> <table cellspacing="15" cellpadding="0" border="0"> <tbody> <tr>'; // query to get all published posts $query= "SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_password='' ORDER BY post_date_gmt DESC "; $posts = $wpdb->get_results($query); foreach ($posts as $post) { // retrieve post information we need $title = $post->post_title; $excerpt= $this->get_excerpt($post->post_content); $url=get_permalink($post->ID); $date = strtotime($post->post_date); // format the date $day = date('d', $date); $month = date('M', $date); $year = date('Y', $date); // look for image in the post content $imageurl=""; preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*) /i' , $post->post_content, $matches); $imageurl=$matches[1]; // get comments for this post $comcount = $wpdb->get_var(" SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_post_ID=$post->ID AND NOT (comment_type = 'pingback' OR comment_type = 'trackback')"); // additional formatiing if ($year!=$curyear) { // close the previous day/month if ($curday) $result.="</div></div></td>"; $curday=''; $curmonth=''; // year start in a new column (<td>) $result.= '<td valign="top"><div class="sz_date_yr">' .$year.'</div><div class="sz_cont">'; $result.= '</div></td>'; $curyear=$year; } if ($month!=$curmonth) { // close the previous day/month if ($curday) $result.="</div></div></td>"; $curday=''; // month starts in a new column (<td>) $result.= '<td valign="top"><div class="sz_date_mon">' .$month.'</div><div class="sz_month">'; $curmonth=$month; } if ($day!=$curday) { // close previous day if ($curday) $result.="</div>"; $result.= '<div class="sz_date_day">'.$day.' </div><div class="sz_day">'; $curday=$day; } // retrieve the archive entry representation ob_start(); include('snazzy-layout-1.php'); $output = ob_get_contents(); ob_end_clean(); $result.=$output; } // close the previous day/month if ($curday) $result.="</div></div></td>"; // close the main page elements $result.="</tr></tbody></table></div>"; // return the result return $result; } This is the part to which the error message is referring to: // retrieve post information we need $title = $post->post_title; $excerpt= $this->get_excerpt($post->post_content); $url=get_permalink($post->ID); $date = strtotime($post->post_date); // format the date $day = date('d', $date); $month = date('M', $date); $year = date('Y', $date); [/code] The error message says that I'm using $this when not in "object context". The part where $this is used is needed to display an excerpt of the post content. This is the same way how it's shown in the book, for some reason it doesn't work for me. What would be an alternative way that I could do to make it work, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/233286-error-using-this-when-not-in-object-context/ Share on other sites More sharing options...
KevinM1 Posted April 10, 2011 Share Posted April 10, 2011 The error is clear in what it's saying. The 'this' keyword can only be used in an object context, which means only when you're writing a class definition. If you're not writing code which is part of a class, you can't use 'this'. Quote Link to comment https://forums.phpfreaks.com/topic/233286-error-using-this-when-not-in-object-context/#findComment-1199723 Share on other sites More sharing options...
chaseman Posted April 10, 2011 Author Share Posted April 10, 2011 Ok I just removed $this-> and it's working now. The $this was unnecessary, I don't know why the author put it there. Quote Link to comment https://forums.phpfreaks.com/topic/233286-error-using-this-when-not-in-object-context/#findComment-1199727 Share on other sites More sharing options...
chaseman Posted April 10, 2011 Author Share Posted April 10, 2011 I can not find $this in the PHP documentation, is there any resource I can learn more about it? Quote Link to comment https://forums.phpfreaks.com/topic/233286-error-using-this-when-not-in-object-context/#findComment-1199731 Share on other sites More sharing options...
dcro2 Posted April 10, 2011 Share Posted April 10, 2011 Try the manual page for classes: http://us3.php.net/manual/en/language.oop5.basic.php Quote Link to comment https://forums.phpfreaks.com/topic/233286-error-using-this-when-not-in-object-context/#findComment-1199733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.