Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-06-05 11:32:17

Registered: 2022-05-05
Posts: 19

Internal links - how to automate

I have two types of links on a page: internal and external. I want them added by option > Content > Links.
The use of categories is possible but not necessary.

I want to insert links using a tag, e.g. <txp:link name=“link nternal 1”/>
Can the txp:link tag only use form: plainlinks, can you point to any one?
Because any: <txp:link form=“links_internal” name=“link nternal 1”/>, doesn’t work.
The second thing is that I would prefer to insert as little as possible and have everything written in forms.

For internal links, I would like to enter only: <txp:link name=“link internal 1”/> and that forms would recognize it best by address, if not by category, whether it’s an internal or external link.

For an internal link, I would like the form to add: rel=“dofollow” and title=“data from the Description field in menu > Content > Links”

How can this be done, whether under the conditions in the form after the address or category, or when entering a link to the article by indicating a different form?


#2 2023-06-06 05:09:32

Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,105

Re: Internal links - how to automate

I would use some shortcode for this

Create a form name internal link, named e.g. link_internal, with as contents:

<txp:linklist id='<txp:yield name="id" />'><a class="" href="<txp:link_url />" title="<txp:link_description escape="html" />"><txp:link_name /></a></txp:linklist>

In your article, you then insert the link, where the id attribute is the ID of your link as found on the links panel :

<txp::link_internal id="xx" />

You can create a similar form for your external links, adding rel="external" and target="_blank"

Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern


#3 2023-06-06 06:32:10

From: Leeds, UK
Registered: 2006-01-29
Posts: 11,299
Website GitHub

Re: Internal links - how to automate

Further to phiw13’s excellent suggestion, you can also expand the scope to use a single shortcode like this:

<txp:linklist id='<txp:yield name="id" />'>
   <a href="<txp:link_url />"<if::yield name="ext"> title="<link::description escape="html" />" rel="dofollow"</if::yield>><link::name /></a>

That means you can specify <txp::link id="59" ext /> to render an external link, or omit the ext to render an internal link.

But you can go a step further and have the shortcode do that automatically based on URL:

<txp:linklist id='<txp:yield name="id" />'>
   <txp:variable name="mylink"><link::url /></txp:variable>
   <txp:variable name="is_ext" trim>
   <if::variable name="mylink" match="pattern" value="https://example.com/.*">NO<txp:else />YES</if::variable>
   <a href="<link::url />"<if::variable name="is_ext" value="YES"> title="<link::description escape="html" />" rel="dofollow"</if::variable>><link::name /></a>

If your link begins with https://example.com then it renders it as an internal link, otherwise it’ll add the title and rel.

astick360 wrote #335473:

I want to insert links using a tag, e.g. <txp:link name=“link nternal 1”/>

Sadly, the current links implementation is… inconsistent.

  1. If you use the <txp:link /> tag then you can specify a name or id, but it outputs a hard-coded anchor tag.
  2. If you specify <txp:linklist> then you can only use an id, but you can customise its container contents (e.g. as above in the shortcode) or via the form attribute.
  3. The <txp:linkdesctitle /> tag outputs the description as title with optional rel and hard-codes its output.

It’s a bit of a mess, sorry. Needs an overhaul, ideally.

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp


#4 2023-06-06 07:03:51

Registered: 2022-05-05
Posts: 19

Re: Internal links - how to automate

Thanks for the info, both solve the problem and that’s cool. Thanks again.


Board footer

Powered by FluxBB