{"id":3483,"date":"2026-06-18T16:15:26","date_gmt":"2026-06-18T14:15:26","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3483"},"modified":"2026-06-18T16:28:14","modified_gmt":"2026-06-18T14:28:14","slug":"automatically-list-wp-posts","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3483","title":{"rendered":"Automatically list wp posts"},"content":{"rendered":"\n<p>You can list automatically wordpress posts in your php webpage, based on its tags.<\/p>\n\n\n\n<p>The following php code allow you to list all wp posts with a given tag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/ --- 1\ufe0f\u20e3 Caricamento WordPress (solo se non gi\u00e0 caricato) ---\nif (!function_exists('get_posts')) {\n    $possible_paths = &#91;\n        __DIR__.\"\/..\/&#91;your-wp-path]\/wp-load.php\",  \/\/ standard\n        __DIR__.\"\/..\/..\/&#91;your-wp-path]\/wp-load.php\", \/\/ if you inclyde subfolders\n    ];\n    foreach ($possible_paths as $path) {\n        if (file_exists($path)) {\n            require_once($path);\n            break;\n        }\n    }\n}\n\n\/\/ --- 2\ufe0f\u20e3 Preparazione query ---\nglobal $post;\n$args = &#91;\n    'posts_per_page' => 200,\n];\n\nif (!empty($whatcategory)) {\n    $args&#91;'category_name'] = $whatcategory; \/\/ category slug \n}\nif (!empty($whattag)) {\n    \/\/ $args&#91;'tag'] = $whattag; \n    $args&#91;'tag_slug__in'] = array_map('trim', explode(',', $whattag)); \/\/ tag slug (not shown names)\n}\n\n\/\/ --- 3\ufe0f\u20e3 Cache opzionale (transient WordPress) ---\n$cache_key = 'lista_blog_' . ($whatcategory ?? 'none') . '_' . ($whattag ?? 'none');\n$myposts = get_transient($cache_key);\n\nif ($myposts === false) {\n    $myposts = get_posts($args);\n    set_transient($cache_key, $myposts, 3600); \/\/ cache 1 ora\n}\n\n\/\/ --- 4\ufe0f\u20e3 Output ---\nif (!empty($myposts)) {\n    echo '&lt;h2>\n        &lt;span lang=\"en\">Our blog posts about this topic&lt;\/span>\n        &lt;span lang=\"it\">Posts del nostro blog su questo tema&lt;\/span>\n    &lt;\/h2>\n    &lt;ul id=\"blog-posts\">';\n\n    foreach ($myposts as $post) {\n        \/\/ Non dipendiamo dal loop globale\n        echo '&lt;li>&lt;a href=\"'.get_permalink($post).'\" rel=\"bookmark\">'.get_the_title($post).'&lt;\/a>&lt;\/li>';\n    }\n\n    echo '&lt;\/ul>';\n    wp_reset_postdata();\n}\n?><\/code><\/pre>\n\n\n\n<p>You can put this code in a separate file, and include it in your php webpage, i.g. in the footer, with a code like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$listablog = \"&#91;the-path-of-your-separate-file\/the-name-of-your-separate-file\";\n  if (file_exists($listablog)) {\n    if (\n        (isset($whatcategory) &amp;&amp; !empty($whatcategory)) ||\n        (isset($whattag) &amp;&amp; !empty($whattag))\n    ) {\n        include $listablog;\n    }\n  }\n?>\n\nIn the header of a webpage where you want add this list, you have to set the value of the tag you want list:<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$whattag=\"your tag,another tag\";<\/code><\/pre>\n\n\n\n<p>Be careful: you has to set, as value, not the visualized tag name, but the &#8220;slug&#8221;. You can see it also with mouse hover and status bar.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can list automatically wordpress posts in your php webpage, based on its tags. The following php code allow you to list all wp posts with a given tag: You can put this code in a separate file, and include it in your php webpage, i.g. in the footer, with a code like the following: &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=3483\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;Automatically list wp posts&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[566],"tags":[1001,488],"class_list":["post-3483","post","type-post","status-publish","format-standard","hentry","category-webmaster","tag-automatic-links","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3483","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=3483"}],"version-history":[{"count":5,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3483\/revisions"}],"predecessor-version":[{"id":3489,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3483\/revisions\/3489"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}