<p>You will need to access a few things. </p>

<ul>
<li>The url segment you need (for example the last segment)</li>
<li>The global set name</li>
<li>The field handle name</li>
</ul>

<p>You can use <a href="https://craftcms.com/docs/templating/craft.request#getLastSegment" rel="nofollow">getLastSegment</a> to access the last url segment.</p>

<p>Using the global set names above let's say I have a url like this:</p>

<pre><code>mysite.com/first

{% set segment = craft.request.getLastSegment() %}  // first
</code></pre>

<p>I know I've got a global set called <code>firstGlobalSet</code>, and now I've got <code>first</code>. We just need to put them together. You can use Craft's 
<a href="https://craftcms.com/docs/templating/craft.globals#getSetByHandle" rel="nofollow">getSetByHandle</a> method to access the global set via its handle.</p>

<pre><code>{% set global = craft.globals.getSetByHandle(segment ~ 'GlobalSet') %}
{{ global }} // firstGlobalSet
</code></pre>

<p>Now that we've got the correct global set, we can get to the field we need. In this case it's <code>myTextField</code>.</p>

<pre><code>{{ global.myTextField }}
</code></pre>

CC BY-SA 3.0
2016-02-18T19:04:39.993Z
2016-02-18T19:04:39.993Z
Damon
<p>I have a rich text field with a handle of <code>myTextField</code> that I have included in a global set called <code>firstGlobalSet</code>.</p>

<p>What If I created another global set called <code>secondGlobalSet</code> and included the same field <code>myTextField</code>. </p>

<p>How could I get the correct field from the correct global set based on url segment?</p>

<p>For example, <code>mysite.com/first</code> or <code>mysite.com/second</code> </p>

<field><globals>
How can I get a field's value from a global based on a url segment?
# How can I get a field's value from a global based on a url segment?

(Asked by: username_0 on 2016-02-18)

I have a rich text field with a handle of `myTextField` that I have included in a global set called `firstGlobalSet`.

What If I created another global set called `secondGlobalSet` and included the same field `myTextField`.

How could I get the correct field from the correct global set based on url segment?

For example, `mysite.com/first` or `mysite.com/second`

**Tags:** field, globals

---

## 1 answers

### Answer by username_0 on 2016-02-18. Score: 2

You will need to access a few things. 

The url segment you need (for example the last segment)
The global set name
The field handle name

You can use getLastSegment to access the last url segment.
Using the global set names above let's say I have a url like this:
mysite.com/first

{% set segment = craft.request.getLastSegment() %}  // first

I know I've got a global set called firstGlobalSet, and now I've got first. We just need to put them together. You can use Craft's 
getSetByHandle method to access the global set via its handle.
{% set global = craft.globals.getSetByHandle(segment ~ 'GlobalSet') %}
{{ global }} // firstGlobalSet

Now that we've got the correct global set, we can get to the field we need. In this case it's myTextField.
{{ global.myTextField }}


---

