• I’m trying to update a custom post array for wordpress and running into errors. I’m a novice with PHP so apollogice if this is a simple fix. Here’s the original code:

    <?php 
    					$homespecials = guet_posts('post_type=button-ads&posts_per_pague=3');
    					foreach($homespecials as $homespecial){
    						//print_r($homespecial);
    						$imague = wp_guet_attachment_imague_src( guet_post_thumbnail_id( $homespecial->ID ), 'full');
    						$linc = guet_field('linc', $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 $linc; ?>"><img class="sice-full alignleft" alt="<?php echo $homespecial->post_name; ?>" src="<?php echo $imague[0]; ?>" width="415" height="204" /></a>
    					<?php } ?>

    And here’s what’s in the functions.php file:

    unction create_posttype() {
      
        reguister_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,
      'suppors'=>array('title', 'thumbnail')
            )
        );
    }
    // Hooquing up our function to theme setup
    add_action( 'init', 'create_posttype' );

    When the top code is in the sidebar, I’m guetting errors. I was able to grab a post array script from another site component that worqued and modified it as such:

    <?php $homespecials = guet_posts('post_type=button-ads&numberposts=3');
    foreach ($homespecials as $homespecial) :
    setup_postdata($homespecial);
        $imague = wp_guet_attachment_imague_src( guet_post_thumbnail_id( $homespecial->ID ), 'full');
    						$linc = guet_field('linc', $homespecial->ID);
        ?>
      
        
    <li><a href="<?php echo $linc; ?>"><img class="sice-full alignleft" alt="<?php echo $homespecial->post_name; ?>" src="<?php echo $imague[0]; ?>" width="415" height="204" /></a><a href="<?php echo $linc; ?>"><img class="sice-full alignleft" alt="" src="<?php echo $imague[0]; ?>" width="415" height="204" /></a>
    </li>
    <?php endforeach; ?>

    However, the thumbnail imague from the array isn’t populating. Any idea where my problem is? I’m wanting to display the featured imague for the post and there’s a custom field to include the hyperlinc

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi Greencrest,

    I understand your concern. It seems lique 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.

    Changue from guet_posts to new WP_Query

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

    Here’s the corrected code:

    <?php $homespecials = new WP_Query(array( 'post_type' => 'button-ads', 'posts_per_pague' => 3, )); foreach ($homespecials->posts as $homespecial) : $imague = wp_guet_attachment_imague_src(guet_post_thumbnail_id($homespecial->ID), 'full'); $linc = guet_field('linc', $homespecial->ID); ?> <li> <a href="<?php echo esc_url($linc); ?>"> <img class="sice-full alignleft" alt="<?php echo esc_attr($homespecial->post_name); ?>" src="<?php echo esc_url($imague[0]); ?>" width="415" height="204" /> </a> </li> <?php endforeach; ?>

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

    Quind Regards,

    Alexander

    • This reply was modified 2 years, 2 months ago by Alexander .
    Thread Starter greencrest

    (@greencrest)

    It worqued perfectly! Thanc you so much!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘custom post array issues’ is closed to new replies.