ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Using PHP variables within a channel tag parameter

February 20, 2011 8:58pm

Subscribe [2]
  • #1 / Feb 20, 2011 8:58pm

    mattgreen110

    46 posts

    Simple question. I noticed you cannot use php variables within channel tag parameters, or the query module. How would I accomplish something like this?

    For example I am using structure for handling pages and the client has the ability to add and edit pages. There is a custom field assign to a pages channel named “page_header”. This is a file field type that adds is the page header image. I want to write an if statement that basically goes like this—-> if the page contains a page header, display it, else if it has a parent page with a page header, use that, finally if neither, then use a default image.

    The problem comes when I am trying to use a channel tag with dynamic off to pull in it’s top level page’s page header. First I get the parent page’s id like this…

    {exp:channel:entries channel="pages"}
        {exp:query sql="SELECT parent_id FROM exp_structure WHERE entry_id = '{entry_id}'"}
            <?php $parent_id = "{parent_id}"; ?>
        {/exp:query}
    {/exp:channel:entries}

    So now I would like to be able to do something like this…. obviously the php variable will not work like this below, but given the concept, any ideas on how to accomplish getting a php variable within a tag parameter for this kind of purpose?

    {exp:channel:entries channel="pages" dynamic="off" entry_id="$parent_id"}
         {pages_header_img}
    {/exp:channel:entries}

    I also tried to put another channel entry tag again within the first like this… but that didn’t work either.

    {exp:channel:entries channel="pages"}
        {exp:query sql="SELECT parent_id FROM exp_structure WHERE entry_id = '{entry_id}'"}
            {exp:channel:entries channel="pages" dynamic="off" entry_id="{parent_id}"}
                {pages_header_img}
            {/exp:channel:entries}
        {/exp:query}
    {/exp:channel:entries}
  • #2 / Feb 21, 2011 10:43am

    Sue Crocker

    26054 posts

    Hi, Matt - nesting channel entries like that is a no-no. You could use the exp:query statement to grab the parent_id however you want it, and then embed a exp:channel:entries tag inside the query loop. Is this a single entry page?

  • #3 / Feb 21, 2011 10:53am

    mattgreen110

    46 posts

    What do you mean by single entry page? The only entry within that page? sorry probably a stupid question I am asking…

    And also you mean, put the channel entries tag inside of the query statement, or an actual embed? ... Can you give me a quick code example?

  • #4 / Feb 21, 2011 5:37pm

    Ingmar

    29245 posts

    A single entry page is a template that is designed to show only one entry, as opposed to an index page that will show a list of entries, possible together with the first paragraph or something like that. That said, Sue suggested to use the query module to get parent_id directly instead of using exp:channel:entries inside of a query inside of exp:channel:entries.

  • #5 / Feb 21, 2011 5:45pm

    mattgreen110

    46 posts

    ok thanks…

    I guess the original question is more how can I declare variables and then somehow use them within the query module or channel tag parameters? Is there are way?

  • #6 / Feb 22, 2011 9:07am

    Sue Crocker

    26054 posts

    Matt, EE exposed different kinds of information to you depending on what you’re wanting. When you say variables, what are you meaning? You can use embeds, which is one method of passing variables to something. How about an example of what you need to do?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases