Skip to content

Replacing matrix image slugs into another field

Mark Croxton edited this page Aug 22, 2013 · 8 revisions

Replace {image_n} tags in a content field when capturing multiple entries as a list:

{exp:stash:set_list name="blog_list" parse_tags="yes" parse_depth="2"}
    {exp:channel:entries channel="blog" limit="5"}

        {stash:item_title}{title}{/stash:item_title}
        {stash:entry_date}{entry_date format="%F %j%S %Y"}{/stash:entry_date}

        {!-- Tag pair for Matrix field named 'blog_images' --}           
        {blog_images}
            {exp:stash:set name="image_{row_count}" type="snippet"}
                <img src="{image_file:url}">
            {/exp:stash:set}
        {/blog_images}

        {!-- Parse body field at this stage in order to separately capture all the Matrix field images per entry --}
        {exp:stash:parse parse_vars="yes"}
            {stash:item_body}{blog_body}{/stash:item_body}
        {/exp:stash:parse}

    {/exp:channel:entries}  
{/exp:stash:set_list}

{exp:stash:get_list name="blog_list"}
    <h1>{title} - Posted on {entry_date}</h1>
    {body}
{/exp:stash:get_list}

When pagination is required:

{exp:stash:set_list name="blog_list" parse_tags="yes" parse_depth="2"}
    {exp:channel:entries channel="blog" limit="5" paginate="bottom"}

        {stash:title}{title}{/stash:title}     
        {stash:entry_date}{entry_date format="%F %j%S %Y"}{/stash:entry_date}
    
        {!-- Tag pair for Matrix field named 'blog_images' --}
        {blog_images}
            {exp:stash:set name="image_{row_count}" type="snippet"}
                <img src="{image_file:url}">
            {/exp:stash:set}
        {/blog_images}
    
        {!-- Parse body field at this stage in order to separately capture all the Matrix field images per entry --}
        {exp:stash:parse parse_vars="yes"}
            {stash:body}{blog_body}{/stash:body}
        {/exp:stash:parse}    

        {paginate}
        {exp:stash:set_list:nested name="pagination"}
            {stash:current_page}{current_page}{/stash:current_page}
            {stash:total_pages}{total_pages}{/stash:total_pages}
            {pagination_links}
                {previous_page}{stash:previous_page}{pagination_url}{/stash:previous_page}{/previous_page}
                {stash:pages}{page}<a href="{pagination_url}"style="font-weight:bold">{pagination_page_number}</a>{/page}{/stash:pages}
                {next_page}{stash:next_page}{pagination_url}{/stash:next_page}{/next_page}
            {/pagination_links}
        {/exp:stash:set_list:nested}
        {/paginate}

    {/exp:channel:entries}  
{/exp:stash:set_list} 

{exp:stash:get_list name="blog_list"}
    <h1>{title} - Posted on {entry_date}</h1>
    {body}
{/exp:stash:get_list}

{exp:stash:get_list name="pagination"}
     Page {current_page} of {total_pages} | {if previous_page}<a href="{previous_page}">Prev</a> | {/if} {pages}{if next_page} | <a href="{next_page}">Next</a>{/if}
{/exp:stash:get_list}
Clone this wiki locally