Jump to content

API for anchor


Manaus
 Share

Recommended Posts

Hello, is there a method for generating a link on providing the right selector? Something like

<?php echo $pages->get("/mypage/")->link ?>

generating 

<a href="/myurl/">mytitle</a>

Instead of writing the whole string

<?= "<a href='".$pages->get("/")->url."'>".$pages->get("/")->title."</a>" ?>

or 

<a href="<?= $pages->get('/')->url"><?= $pages->get("/")->title ?></a>

Thanks!

Link to comment
Share on other sites

Hi @Manaus

Why don't you use a function?

<?php namespace ProcessWire;

/*
 *
 * /site/templates/_func.php
 * use include_once in your code template
 */
	function my_render_link($selector) {
		$current = wire('pages')->get($selector);
		if($current->id){
			$out = "<a href='".$current->url."'>".$current->title."</a>";
		}else{
			$out = false;
		}
		return $out;
    }
?>

I haven't tested but should work.

  • Like 3
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...