Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-10-10 09:55:07

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,630
Website GitHub Twitter

[SOLVED] get the value of a custom field in a variable

I’m trying to get the value of a custom_field of the current article into a variable. The only way I can think of doing it does not work. can someone point to me what I am doing wrongly?

<txp:variable name="relatedtexts"><txp:article><txp:custom_field name="venue" /></txp:article></txp:variable>

Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#2 2016-10-10 10:25:43

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: [SOLVED] get the value of a custom field in a variable

colak wrote #302069:

Can someone point to me what I am doing wrongly?

<txp:variable name="relatedtexts"><txp:article><txp:custom_field name="venue" /></txp:article></txp:variable>...

It should work as far as you call <txp:variable name="relatedtexts" /> then. It does work for me.
No typo in your variable/field name?


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#3 2016-10-10 11:42:03

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,387
Website GitHub

Re: [SOLVED] get the value of a custom field in a variable

If the field you want is in the current article, can you not just use:

<txp:variable name="relatedtexts"><txp:custom_field name="venue" /></txp:variable>

inside your <txp:if_individual_article> page flow somewhere? Removes a layer of complexity.


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

Offline

#4 2016-10-10 11:57:04

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,630
Website GitHub Twitter

Re: [SOLVED] get the value of a custom field in a variable

Hi Nicolas

Thanks so much for testing it. I’m yet to get the expected results but I’m still trying. Needless to say that the above tags are part of a more complex query which I probably got something wrong somewhere in there.


Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#5 2016-10-21 15:42:20

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,630
Website GitHub Twitter

Re: [SOLVED] get the value of a custom field in a variable

Hi Stef… I did not notice your post here, my apologies. You obviously wrote it when I was writing mine.

This is the code which I am trying to get it to work but txp just ignores it:

<txp:variable name="relate"><txp:custom_field name="venue" /></txp:variable>
<txp:variable name="relatedtexts"><txp:article_custom venue='<txp:variable name="relate" />' limit="30" break="," wraptag=""><txp:if_article_id><txp:else /><txp:article_id /></txp:if_article_id></txp:article_custom></txp:variable>
<txp:variable name="moretexts"><txp:article_custom limit="11" section="texts" break="," sort="rand()"><txp:if_article_id><txp:else /><txp:article_id /></txp:if_article_id></txp:article_custom></txp:variable>

<txp:if_variable name="relatedtexts" value="">
<txp:article_custom id='<txp:variable name="moretexts" />' limit="10" break="li" wraptag="ul" class="related_articles">
<txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>
<txp:else />
<txp:article_custom wraptag="ul" break="li" limit="10" class="related_articles" id='<txp:rah_repeat offset="1" break="," duplicates="1" sort="regular asc" value=''<txp:variable name="relatedtexts"/>,<txp:variable name="moretexts" />''> <txp:rah_repeat_value /> </txp:rah_repeat>' sort='field(ID,<txp:variable name="relatedtexts" />,<txp:variable name="moretexts" />)'><a href="<txp:permlink />" rel="canonical"><txp:title /></a> /></txp:article_custom>
</txp:if_variable>

basically I am trying to show a list of texts in the sidebar by the same author (most of them without any privileges to the site) and augment that list with other texts. For the authors I use the id of the entries created in the links tab and I unfortunately use the venue custom_field to call their names.

Maybe the issue is similar to Alesh’s here. I know the code above is complex but I can not think of another way of doing it. To make the issue more complex there are times that there are two authors who are called by their ids and a comma.

The rah_repeat plugin helps to find and delete duplicates in the resulting list of ids.
The whole of the code resides in an individual_article context.

> Edit: I noticed a mistake. Corrected it above, tested it and still, it is just showing linked titles of random texts.

Last edited by colak (2016-10-21 16:30:27)


Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#6 2016-10-21 20:05:50

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,153
Website

Re: [SOLVED] get the value of a custom field in a variable

I don’t think it is the culprit, but If I’m not mistaken you have an extra /> just before the final </txp:article_custom>.


TXP Builders – finely-crafted code, design and txp

Offline

#7 2016-10-21 20:43:58

etc
Developer
Registered: 2010-11-11
Posts: 4,331
Website GitHub

Re: [SOLVED] get the value of a custom field in a variable

You can simplify it a bit, since 4.6 has some new features:

<txp:variable name="relatedtexts"><txp:related_articles match="venue"  limit="30" break="," wraptag=""><txp:article_id /></txp:article_custom></txp:variable>
<txp:variable name="moretexts"><txp:article_custom limit="11" section="texts" break="," sort="rand()" exclude='<txp:article_id />'><txp:article_id /></txp:article_custom></txp:variable>

<txp:if_variable name="relatedtexts" value="">
<txp:article_custom id='<txp:variable name="moretexts" />' limit="10" break="li" wraptag="ul" class="related_articles">
<txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>
<txp:else />
<txp:article_custom wraptag="ul" break="li" limit="10" class="related_articles" id='<txp:variable name="relatedtexts"/>,<txp:variable name="moretexts" />'>
<a href="<txp:permlink />" rel="canonical"><txp:title /></a>
</txp:article_custom>
</txp:if_variable>

I don’t say it works, though. :-\

Edit: no need for rah_repeat.

Last edited by etc (2016-10-21 20:59:05)

Offline

#8 2016-10-22 05:34:28

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,630
Website GitHub Twitter

Re: [SOLVED] get the value of a custom field in a variable

Hi Julian, and Oleg

Thanks for the input.

I like Oleg’s solution but there is something I do not understand in it.

The relatedtexts variable works mostly fine in my limited test but how do we know that the moretexts variable will not select articles matching the venue variable of the article?

The relatedtexts variable does not work when there are two authors. ie, when there are two numbers such as 14,34. In such case it should be checking for either number to match.


Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#9 2016-10-22 09:24:37

etc
Developer
Registered: 2010-11-11
Posts: 4,331
Website GitHub

Re: [SOLVED] get the value of a custom field in a variable

colak wrote #302381:

Hi Julian, and Oleg

Hi Yiannis

The relatedtexts variable works mostly fine in my limited test but how do we know that the moretexts variable will not select articles matching the venue variable of the article?

It will, but in 4.6 all duplicates are removed from id attribute, so you will get every article only once, starting with <txp:variable name="relatedtexts" />. I suspect that was already the case in 4.5 (for other reasons, though).

The relatedtexts variable does not work when there are two authors. ie, when there are two numbers such as 14,34. In such case it should be checking for either number to match.

Presently you can not do it with custom fields in core, but this should work (replace custom_1 with your venue custom field number):

<txp:etc_query name="find" data='<txp:custom_field name="venue" />' markup="list" break=" OR " >FIND_IN_SET({?}, custom_1)</txp:etc_query>
<txp:if_variable name="find">
	<txp:etc_query name="relatedtexts" data='(<txp:variable name="find" />) AND ID != <txp:article_id />' markup="db" populate="article" break=","><txp:article_id /></txp:etc_query>
<txp:else />
	<txp:variable name="relatedtexts" value="" />
</txp:if_variable>

Now <txp:variable name="relatedtexts" /> contains the ids of all other articles having an author in common with the current one.

Last edited by etc (2016-10-22 09:47:31)

Offline

#10 2016-10-22 10:23:49

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,630
Website GitHub Twitter

Re: [SOLVED] get the value of a custom field in a variable

That looks cool and I’ll try it when I solve this head scratching problem.


Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#11 2016-10-22 17:30:20

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,630
Website GitHub Twitter

Re: [SOLVED] get the value of a custom field in a variable

I’m still working on this:)

<div class="sub">
<txp:etc_query name="find" data='<txp:custom_field name="venue" />' markup="list" break=" OR " >FIND_IN_SET({?}, custom_3)</txp:etc_query>
<txp:variable name="moretexts"><txp:article_custom limit="20" section="texts" break="," sort="rand()" exclude='<txp:article_id />'><txp:article_id /></txp:article_custom></txp:variable>
<txp:if_variable name="find" value="">
<txp:article_custom section="texts" sort="rand()" limit="20" break="li" wraptag="ul" class="submenu" label="Other Texts" labeltag="h4" id='<txp:variable name="moretexts" />' exclude='<txp:article_id />'> 
<a rel="canonical" href="<txp:permlink />"><txp:title /></a>
</txp:article_custom>
<txp:else />
<txp:etc_query name="relatedtexts" data='(<txp:variable name="find" />) AND ID != <txp:article_id />' markup="db" populate="article" break=","><txp:article_id /></txp:etc_query>
<txp:article_custom id='<txp:variable name="relatedtexts" />' limit="20" break="li" wraptag="ul" class="related_articles" label="More texts by the author" labeltag="h4">
<a rel="canonical" href="<txp:permlink />"><txp:title /></a>
</txp:article_custom>
<txp:article_custom sort="rand()" section="texts" exclude='<txp:article_id />,<txp:variable name="find" />' limit="15" break="li" wraptag="ul" class="submenu" label="Other Texts" labeltag="h4"> 
<a rel="canonical" href="<txp:permlink />"><txp:title /></a>
</txp:article_custom>
</txp:if_variable>
</div>

the above mostly works as expected and differently to my initial question in this thread.

The problem: When an author has more texts all appears as expected but when we only have one text, a random list from all sections is populated.. Is there something wrong with my conditionals in there?


Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#12 2016-10-22 17:47:36

etc
Developer
Registered: 2010-11-11
Posts: 4,331
Website GitHub

Re: [SOLVED] get the value of a custom field in a variable

The condition <txp:if_variable name="find" value=""> is never met. This variable

  • either contains a db query like FIND_IN_SET(2, custom_3) OR FIND_IN_SET(5, custom_3) if current venue is 2,5;
  • or is not set if this field is empty (i.e. the current article has no declared authors).

You should somehow check <txp:if_variable name="relatedtexts"> instead.

…to be continued…

Offline

Board footer

Powered by FluxBB