Jump to content

Trying to understand $Page->links()


montero4
 Share

Recommended Posts

Hello,

I got this code working where I'm printing out the other pages linking to the current page:

$items = $page->links();

if (count($items)) {
    
    echo ("some title");

    foreach ($items as $item) {
        $out="";
        if ($item->blog_tags) {
            foreach ($item->blog_tags as $tag) {
                $out .=$tag->title.", ";
            }
        }
        $out = rtrim($out, ", ");
         echo "<p><a href='{$item->url}'>{$item->title}</a> (".$out.")<br>{$item->blog_summary}</a></p>";
    }
}

1) What I'm trying to understand is why $items=$page->links(); works but if I try to limit it to only looking at the blog_body field it doesn't work, i.e. $page->links($field='blog_body');

2) Also, is there a way to sort the $items result in descending order? Something like sort=-blog_date so that the blog_posts are shown in descending order? How would I do that?

Thank you in advance for any help.

Regards,

Jonah

Link to comment
Share on other sites

1 hour ago, montero4 said:

if I try to limit it to only looking at the blog_body field it doesn't work, i.e. $page->links($field='blog_body');

The $page->links() method takes two optional arguments:

NAME TYPE(S) DESCRIPTION
selector (optional) string, bool

Optional selector to filter by or boolean true for “include=all”. (default='')

field (optional) string, Field

Optionally limit results to specified field. (default=all applicable Textarea fields)

You are passing only one argument, so this is interpreted as the first argument. Passing "$field='blog_body'" doesn't make this become the second argument - your code is saying "pass the string 'blog_body' as the first argument and also assign the string 'blog_body' to a variable named $field".

If you want to specify the second argument (field) but not the first (selector) you need to pass a value for the first argument, and in this case you would pass the default value which is an empty string. So you would do this:

$items = $page->links('', 'blog_body');

 

1 hour ago, montero4 said:

Also, is there a way to sort the $items result in descending order? Something like sort=-blog_date so that the blog_posts are shown in descending order?

It would be reasonable to think that you could pass a sort as part of the selector argument, but actually this doesn't work because behind the scenes this method is ultimately just getting some page IDs and then loading data from the database using those IDs without any ORDER_BY clause.

So you'll need to sort the links after you get them:

$items = $page->links('', 'blog_body');
$items->sort('-blog_date');

 

  • Like 1
  • Thanks 1
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...