Jump to content

Get recent comments for sidebar


Beate
 Share

Recommended Posts

Hi Beate, Congrats on the migration also this is how it goes, First you need to go the your Fields and see the name of the fields that hold the comments, then all you need to do is to 

$fields->find("comment_field_selector,sort=date,limit=AMOUNT_OF_COMMENT");

 

  • Like 2
Link to comment
Share on other sites

Thanks! Never thought this could work. But: are you sure about the syntax?

$letzte_kommentare = $fields->find("kommentare,sort=created,limit=5");

doesn't work,

Fatal error: Exception: Unknown Selector operator: '' -- was your selector value properly escaped? field='kommentare', value='', selector: 'kommentare,sort=created,limit=5' (in /var/www/vhosts/path_to_pw/wire/core/Selectors.php line 395)

(the name of the date is created, looked it up in the database.

Link to comment
Share on other sites

Maybe I am confused here, but I think you want the following - you have to get the comments from a given page - obviously in this case, the current one ($page)

$latest_comments = $page->comments->find("limit=5, sort=-created");
echo $latest_comments->render();

or translated to:

$letzte_kommentare = $page->kommentare->find("limit=5, sort=-created");
echo $letzte_kommentare->render();

 

Link to comment
Share on other sites

1 minute ago, adrian said:

you have to get the comments from a given page

Well, you don't have to. I assumed this was for some feature on the Home page where the latest comments (sitewide) are displayed. I have something like this on one of my sites, although doing a custom render that links each comment to the page it appears on.

  • Like 1
Link to comment
Share on other sites

3 minutes ago, Robin S said:

Well, you don't have to. I assumed this was for some feature on the Home page where the latest comments (sitewide) are displayed.

Of course, I wasn't thinking - I have done the same thing myself on occasion :)

Link to comment
Share on other sites

18 hours ago, Robin S said:

Try this:


$field = $fields->get('comments'); // assumes your comments field is named 'comments'
$latest_comments = $field->type->find($field, "limit=5, sort=-created");
echo $latest_comments->render();

 

Yeah! That's it :) Thanks to all.

@Adrian: yes, I wanted all recent comments, not the one from one single entry.

Link to comment
Share on other sites

  • 5 months later...

Hello all. I am trying to imlement the comments on my main page as well, however my need is just to print the page name, the author and the date of the last N-comments.

The examples above are meant to render the comments itself, where I am trying to pull up the pages ccontaining those comments and get them ordered. So I thought I could try something like this:

$com = $pages->find('template=articles_inner, comments>0, sort=comments->created'); or 

$com = $pages->find('template=articles_inner, comments!="", sort=-comments->created');

but it just shows no results.

My template name is "articles-inner" which implies for all child pages added as articles so the template name is correct. The field name that holds the comments is: "comments" so that is also correct, but what got me was that I am not aware how to check if the field contains some value (not empty) and also how to sort the resulted pages by comments post date.

Any ideas or suggestions?

 

Link to comment
Share on other sites

@flydev Thank you very much for the link. I had to modify some parts for sure but it works now fine. Here is my code so far:

<section id="section-lastest-responses" class="section">
  <ul class="fa-ul">
    <? $comment_pages = $pages->find("template!=news-inner, sort=-comments.created, limit=4");

	foreach($comment_pages as $p) {   
	$comments = $p->comments->find("created>=0, sort=-created, status=1, limit=1");

		foreach($comments as $c) { ?>
			<li>
			  <i class="fa-li fa fa-list-alt fa-fw text-muted"></i>
			  <h3 class="h5"><a href="<?=$c->url?>"><?=$p->title?></a></h3>
			  <small class="meta text-muted">
				<span class="time"><i class="fa fa-clock-o fa-fw"></i> <?php echo wireRelativeTimeStr($c->created)?></span>
				<span class="category"><i class="fa fa-folder-open-o fa-fw"></i> <a href="#"><?=$p->parent->title?></a></span>
			  </small>
			</li>
		<? } 
	} ?>
  </ul>
</section>

Now to make it more fun and learn something extra, I decided to give the users a chance to click on the page title which would lead them straight to the comment. To achieve that, I changed the articles-inner template and added the comment id to every listed one. Then I am hyperlinking the page title:

<a href="<?=$page->url . '#' . $c->id?>"> and it shows the link and the ID correctly, however the link has a backslash and it messes up the anchor (eg. http://domain.com/article/#id3)

Am I missing something simple again in the big  picture? 

Link to comment
Share on other sites

So the example of Ryan worked but there are still some discrepancies to fix:

1. The pages query selects the 4 pages containing latest comments, but if a page has more than 1 comment, than this is where it gets confusing.

As far as the comments query would repeat for every page that has comments, unless I select to show 1 comment per page (limit=1) I can't show just the 4 latest (e.g. if 4 pages hold 2 comments each, even though the limit in the comments query is 4, it will give 8 results).

2. For some strange reason, $cite does not show the user who have posted the comment but just an empty string. I tried to use $c->cite but that is empty too. Went to check in the field parameters for any reason why it would not show the author, but did not find anything.

3. While listing the comments in articles-inner as far as I use the API to show the results but not rendering those, how would I add the comments rating?

So the fun part continues and was too early to celebrate :)

Link to comment
Share on other sites

I had some progress in showing the proper comments topics and listing their titles promptly. To achieve that, I had to change the approach, so instead of first finding the pages that has some comments and then select the comments listed in them and limitting, I took the reverse approach by first searching for the comments and then match the pages whose id match with the comments id. Here is the example that worked like a charm and now I see all comments promptly even if they are posted in the same page or in a few repetetive ones:

Spoiler

<section id="section-lastest-responses" class="section">
	<ul class="fa-ul">

		<? 
		// Assigning $field to simplify the query
		$field = $fields->get('comments'); 

		// Find the latest comments limitting the output to 4, sort by add date and show only approved
		$latest_comments = $field->type->find($field, "limit=4, sort=-created, status=1");

		// Counting the number of comments and assigning them to $total variable
		$total = count($latest_comments); 

		// Checking if $total is not empty
		if(!$total) continue;

		// Assigning the results of the array to $lc
		foreach($latest_comments as $lc) {

			//This was supposed to pull up the author but not working???
			$cite = htmlentities($lc->cite);
			
			// $text would hold the comment text if a need be to show it
			$text = nl2br(htmlentities($lc->text, ENT_QUOTES, "UTF-8")); 
			
			// $created is setting the format of comments date&time stamp
			$created = date('F j, Y H:i a', $lc->created);

			// Find the pages that hold the comments and sort by the date of comments adding
			$commentPages = $pages->find("template=articles-inner, comments.id=$lc->id"); 


			// loop through the pages that have new comments
			foreach($commentPages as $p) { ?>

					<li>
						<i class="fa-li fa fa-list-alt fa-fw text-muted"></i>
						<h3 class="h5">
							<a href="<?=$p->url . '#comment-' . $lc->id ?>"><?=$p->title?></a>
						</h3>
						<small class="meta text-muted">
							<span class="time"><i class="fa fa-clock-o fa-fw"></i> 
								<?php echo wireRelativeTimeStr($lc->created)?>
							</span>
							<span class="time">
								<i class="fa fa-user fa-fw"></i> <?=$lc->cite ?> 
							</span>
							<span class="category"><i class="fa fa-folder-open-o fa-fw"></i> 
								<a href="#"><?=$p->parent->title?></a>
							</span>
						</small>
					</li>
		<? } 

		} ?>

	</ul>
</section>

 

 

Now I need to figure out why on Earth the $cite returns empty string instead of showing the comments author and it would all be sorted. Special thanks to @Robin S and @adrian for sharing a way to query fields instead of pages. I should read a bit more about that ...

 

Link to comment
Share on other sites

Ok, I got all that was needed for my design thanks to @fbg13 code for obtaining the user via the author id. I am attaching my complete code that shows the topic where the latest X-comments were added, shows the date of adding (based on the time format), shows the author of the comment and in my scenario shows the parent of the page (which in my case was used as category). On top of that it contains the anchors to the original comment so if clicked it would go straight to the content of the comment. To have the anchoring working, you need to assign the comment-## to the form (where ## is the ID of the comment).

Spoiler

<section id="section-lastest-responses" class="section">
    <ul class="fa-ul">

        <? 
		// Assigning $field to simplify the query
		$field = $fields->get('comments'); 

		// Find the latest comments limitting the output to 4, sort by add date and show only approved
		$latest_comments = $field->type->find($field, "limit=4, sort=-created, status=1");

		// Counting the number of comments and assigning them to $total variable
		$total = count($latest_comments); 

		// Checking if $total is not empty
		if(!$total) continue;

		// Assigning the results of the array to $lc
		foreach($latest_comments as $lc) {

			//The $cite populates the username in the DB>field.comments>cite
			$cite = htmlentities($lc->cite); // prep what we need for output

			// $text would hold the comment text if a need be to show it
			$text = nl2br(htmlentities($lc->text, ENT_QUOTES, "UTF-8")); 

			// $created is setting the format of comments date&time stamp
			$created = date('F j, Y H:i a', $lc->created);

			// Find the pages that hold the comments and sort by the date of comments adding
			$commentPages = $pages->find("template=articles-inner, comments.id=$lc->id"); 

			// loop through the pages that have new comments
			foreach($commentPages as $p) { ?>

            <li>
                <i class="fa-li fa fa-list-alt fa-fw text-muted"></i>
                <h3 class="h5">
					<a href="<?=$p->url . '#comment-' . $lc->id ?>"><?=$p->title?></a>
				</h3>
                <small class="meta text-muted">
					<span class="time">
						<i class="fa fa-user fa-fw"></i> <?=$cite?> 
					</span>																
					<span class="time"><i class="fa fa-clock-o fa-fw"></i> 
						<?php echo wireRelativeTimeStr($lc->created)?>
					</span>
					<span class="category"><i class="fa fa-folder-open-o fa-fw"></i> 
						<a href="#"><?=$p->parent->title?></a>
					</span>
				</small>
            </li>
            <? } 

		} ?>

    </ul>
</section>

 

Thanks again to all who kindly shared their code that helped me get here.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...