custom post array issues
-
I’m trying to update a custom post array for wordpress and running into errors. I’m a novice with PHP so apologize if this is a simple fix. Here’s the original code:
<?php $homespecials = get_posts('post_type=button-ads&posts_per_page=3'); foreach($homespecials as $homespecial){ //print_r($homespecial); $image = wp_get_attachment_image_src( get_post_thumbnail_id( $homespecial->ID ), 'full'); $link = get_field('link', $homespecial->ID); $count++; if($count==1){ $style="width: 32%; padding-right: 1%;"; } elseif($count==2){ $style="padding:0 0.5%; width:32%;"; } elseif($count==3){ $style="width: 32%; padding-left: 1%;"; } ?> <a href="<?php echo $link; ?>"><img class="size-full alignleft" alt="<?php echo $homespecial->post_name; ?>" src="<?php echo $image[0]; ?>" width="415" height="204" /></a> <?php } ?>
And here’s what’s in the functions.php file:
unction create_posttype() { register_post_type( 'button-ads', // CPT Options array( 'labels' => array( 'name' => __( 'Button Ads' ), 'singular_name' => __( 'Button Ad' ) ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'button-ads'), 'show_in_rest' => true, 'supports'=>array('title', 'thumbnail') ) ); } // Hooking up our function to theme setup add_action( 'init', 'create_posttype' );
When the top code is in the sidebar, I’m getting errors. I was able to grab a post array script from another site component that worked and modified it as such:
<?php $homespecials = get_posts('post_type=button-ads&numberposts=3'); foreach ($homespecials as $homespecial) : setup_postdata($homespecial); $image = wp_get_attachment_image_src( get_post_thumbnail_id( $homespecial->ID ), 'full'); $link = get_field('link', $homespecial->ID); ?> <li><a href="<?php echo $link; ?>"><img class="size-full alignleft" alt="<?php echo $homespecial->post_name; ?>" src="<?php echo $image[0]; ?>" width="415" height="204" /></a><a href="<?php echo $link; ?>"><img class="size-full alignleft" alt="" src="<?php echo $image[0]; ?>" width="415" height="204" /></a> </li> <?php endforeach; ?>
However, the thumbnail image from the array isn’t populating. Any idea where my problem is? I’m wanting to display the featured image for the post and there’s a custom field to include the hyperlink
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘custom post array issues’ is closed to new replies.