Support

Account

Home Forums General Issues post object – output title from page not post

Solved

post object – output title from page not post

  • Hi. I have created an acf block for gutenberg.
    I used a post object for this block and the content from the selected post is shown in the frontend but it gives me the title of the page, not from the selected post.
    Any ideas?

    Here is the code:

    <?php if( have_rows('cta_content') ): ?>
    	<?php while( have_rows('cta_content') ): the_row(); ?>
    		<?php if( get_row_layout() == 'individually' ): ?>
    			<h1 class="cta__headline"><?php the_sub_field('headline'); ?></h1>
    			<div class="cta__content"><?php the_sub_field('content'); ?></div>
    		<?php elseif( get_row_layout() == 'from_post' ): ?>
    			
    			<?php
    			
    			$post_object = get_sub_field('content');
    			
    			if( $post_object ):
    				
    				$post = $post_object;
    				setup_postdata( $post );
    				?>
    				<div>
    
    					<h1 class="cta__headline"><?php the_title(); ?> <?php the_ID();?></h1>
    					<div class="cta__content"><?php the_content(); ?></div>
    				</div>
    				<?php echo '<pre>';
    				print_r( get_sub_field('content')  );
    				echo '</pre>';
    				die; ?>
    				<?php wp_reset_postdata(); ?>
    			<?php endif; ?>
    		
    		<?php endif; ?>
    	<?php endwhile; ?>
    <?php endif; ?>

    Here the output with debugging:
    (“Blöcke” – Headline from page; “Ein Test” – Headline from post)

    BLÖCKE 72
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes
    
    WP_Post Object
    (
        [ID] => 83
        [post_author] => 1
        [post_date] => 2020-06-03 14:06:39
        [post_date_gmt] => 2020-06-03 12:06:39
        [post_content] => 
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes
    
        [post_title] => Ein Test
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => ein-test
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2020-06-09 09:30:29
        [post_modified_gmt] => 2020-06-09 07:30:29
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => //localhost:3000/?p=83
        [menu_order] => 0
        [post_type] => post
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
  • Hi there,
    instead of the_title() try function get_the_title() and pass a post ID inside.
    Have a look at doc:

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

The topic ‘post object – output title from page not post’ is closed to new replies.