• Resolved greencrest

    (@greencrest)


    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)
  • Hi Greencrest,

    I understand your concern. It seems like you’re encountering some issues with updating a custom post array in WordPress. After reviewing your code, I’ve identified a few potential issues and made some corrections.

    Change from get_posts to new WP_Query

    1. Instead of using get_posts, I recommend using WP_Query which is more versatile and gives you better control over your query.Replace:$homespecials = get_posts('post_type=button-ads&numberposts=3'); with:$homespecials = new WP_Query(array( 'post_type' => 'button-ads', 'posts_per_page' => 3, ));

    Here’s the corrected code:

    <?php $homespecials = new WP_Query(array( 'post_type' => 'button-ads', 'posts_per_page' => 3, )); foreach ($homespecials->posts as $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 esc_url($link); ?>"> <img class="size-full alignleft" alt="<?php echo esc_attr($homespecial->post_name); ?>" src="<?php echo esc_url($image[0]); ?>" width="415" height="204" /> </a> </li> <?php endforeach; ?>

    Please test this modified code and let me know if it resolves the issues you were facing. If you have any further questions or encounter additional problems, feel free to ask for more assistance.

    Kind Regards,

    Alexander

    • This reply was modified 11 months, 1 week ago by Alexander.
    Thread Starter greencrest

    (@greencrest)

    It worked perfectly! Thank you so much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom post array issues’ is closed to new replies.