Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Read content -> look for article with same name - link
Has someone tried to do the following:
Automatically read content from a custom field (or as well article body),
process for terms, which have a respective article in database under same name,
then link those terms automatically to the respective article, when displaying the content of the custom field (or article body), which contains it.
Any hints appreciated.
A hole turned upside down is a dome, when there’s also gravity.
Offline
Re: Read content -> look for article with same name - link
That’s not too difficult, but can become time-expensive on huge text blocks (article body). You just need to create Title => link
replacements pairs with <txp:article_custom />
and pass them to a replacement plugin.
Offline
Re: Read content -> look for article with same name - link
Thank you, etc. What kind of replacement plugin would that be, rah_replace? I looked at your link, but it seemed, it would only work in 4.7? i am on 4.6.2, waiting for the official release
A hole turned upside down is a dome, when there’s also gravity.
Offline
Re: Read content -> look for article with same name - link
The only 4.7 feature my example uses is JSON encoding. You can replace this line:
"<txp:title escape="json" />" : "<txp:permlink escape="json"><txp:title /></txp:permlink>"
with
<txp:php>
echo json_encode(parse('<txp:title />')), ' : ', json_encode(parse('<txp:permlink><txp:title /></txp:permlink>'));
</txp:php>
I wouldn’t recommend using straightforward replacement with rah_replace
or other plugin, since it would link also article titles inside links, producing
<a href="#"><a href="#">Title</a></a>
Offline
Re: Read content -> look for article with same name - link
So i take it that you use etc_query for this.
I am trying to extract comma-separated terms from a custom field and return them linked to the same field when displayed, but seem to be digging in the dark.
I use
<txp:if_custom_field name="works with">
<txp:variable name="replacements">{
<txp:article_custom break=",">
<txp:php>
echo json_encode(parse('<txp:custom_field name="works with" />')), ' : ', json_encode(parse('<txp:permlink><txp:title /></txp:permlink>'));
</txp:php>
</txp:article_custom>
}</txp:variable>
<txp:etc_query data='<txp:custom_field name="works with" />'
replace="//p/text()&={$json_decode({?replacements}|1).strtr({?}|$)}" />
</txp:if_custom_field>
and get no output at all.
Just using <txp:variable name="replacements" />
gives me a lot of output of all kinds of info stored in that custom field in various articles, but Oleg, i am an etc_query newbie.
EDIT: Links generated in txp:variable output look like https://website.net/%22https:////website.net//article-meta-title/%22
Can you give me a hint, please?
EDITED: Textile — uli
Last edited by uli (2018-04-05 10:17:51)
A hole turned upside down is a dome, when there’s also gravity.
Offline
Re: Read content -> look for article with same name - link
Uli?
A hole turned upside down is a dome, when there’s also gravity.
Offline
#7 2018-04-05 10:29:36
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,304
Re: Read content -> look for article with same name - link
Just sent you mail to explain. Sorry!
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: Read content -> look for article with same name - link
No worries, but I need your help.
jayrope wrote #310641:
I am trying to extract comma-separated terms from a custom field
what terms: article titles, their url titles, ..?
and return them linked to the same field when displayed
So, you want the “terms” to be replaced by article links?
Offline
Re: Read content -> look for article with same name - link
Oleg, exactly, extracting comma-separated article titles from a custom field, then replace the respective entries with links to the respective articles in the same field, only in frontend, so only for display (yet ;).
In fact really cool would be, if i get a live list of existing article titles, when starting to type terms into that custom field in the backend. But that’s version “future”.
Last edited by jayrope (2018-04-05 10:53:47)
A hole turned upside down is a dome, when there’s also gravity.
Offline
Re: Read content -> look for article with same name - link
Then yes, it could be rah_replace
, but this should work too:
<txp:if_custom_field name="works with">
<txp:variable name="replacements">{
<txp:article_custom break="," limit="999"><txp:php>
echo json_encode(parse('<txp:title />')).' : '.json_encode(parse('<txp:permlink><txp:title /></txp:permlink>'));
</txp:php></txp:article_custom>
}</txp:variable>
<txp:etc_query data='<txp:custom_field name="works with" />' markup="raw">
{$json_decode({?replacements}|1).strtr({?}|$)}
</txp:etc_query>
</txp:if_custom_field>
Not sure works with
is a good name choice, works_with
looks more robust.
Edit: rah_replace
version:
<txp:if_custom_field name="works with">
<txp:article_custom limit="999">
<txp:if_first_article>
<txp:variable name="titles" value='<txp:title />' />
<txp:variable name="links" value='<txp:permlink><txp:title /></txp:permlink>' />
<txp:else />
<txp:variable name="titles" value='<txp:variable name="titles" />,<txp:title />' />
<txp:variable name="links" value='<txp:variable name="links" />,<txp:permlink><txp:title /></txp:permlink>' />
</txp:if_first_article>
</txp:article_custom>
<txp:rah_replace from='<txp:variable name="titles" />' to='<txp:variable name="links" />'>
<txp:custom_field name="works with" />
</txp:rah_replace>
</txp:if_custom_field>
Offline
Re: Read content -> look for article with same name - link
I agree, works_with would be more robust, but that’s just how glz_cf names those custom fields eventually.
And thank you, I tried the etc_query code from you, it works, but not on article titles/terms, which consist of more than one word.
Last edited by jayrope (2018-04-05 14:20:49)
A hole turned upside down is a dome, when there’s also gravity.
Offline
Re: Read content -> look for article with same name - link
It works for any title for me, but make sure that they match exactly (e.g. upper/lower case). Output the variable(s) and the custom field and compare.
Offline