{"id":3082,"date":"2024-12-20T21:42:32","date_gmt":"2024-12-20T20:42:32","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3082"},"modified":"2024-12-20T21:43:42","modified_gmt":"2024-12-20T20:43:42","slug":"folders-metadata-in-php","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3082","title":{"rendered":"folder&#8217;s metadata in php"},"content":{"rendered":"\n<p>You can add metadata to a website folder, i.g. adding in it a file, such as . folder, within write something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"description\": \"your description\",\n    \"order\": 2\n}<\/code><\/pre>\n\n\n\n<p>&#8220;order&#8221;: 2, in case you want that that folder be the second in your webpage<\/p>\n\n\n\n<p>In the webpage you can retrieve the metadata info with a code like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$d = dir(\".\");\n$folders = &#91;];\n\n\/\/ Array delle cartelle da escludere\n$excludedDirs = &#91;'excluded-dir', 'another-excluded-dir', 'yet-another-excluded-dir'];\n\nwhile (false !== ($entry = $d->read())) {\n    \/\/ Controlla se l'entry \u00e8 una directory e non \u00e8 una delle cartelle da escludere\n    if (is_dir($entry) &amp;&amp; ($entry != '.') &amp;&amp; ($entry != '..') &amp;&amp; !in_array($entry, $excludedDirs)) {\n        $metadataFile = $entry . '\/.directory';\n        $metadata = &#91;];\n\n        \/\/ Recupera i metadati dal file .directory\n        if (file_exists($metadataFile)) {\n            $metadata = json_decode(file_get_contents($metadataFile), true); \/\/ Leggi e decodifica il contenuto del file\n        }\n\n        \/\/ Aggiungi la cartella e i metadati all'array\n        $folders&#91;] = &#91;\n            'name' => $entry,\n            'displayName' => str_replace('-', ' ', $entry), \/\/ Sostituisci i trattini con spazi per la visualizzazione\n            'order' => isset($metadata&#91;'order']) ? $metadata&#91;'order'] : PHP_INT_MAX, \/\/ Usa un valore alto se non \u00e8 specificato\n            'description' => isset($metadata&#91;'description']) ? $metadata&#91;'description'] : ''\n        ];\n    }\n}\n\n\/\/ Ordina le cartelle in base all'attributo 'order'\nusort($folders, function($a, $b) {\n    return $a&#91;'order'] &lt;=> $b&#91;'order'];\n});\n\necho \"&lt;h2>sotto-sezioni&lt;\/h2>\\n&lt;ul>\";\nforeach ($folders as $folder) {\n    echo \"&lt;li order='{$folder&#91;'order']}'>&lt;a  class='big' href='{$folder&#91;'name']}'>{$folder&#91;'displayName']}&lt;\/a>\";\n    if ($folder&#91;'description']) {\n        echo \"&lt;br \/>&lt;p>{$folder&#91;'description']}&lt;\/p>\"; \/\/ Mostra la descrizione se presente\n    }\n    echo \"&lt;\/li>\";\n}\necho \"&lt;\/ul>\";\n\n$d->close();\n?><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can add metadata to a website folder, i.g. adding in it a file, such as . folder, within write something like: &#8220;order&#8221;: 2, in case you want that that folder be the second in your webpage In the webpage you can retrieve the metadata info with a code like this:<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[885,569,751,566],"tags":[903,916,178,904],"class_list":["post-3082","post","type-post","status-publish","format-standard","hentry","category-code","category-internet","category-php","category-webmaster","tag-automatic-list","tag-folder-list","tag-metadata","tag-metatags"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3082"}],"version-history":[{"count":1,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3082\/revisions"}],"predecessor-version":[{"id":3083,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3082\/revisions\/3083"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}