{"id":17202111,"date":"2023-11-14T22:17:58","date_gmt":"2023-11-14T22:17:58","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/custom-post-array-issues\/"},"modified":"2023-11-14T22:17:58","modified_gmt":"2023-11-14T22:17:58","slug":"custom-post-array-issues","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/custom-post-array-issues\/","title":{"rendered":"custom post array issues"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I&#8217;m trying to update a custom post array for wordpress and running into errors. I&#8217;m a novice with PHP so apologize if this is a simple fix. Here&#8217;s the original code: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php \n\t\t\t\t\t$homespecials = get_posts('post_type=button-ads&amp;posts_per_page=3');\n\t\t\t\t\tforeach($homespecials as $homespecial){\n\t\t\t\t\t\t\/\/print_r($homespecial);\n\t\t\t\t\t\t$image = wp_get_attachment_image_src( get_post_thumbnail_id( $homespecial-&gt;ID ), 'full');\n\t\t\t\t\t\t$link = get_field('link', $homespecial-&gt;ID);\n\t\t\t\t\t\t$count++;\n\t\t\t\t\t\tif($count==1){\n\t\t\t\t\t\t\t$style=\"width: 32%; padding-right: 1%;\";\n\t\t\t\t\t\t} elseif($count==2){\n\t\t\t\t\t\t\t$style=\"padding:0 0.5%; width:32%;\";\n\t\t\t\t\t\t} elseif($count==3){\n\t\t\t\t\t\t\t$style=\"width: 32%; padding-left: 1%;\";\n\t\t\t\t\t\t}\n\t\t\t\t\t\t?&gt;\n&lt;a href=\"&lt;?php echo $link; ?&gt;\"&gt;&lt;img class=\"size-full alignleft\" alt=\"&lt;?php echo $homespecial-&gt;post_name; ?&gt;\" src=\"&lt;?php echo $image&#091;0]; ?&gt;\" width=\"415\" height=\"204\" \/&gt;&lt;\/a&gt;\n\t\t\t\t\t&lt;?php } ?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And here&#8217;s what&#8217;s in the functions.php file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unction create_posttype() {\n  \n    register_post_type( 'button-ads',\n    \/\/ CPT Options\n        array(\n            'labels' =&gt; array(\n                'name' =&gt; __( 'Button Ads' ),\n                'singular_name' =&gt; __( 'Button Ad' )\n            ),\n            'public' =&gt; true,\n            'has_archive' =&gt; true,\n            'rewrite' =&gt; array('slug' =&gt; 'button-ads'),\n            'show_in_rest' =&gt; true,\n  'supports'=&gt;array('title', 'thumbnail')\n        )\n    );\n}\n\/\/ Hooking up our function to theme setup\nadd_action( 'init', 'create_posttype' );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the top code is in the sidebar, I&#8217;m getting errors. I was able to grab a post array script from another site component that worked and modified it as such:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php $homespecials = get_posts('post_type=button-ads&amp;numberposts=3');\nforeach ($homespecials as $homespecial) :\nsetup_postdata($homespecial);\n    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $homespecial-&gt;ID ), 'full');\n\t\t\t\t\t\t$link = get_field('link', $homespecial-&gt;ID);\n    ?&gt;\n  \n    \n&lt;li&gt;&lt;a href=\"&lt;?php echo $link; ?&gt;\"&gt;&lt;img class=\"size-full alignleft\" alt=\"&lt;?php echo $homespecial-&gt;post_name; ?&gt;\" src=\"&lt;?php echo $image&#091;0]; ?&gt;\" width=\"415\" height=\"204\" \/&gt;&lt;\/a&gt;&lt;a href=\"&lt;?php echo $link; ?&gt;\"&gt;&lt;img class=\"size-full alignleft\" alt=\"\" src=\"&lt;?php echo $image&#091;0]; ?&gt;\" width=\"415\" height=\"204\" \/&gt;&lt;\/a&gt;\n&lt;\/li&gt;\n&lt;?php endforeach; ?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, the thumbnail image from the array isn&#8217;t populating. Any idea where my problem is? I&#8217;m wanting to display the featured image for the post and there&#8217;s a custom field to include the hyperlink<\/p>\n","protected":false},"template":"","class_list":["post-17202111","topic","type-topic","status-publish","hentry","topic-tag-array","topic-tag-custom-posts"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17202111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17202111\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=17202111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}