Jump to content

Error: Using $this when not in object context


chaseman

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.