Skip to content

Instantly share code, notes, and snippets.

@ianshea
Created July 16, 2014 18:47
Show Gist options
  • Save ianshea/f8182b5dca895f64d416 to your computer and use it in GitHub Desktop.
Save ianshea/f8182b5dca895f64d416 to your computer and use it in GitHub Desktop.
First Entry from First Sub Category from Top Category Slug in Craft
{# Get the category from the slug. This will return one CategoryModel #}
{% set topCat = craft.categories.slug('someSlug').first() %}
{# Get the first subcategory under the top level category using the getChildren() method on the category model. #}
{% set firstSubCat = topCat.getChildren().first() %}
{# Grab the first entry under that category using the relatedTo function and appending the .first() method to return only one item. #}
{% set firstEntry = craft.entries.relatedTo( firstSubCat ).first() %}
{# Set your image from your asset field #}
{% set image = firstEntry.assetField.first() %}
{# Party #}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment