{"id":232748,"date":"2023-01-27T10:17:00","date_gmt":"2023-01-27T07:17:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=232748"},"modified":"2023-01-28T18:18:32","modified_gmt":"2023-01-28T15:18:32","slug":"como-mostrar-postagens-aleatorias-no-wordpress-usando-miniaturas","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/pt-pt\/como-mostrar-postagens-aleatorias-no-wordpress-usando-miniaturas\/","title":{"rendered":"Como mostrar postagens aleat\u00f3rias no WordPress usando miniaturas?"},"content":{"rendered":"<p>Nos <a href=\"https:\/\/wordpress.mediadoma.com\/pt-pt\/como-mostrar-posts-historicos-hoje-no-wordpress\/\" title=\"\u00faltimos posts\">\u00faltimos posts<\/a>, sabemos que um dos m\u00e9todos para aumentar as visualiza\u00e7\u00f5es de p\u00e1gina \u00e9 mostrar uma lista de posts no passado. Outro m\u00e9todo \u00e9 mostrar postagens aleat\u00f3rias. Claro, estamos mostrando as postagens usando a miniatura na postagem, o que ser\u00e1 atraente. Portanto, a melhor ideia \u00e9 coloc\u00e1-lo no menu de navega\u00e7\u00e3o (algo como o <a href=\"https:\/\/wordpress.mediadoma.com\/pt-pt\/como-mostrar-artigos-relacionados-usando-o-conteudo-correspondente-do-google-adsense\/\" title=\"conte\u00fado do Google Match\">conte\u00fado do Google Match<\/a> ).<\/p>\n<p>Voc\u00ea precisaria primeiro definir uma fun\u00e7\u00e3o para recuperar a primeira imagem na postagem. Isso deve ser usado se a [Imagem do recurso] n\u00e3o estiver definida para uma postagem.<\/p>\n<pre><code>\u00a0function catch_first_image($post_id) {\n\u00a0 \u00a0ob_start();\n\u00a0 \u00a0ob_end_clean();\n\u00a0 \u00a0$related_post = get_post($post_id);\n\u00a0 \u00a0$content = $related_post-&gt;post_content;\n\u00a0 \u00a0$output = preg_match_all('\/&lt;img.+src=['\"]([^'\"]+)['\"].*&gt;\/i', $content, $matches);\n\u00a0 \u00a0return $matches[1][0];\n\u00a0} <\/code><\/pre>\n<p>Em seguida, coloque o seguinte em algum lugar que voc\u00ea gosta, por exemplo Single.php. Voc\u00ea pode ajustar o n\u00famero de postagens ($num = 4) de acordo.<\/p>\n<pre><code>&lt;style&gt;\n.justyyimg {max-width:120px; height: 120px;}\n&lt;\/style&gt;\n\u00a0\n&lt;div id=\"related-posts\"&gt;\n\u00a0 \u00a0 &lt;div class=\"related\" class=\"clearfix\"&gt;\n\u00a0 \u00a0 \u00a0 &lt;?php \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\n\u00a0 \u00a0 \u00a0 $post_num = 4;\n\u00a0 \u00a0 \u00a0 $exclude_id = $post-&gt;id; \n\u00a0 \u00a0 \u00a0 $args = array(\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'post_status' =&gt; 'publish',\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'post__not_in' =&gt; explode(',', $exclude_id),\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'orderby' =&gt; 'rand',\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'posts_per_page' =&gt; $post_num\n\u00a0 \u00a0 \u00a0 );\n\u00a0 \u00a0 \u00a0 query_posts($args);\n\u00a0 \u00a0 \u00a0 while( have_posts()) { the_post(); ?&gt;\n\u00a0 \u00a0 \u00a0 &lt;a href=\"&lt;?php echo the_permalink(); ?&gt;\" title=\"&lt;?php the_title(); ?&gt;\" &gt;\n\u00a0 \u00a0 \u00a0 &lt;?php \n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if (has_post_thumbnail()) {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 the_post_thumbnail();\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } else {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $img = catch_first_image(get_the_id());\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if (strlen($img)) {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 echo \"&lt;img alt='\".get_the_title().\"' class='justyyimg' src=\"\".$img.\"\"\/&gt;\";\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } else {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 echo \"&lt;img alt='\".get_the_title().\"' class='justyyimg' src=\"&lt;a class=\"vglnk\" target=\"_blank\" href=\"https:\/\/justyy.com\/jpg\/\".mt_rand\" rel=\"nofollow\"&gt;&lt;span&gt;https&lt;\/span&gt;&lt;span&gt;:\/\/&lt;\/span&gt;&lt;span&gt;justyy&lt;\/span&gt;&lt;span&gt;.&lt;\/span&gt;&lt;span&gt;com&lt;\/span&gt;&lt;span&gt;\/&lt;\/span&gt;&lt;span&gt;jpg&lt;\/span&gt;&lt;span&gt;\/\".&lt;\/span&gt;&lt;span&gt;mt&lt;\/span&gt;&lt;span&gt;_&lt;\/span&gt;&lt;span&gt;rand&lt;\/span&gt;&lt;\/a&gt;(1, 20).\".jpg\"\/&gt;\";\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 ?&gt;&lt;\/a&gt;\n\u00a0 \u00a0 \u00a0 &lt;?php\n\u00a0 \u00a0 \u00a0 \u00a0 $exclude_id .= ','. get_the_id(); \u00a0 \u00a0 \u00a0 \u00a0 \n\u00a0 \u00a0 \u00a0 } \n\u00a0 \u00a0 \u00a0 wp_reset_query();\n\u00a0 \u00a0 \u00a0 ?&gt;\n\u00a0 \u00a0 &lt;\/div&gt;\n&lt;\/div&gt; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/code><\/pre>\n<p>Usamos o <strong>has_post_thumbnail()<\/strong> para verificar primeiro a exist\u00eancia de miniaturas de postagem.<\/p>\n<pre><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if (has_post_thumbnail()) {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 the_post_thumbnail();<\/code><\/pre>\n<p>Voc\u00ea pode, \u00e9 claro, mostrar postagens relacionadas ajustando a consulta.<\/p>\n<p>\u2013A definir\u2013<\/p>\n<p>Classifica\u00e7\u00e3o: 10,0\/ <strong>10<\/strong> (1 voto expresso)<\/p>\n<p>403 palavras<br \/>\n<strong>\u00daltimo Post<\/strong>: <a href=\"https:\/\/wordpress.mediadoma.com\/pt-pt\/como-mostrar-posts-historicos-hoje-no-wordpress\/\" title=\"Como Mostrar Posts do Hist\u00f3rico 'Hoje' no WordPress?\">Como Mostrar Posts do Hist\u00f3rico &#8216;Hoje&#8217; no WordPress?<\/a><br \/>\n<strong>Pr\u00f3ximo post<\/strong>: <a href=\"https:\/\/wordpress.mediadoma.com\/pt-pt\/plugin-wp-rocket-um-deve-ter-para-usuarios-do-wordpress\/\" title=\"Plugin WP-Rocket \u2013 Um must have para usu\u00e1rios do WordPress!\">Plugin WP-Rocket \u2013 Um must have para usu\u00e1rios do WordPress!<\/a><\/p>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">Fonte de grava\u00e7\u00e3o:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/helloacm.com\" class=\"external external_icon\">helloacm.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Como mostrar postagens aleat\u00f3rias no WordPress usando miniaturas?<\/p>\n","protected":false},"author":1,"featured_media":223922,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[898,837,998,920,806,846,867],"tags":[1170],"class_list":["post-232748","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codigo-2","category-guia-para-iniciantes","category-marketing-de-conteudo","category-outro","category-php-8","category-tutoriais","category-wordpress-8","tag-affiai-pt-pt"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/posts\/232748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/comments?post=232748"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/posts\/232748\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/media\/223922"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/media?parent=232748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/categories?post=232748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pt-pt\/wp-json\/wp\/v2\/tags?post=232748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}