Tag

Statamic

Statamic

Code Examples for Statamic Navigation Structures

Navigation structures in Statamic offer the ability to construct complex page trees with pages and subpages. The following code examples will assist in maintaining and creating these structures. Reset Structure To reset the entire structure of a page, the following code can be used: use Statamic\Facades\Collection; $structure = Collection::findByHandle("pages")->structure(); $tree = $structure->in("de"); $tree->delete(); This code would reset the structure of the d

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Example of Statamic Search via PHP Code

This article explains how to utilize the search function in the CMS Statamic using PHP code. Let's take a look at the following code example: use Statamic\Facades\Search; $builder = Search::index('default') ->ensureExists() ->search('This is my search query') ->where('status', 'published'); $results = $builder->limit(5)->get(); foreach ($results as $result) { echo $result->getCpTitle().PHP_EOL; } First, we initialize the search, set the search index to defau

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Code Examples for Statamic Entries

Query all entries of a collection use Statamic\Facades\Entry; Entry::query()->where("collection", "posts")->get(); In this example, the Entry facade of Statamic is used to query all entries (entries) in a specific collection (collection). Here, all entries of the posts collection are searched for. The get() method executes the query and returns the results. Query all entries of a user in a collection use Statamic\Facades\Entry; use Statamic\Facades\User; $author = Us

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Deployment Optimizations for Statamic Static Cache

The Flat File CMS Statamic allows the caching of pages through its 'Static Cache' feature, enabling very fast loading speeds. However, the use of complex plugins or extensions, such as Markdown Highlight or spatie/commonmark-shiki-highlighter, can significantly extend the caching duration for individual pages. For extensive websites, the complete renewal of the cache can take several minutes and heavily load the server during deployments. Two approaches are available for optimization. In the def

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Code Examples for Statamic Users

Querying UserID To retrieve the UserID of a user, we first use the User facade from Statamic. With User::query()->where('name', 'Sebastian Widmann')->first();, the user with the name 'Sebastian Widmann' is queried. first() ensures that only the first matching entry is returned. Subsequently, the id of the found user is stored in the $user_id variable. use Statamic\Facades\User; $user = User::query()->where('name', 'Sebastian Widmann')->first(); $user_id = $user->id; Querying Us

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Code Examples for Statamic Taxonomies and Terms

Taxonomies Taxonomies in Statamic provide the ability to categorize and organize content. For example, they can be used for tags, categories, or any other type of groupable content. Query all Taxonomies This code example reads all taxonomies in Statamic and prints their titles. use Statamic\Facades\Taxonomy; $taxonomies = Taxonomy::all(); foreach($taxonomies as $taxonomy) { echo $taxonomy->title . PHP_EOL; } Query Taxonomy by Handle This code example fetches a specific taxonomy by its ha

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Statamic - Checking User Status in Templates

Statamic provides flexible options for checking user login and permissions. This becomes particularly useful when you want to display links or navigation entries only if the user has the corresponding permission or status. Login Status Check Use the following tag to verify a user's login status: {{ if logged_in }} You are logged in! {{ /if }} Access to the Administration Area / Control Panel In the Statamic CMS, the administration area is referred to as the "Control Panel". Use the f

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Discover 404 HTTP Code in Statamic templates

The Hypertext Transfer Protocol (HTTP) offers a wide range of status codes, providing crucial information about the current request. These HTTP codes play a vital role in search engine optimization (SEO) as search engines like Google use them to understand a website's state and index it accordingly. One of the most well-known error codes is the 404 status code, which signals that a page has not been found. This can happen when a link leads to a non-existent page, or a URL is entered incorrectly.

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Check, if an entry has been translated

To check if an entry has been translated in Statamic, you can use the following code: {{ if {locales:count self="false"} > 0 }} Content has been translated {{ /if }} In this code, locales:count returns the number of translations. By using self="false", the currently displayed version is ignored. This value needs to be greater than 0, indicating that at least one additional translation is available.

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Statamic

Customize 404 error page when using Statamic

HTTP codes are a vital component of web communication and play a crucial role in Search Engine Optimization (SEO). They provide important information about the status of a web request. Well-known examples are the 200 code for successful requests, or the 404 code, which indicates that a page was not found. Statamic offers the option to create custom templates for various HTTP codes. This allows web developers to provide tailor-made pages for users, even if they encounter an error. The 404 error c

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer