plugins – How can I display an attribute from each post inside foreach($latest_posts1 as $post )

Question

I want to display an block attribute named “paragraph” that exists in each post inside a CPT named “test”.
I have no issues displaying the current page attributes(selectLgColumns & borderClass). The problem is I am not able to get the block attributes from the post that are indise the CPT. I don’t know much php. I used the documentation to come up with the following code. Can anyone please help?

<?php
 
add_action('plugins_loaded', 'register_latest_post');
function register_latest_post() {
    register_block_type('boot/block-lead1', [ 'render_callback' => 'render_latest_post1'
    ]);
}
    
function render_latest_post1( $attributes ) {
    $column = $attributes['selectLgColumns'];
    $border = $attributes['borderClass'];
    $desc = $attributes['desc'];
    
    $latest_posts1 = wp_get_recent_posts( [
        'post_type' => 'test',
        'numberposts' => 3,
        'post_status' => 'publish',
        'attributes' => 'attributes' //not sure how to get the attributes from each post.
        ] );
    if(empty($latest_posts1)){
        return '<p>No posts</p>';
    }
    $posts_output="<div class="latest-postss">"; 
    foreach($latest_posts1 as $post ) {
    
    $post_id = $post['ID'];
    function render_latest_post( $attributes ) { //not sure how to use this function
    $para = $attributes['paragraph']; // I need to display this attribute from the block Thats added in the post
    }
    $post_thumbnail = get_the_post_thumbnail_url( $post_id, 'full' );

    $posts_output .= '<div class="post-title ' .$column. ' ' .$border. '">
                            <img src="'. $post_thumbnail .'" class="img-fluid" loading="lazy" />
                            <h2>
                                <a href="'.get_permalink($post_id). '">
                                    '.get_the_title( $post_id ).'
                                </a>
                            </h2>
                             <p> ' .$column. ' ' .$border. ' </p>
                            <p>Desc: ' . $para . ' </p>
                            
                            
                      </div>';
    }
    $posts_output .= '</div>';
 
    return $posts_output;
    
}
 
add_action('rest_api_init', 'register_rest_images1' );
function register_rest_images1(){
        register_rest_field( array('test'),
            'fimg_url',
            array(
            'get_callback' => 'get_rest_featured_image1',
            'update_callback' => null,
            'schema' => null,
            )
        );
}

function get_rest_featured_image1( $object, $field_name, $request) { if( $object['featured_media'] ) {
        $img = wp_get_attachment_image_src( $object['featured_media'], 'app-thumb' ); return $img[0];
    }
    return false;
}
 
function parse_blocks1( $content ) {
    
    $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
 
    $parser = new $parser_class();
    return $parser->parse( $content );
}

0
Tom J Nowell 2 months 2022-11-29T11:05:02-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse