Output page field > page URL


#1

Hi, i’m struggling trying to output the page url from a page field, it’s from a structure field on the site wide template, im trying to display CTA button in the navbar from the menu snippet, and the bit of code to output the “ctalink” page field looks like that :

<?php echo $navcta["ctalink"]; ?>

if i select a page from the page field, for example projects/project-a, it outputs “projects/project-a” in the HTML, and the website seems to add the "localhost/website/, but the link is only valid from the homepage as its seems to add projects/project-a ton whatever URL it has been clicked…

How can i simply output the absolute URL of my internal page?


#2

Can you share more of your code and the blue print? Where is the data stored? How are you setting $navcta.

I asked a similar question a couple of years ago, maybe this thread holds the answer… Specific entry in a structure field

It might be over kill for what you want but i do CTA links via a kirby route to get a list of every page on the site, and using that as an option list on a select field. This saves you having to store the links in a structure feild, and is completly dynamic.

The route will look something like this (im using AutoID but should be easy enough to switch this to UID or something):

  // Page Links
  array(
    'pattern' => 'pagelist.json',
    'action'  => function() {
      header('Content-type: application/json; charset=utf-8');
      $pages = site()->index()->visible()->filterBy('template', 'not in', ['error', 'blogarticle']);
      $json = array();

      foreach ($pages as $page) {
          $json[$page->autoid()->value()] = $page->title()->value();
      }

      echo json_encode($json);
    }
  ),

You can then pump that list into a Select field:

  ctalinkurl:
      label:          Button Link
      type:           select
      options:        url
      url:            pagelist.json
      width:          1/2
      help:           The button link value

And get it back with (assuming your data is stored on the site page):

<?php
$ctalinkurls = $site->ctalinkurl();
$ctalink = $site->url(). '/' .$site->index()->filterBy('autoid', '==', $ctalinkurls);
?>

<a href="<?= $ctalink ?>">

#3

Hey, to be more specific, id like to output in my template the full URL from a page field.


#4

Ah, i see… when you said page field, it sounded ambigious - could have meant any field on the page :slight_smile:

Ive never used that field, but what happens if you do:

<?php echo $navcta["ctalink"]->value(); ?>

Im assuming your doing ->yaml somewhere on the stucture field and setting it in $navcta?


#5

To get the URL, it should be

<?php
if($p = page($navcta["ctalink"]) {
   echo $p->url();
}

#6

Hi, your example broken my code completely, i feel like there is a part of the code missing. Also, why a if?

here is the full structure code snippet i feel it will be needed to resolve this

https://pastebin.com/aZ067kAt

all im looking for is to have the full absolute url of the selected page from the panel page field. not just /pagename/


#7

What do you mean with broken code, what is the exact error message?

The if statement is necessary, because you can’t rely on the page to actually exist.

Assuming that $navcta["ctalink"] returns the path to the page,

page($navcta["ctalink"])

should give. you a page object.

By the way, you can make your life easier if you use toStructure() instead of yaml to create a structure object instead of an array.

Should I guess go in here:

href="<?= $p = page($navcta["ctalink"])? $p->url():''; ?>

#8

@jimbobrjames Just FYI, this code would throw an error. value()is a field method, $navcta["ctalink"] is a string, not a field.


#9

Hi,

Your code worked once i used “tostructure()”, thanks a lot !