I want to display some random page links, I was unable to find something that would help me with it so I decided to give it a go in creating my own plugin.
The idea is to add a <MTRandomPages> block that will allow me to display the random pages in my templates. The function I have is:
CODE
sub RandomPagesLoop{
my ($ctx, $args, $cond) = @_;
my $folder = $args->{folder} || $args->{folders};
$args->{categories} = $folder if $folder;
if($args->{no_folder}) {
require MT::Folder;
my $blog_id = $ctx->stash('blog_id');
my @fols = MT::Folder->load({blog_id => $blog_id});
my $not_folder;
foreach my $folder (@fols) {
if ($not_folder) {
$not_folder .= " OR ".$folder->label;
} else {
$not_folder = $folder->label;
}
}
if ($not_folder) {
$args->{categories} = "NOT ($not_folder)";
}
}
require MT::Page;
$args->{class_type} = MT::Page->properties->{class_type};
my $class = MT->model( $args->{class_type} );
my $blog_id = $ctx->stash('blog_id');
my $blog = $ctx->stash('blog');
my (@filters, %blog_terms, %blog_args, %terms, %args);
$ctx->set_blog_load_context($args, \%blog_terms, \%blog_args)
or return $ctx->error($ctx->errstr);
%terms = %blog_terms;
%args = %blog_args;
my @pages = $class->load(\%terms, \%args);
srand;
my $builder = $ctx->stash('builder');
my $tok = $ctx->stash('tokens');
my %usedPages;
my $res;
my $limit = $args->{limit} || 1;
$limit = @pages if ($limit > @pages);
while($limit > 0){
my $randPage = $pages[ rand @pages ];
while ($usedPages{$randPage->id}) {
$randPage = $pages[ rand @pages ];
}
$usedPages{$randPage->id}++;
local $ctx->{__stash}{blog} = $randPage->blog;
local $ctx->{__stash}{blog_id} = $randPage->blog_id;
local $ctx->{__stash}{entry} = $randPage;
local $ctx->{current_timestamp} = $randPage->authored_on;
local $ctx->{modification_timestamp} = $randPage->modified_on;
my $out = $builder->build($ctx, $tok, { %$cond });
return $ctx->error( $builder->errstr ) unless defined $out;
$res .= $out;
$limit--;
}
$res;
} #sub RandomPagesLoop
my ($ctx, $args, $cond) = @_;
my $folder = $args->{folder} || $args->{folders};
$args->{categories} = $folder if $folder;
if($args->{no_folder}) {
require MT::Folder;
my $blog_id = $ctx->stash('blog_id');
my @fols = MT::Folder->load({blog_id => $blog_id});
my $not_folder;
foreach my $folder (@fols) {
if ($not_folder) {
$not_folder .= " OR ".$folder->label;
} else {
$not_folder = $folder->label;
}
}
if ($not_folder) {
$args->{categories} = "NOT ($not_folder)";
}
}
require MT::Page;
$args->{class_type} = MT::Page->properties->{class_type};
my $class = MT->model( $args->{class_type} );
my $blog_id = $ctx->stash('blog_id');
my $blog = $ctx->stash('blog');
my (@filters, %blog_terms, %blog_args, %terms, %args);
$ctx->set_blog_load_context($args, \%blog_terms, \%blog_args)
or return $ctx->error($ctx->errstr);
%terms = %blog_terms;
%args = %blog_args;
my @pages = $class->load(\%terms, \%args);
srand;
my $builder = $ctx->stash('builder');
my $tok = $ctx->stash('tokens');
my %usedPages;
my $res;
my $limit = $args->{limit} || 1;
$limit = @pages if ($limit > @pages);
while($limit > 0){
my $randPage = $pages[ rand @pages ];
while ($usedPages{$randPage->id}) {
$randPage = $pages[ rand @pages ];
}
$usedPages{$randPage->id}++;
local $ctx->{__stash}{blog} = $randPage->blog;
local $ctx->{__stash}{blog_id} = $randPage->blog_id;
local $ctx->{__stash}{entry} = $randPage;
local $ctx->{current_timestamp} = $randPage->authored_on;
local $ctx->{modification_timestamp} = $randPage->modified_on;
my $out = $builder->build($ctx, $tok, { %$cond });
return $ctx->error( $builder->errstr ) unless defined $out;
$res .= $out;
$limit--;
}
$res;
} #sub RandomPagesLoop
In my template I have:
CODE
<mt:RandomPages limit="5">
<li><h4><a href="<$mt:PagePermalink$>"><$mt:PageTitle$></a></h4> <$mt:PageExcerpt$></li>
</mt:RandomPages>
<li><h4><a href="<$mt:PagePermalink$>"><$mt:PageTitle$></a></h4> <$mt:PageExcerpt$></li>
</mt:RandomPages>
But it doesn't display anything and the plugin doesn't throw any errors when publishing.
Any help or advice will be greatly appreciated.
Thanks
