Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-10-30 12:41:10

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

accessing custom_field content via global variables

hello all,

i found a really nice overview on accessing textpattern’s global variables (edit: in g1.19 ) here, it does not answer a question i have now, though:

how would one reference the content of single custom fields via textpattern’s global variable $thisarticle[‘thisid’] ?
i can’t find entries for custom fields in the list of attributes of $thisarticle.

or is that the wrong way to go?
how would i generally reference the content of custom fields in conjunction to a certain article other than using the txp built-in tags?

i need this info to be able to construct an order form drawing values from custom fields of individually displayed articles to be posted into a php sessionID based custom shopping cart.

thanx mucho in advance!

tooot!

Last edited by jayrope (2006-10-31 18:59:05)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#2 2006-10-30 13:45:48

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: accessing custom_field content via global variables

answering my own question:

an example for 4.0.4:
if custom_field 1 has the name “item_name”, then its value can be accessed globally with

$thisarticle[‘item_name’];

great!
i DON’T KNOW if that works in earlier versions of txp accordingly, though.


A hole turned upside down is a dome, when there’s also gravity.

Offline

#3 2006-10-30 13:46:05

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: accessing custom_field content via global variables

$thisarticle does contain the custom fields, but it uses the labels you’ve given them. Below two ways to acces the contents of the first custom field:

<txp:php>
  global $prefs, $thisarticle;
  return $thisarticle[$prefs['custom_1_set']]; # by id
</txp:php>
<txp:php>
  global $thisarticle;
  return $thisarticle['the_label_you_gave_to_the_custom1_field']; # by label
</txp:php>

Last edited by ruud (2006-10-30 13:53:52)

Offline

#4 2006-10-30 14:37:42

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: accessing custom_field content via global variables

ruud, thanx very much.

do i really need the “global” lines in the script?

here it works without these lines – and as i outlined above – at least when i am displaying a single article only.

Last edited by jayrope (2006-10-30 14:38:46)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#5 2006-10-30 15:47:32

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: accessing custom_field content via global variables

If it works, then it works ;)
When I use this inside a plugin function, then I need the global lines. Perhaps it’s not needed when used in a txp:php tag. Doesn’t hurt to add them.

Offline

#6 2006-10-30 16:41:52

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: accessing custom_field content via global variables

ruud wrote:

If it works, then it works ;)

Totally!
…was just trying to find out actually, if this way of doing it might suddenly stop working (f.i. when textpattern is being updated again), just because it might be too hacked or so…
in any case : muchas gracias.
toooot!


A hole turned upside down is a dome, when there’s also gravity.

Offline

#7 2006-11-22 11:58:20

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

Re: accessing custom_field content via global variables

I’m wondering about a behaviour of $thisarticle[index]:

When I refer to a customfield using a variable, eg. a variable $cfield defined by an attribute
and try to extract the fields content by

//Check for custom field entry.
if (trim($thisarticle[$cfield]) != '') {
$linktext = $thisarticle[$cfield];
}

the quotes obviously are not needed (it works).

But it is not possible to refer to the articles other fields (e.g. title).

If surrounded by quotes:

//Check for custom field entry.
if (trim($thisarticle["$cfield"]) != '') {
$linktext = $thisarticle["$cfield"];
}

customfields as well as normal fields work.

This seems totally illogical to me.

Any php-expert who could explain this to me?

Normally I thought there ought to be quotes in the first example as well?
Are there mantraps, if changing the codes to the later?

Offline

#8 2006-11-22 13:01:34

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: accessing custom_field content via global variables

The first example should work. Can you show us a complete example in that coding style that doesn’t work?
The second example with the double-quoted variable is very ugly. The quotes serve no purpose there.

Offline

#9 2006-11-22 13:12:05

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

Re: accessing custom_field content via global variables

It’s used in my plugin for managing permlink within excerpts:
sac_link_in_excerpt
Directlink to the code as text

If I specify cfield="title" it doesn’t work with the article’s title. That’s what I don’t understand.
Of course it works if I specify an existing customfield.

With quotes it works with both.
Maybe I should use single quotes as in your examples?

EDIT: Ooops, it works! Stupid me. Yesterday something didn’t work. Sorry.

Last edited by saccade (2006-11-22 13:15:19)

Offline

Board footer

Powered by FluxBB