Support

Account

Home Forums General Issues Post Object, not consistent…

Solved

Post Object, not consistent…

  • I’m pulling in some content from a custom post type, via a selector in another custom post type. Really, it’s *almost* all perfect in this case, but for some reason, the_title() is displaying the title of the original post, not the one selected – but it’s displaying the correct content and thumbnail, so I’m a little perplexed:

    In other words, this is what appears:

    MAIN TITLE
        MAIN CONTENT
            POST-CONTENT SELECTOR CONTENT
            POST-CONTENT SELECTOR FEATURED IMAGE
            MAIN TITLE (not post-content selector content title)

    Here’s my code for the selector area:

    <?php $post_quote = get_field('associated_quote');
    	if( $post_quote ): ?>
    	<blockquote class="testimonial">
    		<?php $post = $post_quote;
    											setup_postdata( $post );
    											$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($quote->ID), 'thumbnail' );
    											$url = $thumb['0'];
    											
    											setup_postdata( $quote );
    												the_content();
    												the_title(); ?>
    											<img src="<?=$url?>" title="<?php the_title() ?>" style="height: auto; width: auto;" class="testimonial-image">
    	</blockquote>
    <?php endif;
    										wp_reset_query(); ?>
  • Hi @brotsky_pixie

    You have multiple setup_postdata() but only one wp_reset_query() there. Could you please tell me what the $quote variable is? Could you please share the JSON export file of the field group and the full code of the page? If the full code is too long, you can share it using https://gist.github.com/.

    If you want to show nested post objects, I believe you need to do it like this:

    <?php
    $post_quote = get_field('associated_quote');
    if( $post_quote ): ?>
    <blockquote class="testimonial">
        <?php
        $post = $post_quote;
        setup_postdata( $post );
        the_title();
        
        
        $quote = get_field('associated_quote');
        $post = $quote;
        setup_postdata( $quote );
            the_title();
        wp_reset_query();    
        ?>
        
    </blockquote>
    
    <?php
    wp_reset_query();
    endif;
    ?>

    I hope this helps. Thanks 🙂

  • Oooof…thought I’d responded to this days ago. My apologies.

    First, $quote is just a text field – a testimonial from a client. Second, I’m not sure how to export the json of the field group. Third, following is the full code for the shortcode. Note that I’ve since made a number of changes as I continued to work on things, but I still haven’t figured out how to get the quotes pulled in so I had temporarily pulled the code entirely. So where it says “Quote goes here”, that’s where the code above was.

    <?php
    //Add Featured Portfolio items shortcode //
    add_shortcode( 'featured_portfolio', 'display_featured_portfolio_items' );
    
    function display_featured_portfolio_items(){
    	
    	ob_start();
    	
    	// Define the query
    	$args = array(
    		'post_type' => 'pixieportfolio',
    		'post_status' => 'publish',
    		'orderby' => 'menu_order',
    		'meta_query'	=> array(
    			array(
    				'key'	  	=> 'featured_portfolio_item',
    				'value'	  	=> '1',
    				'compare' 	=> '=',
    			)
    		),
    		'meta_key' => '_thumbnail_id',
    		'posts_per_page' => 999
    	);
    	
    	$query = new WP_Query( $args );
    	$count = 0;
    
    // Carousel // ?>
    <!--<div class="more-button">
    	<a href="/portfolio-all/" class="btn btn-default btn-lg" type="button" role="button">More Projects</a>
    </div>-->
    
    <div id="featuredCarousel" class="carousel slide" data-ride="carousel" data-pause="hover">
    		
    	<!-- Indicators -->
    	<ol class="carousel-indicators">
    		
    		<?php while($query->have_posts()): $query->the_post(); ?>
    
                <li <?php if($count == 0){ echo 'class="active"';} ?> data-target="#featuredCarousel" data-slide-to="<?php echo $count++; ?>"></li>
    
              <?php endwhile; ?>
    	</ol>
    	
    	<div class="carousel-inner" role="listbox">
    		
    		<?php $count = 0;
    			
    			while ($query->have_posts()) : $query->the_post();
    			$count++;
    			$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'pp-home-portfolio' );
    			$url = $thumb['0'];
    			$values = get_field('tech_choices');
    			$terms = get_the_terms($post->ID, 'post_tag');
    			$custom = get_post_custom($post->ID);
    			$caption = get_field('caption', '');
    			$client =  get_field('client', '');
    			$project_url = get_field('project_url', '');
    			$date = get_field('date', ''); ?>
    		
    		<!-- Wrapper for slides -->
    		<div class="item <?php if ($count == 1) { echo 'active';} ?>" data-slide-number="<?php echo $count++; ?>" id="<? the_ID(); ?>">
    			<div class="item-image" style="background-image: url(<?=$url?>);" title="<?php the_title() ?>" data-toggle="modal" data-target="#myModal<?php echo $count; ?>"></div>
    			
    			<div class="project-details">
    				<div class="project-detail-border shadow-effect"></div>
    				<div class="project-detail">
    					<div class="top-detail">
    						<h4><?php the_title(); ?></h4>
    						<div class="short-description">
    							<?php $content = get_the_content();
    								echo wp_trim_words( $content , '20' );
    							?>
    						</div>
    					</div>
    					
    					<table class="table project">
    						<tr>
    								<td valign="middle" class="fifty">
    									<?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'pixie-portfolio-categories', '', ', ', '' ) );
    echo $terms_as_text;?>
    								</td>
    								
    								<td valign="middle" class="fifty">
    									<?php if( $values ) {
    										
    										if(in_array('WordPress', $values )){ ?>
    											<i class="fa fa-wordpress fa-2x" data-toggle="tooltip" title="Wordpress"></i>
    										<?php } if(in_array('HTML-5', $values )){ ?>
    											<i class="fa fa-html5 fa-2x" data-toggle="tooltip" title="HTML-5"></i>
    										<?php } if(in_array('CSS-3', $values )){ ?>
    											<i class="fa fa-css3 fa-2x" data-toggle="tooltip" title="CSS-3"></i>
    										<?php } if(in_array('Sportswear', $values )){ ?>
    											<i class="fa fa-bicycle fa-2x" data-toggle="tooltip" title="Sportswear"></i>
    										<?php } if(in_array('Adobe', $values )){ ?>
    											<i class="fa fa-paint-brush fa-2x" data-toggle="tooltip" title="Adobe"></i>
    										<?php } if(in_array('Code', $values )){ ?>
    											<i class="fa fa-code fa-2x" data-toggle="tooltip" title="Code"></i>
    										<?php } if(in_array('Ecommerce', $values )) { ?>
    											<i class="fa fa-database fa-2x" data-toggle="tooltip" title="MySQL"></i>
    										<?php }
    									} else {} ?>
    									
    								</td>
    							</tr>
    							
    							<tr>
    								<td colspan="2" class="more-info" data-toggle="modal" data-target="#myModal<?php echo $count; ?>">
    									<div class="view-project">View Project</div>
    								</td>
    							</tr>
    					</table>
    				</div>
    			</div>
    		</div>
    		
    		<!-- Modal -->
    		<div id="myModal<?php echo $count; ?>" class="modal fade" role="dialog" aria-labelledby="myModal<?php echo $count; ?>Label">
    			<div class="modal-dialog modal-lg" role="document">
    				
    				<!-- Modal content-->
    				<div class="modal-content">
    					<div class="modal-header">
    						<button type="button" class="close" data-dismiss="modal">&times;</button>
    						<h2 class="modal-title"><?php the_title() ?></h2>
    						<span>
    							<?php if( $terms ): ?>
    							<ul>
    								<?php foreach( $terms as $term ): ?>
    								<li><a href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a></li>
    								<?php $icon = get_field('creative_icon', $term->taxonomy . '_' . $term->term_id); echo $icon; ?>
    								<?php endforeach; ?>
    							</ul>
    							<?php endif; ?>
    						</span>
    					</div>
    					<div class="modal-body">
    						<div class="container-fluid">
    							<div class="row">
    								<div class="col-md-8">
    									<div>
    										<p class="post-meta gallery-list">
    											<?php if( $client ): ?>
    											<span class="pixie-date">
    												<span class="title">Client:</span> <?=$client?>
    											</span>
    											<?php endif;
    											
    											if ( $date ): ?>
    											<span class="post-cats">
    												<span class="title">Date:</span> <?=$date?>
    											</span>
    											<?php endif;
    											
    											if ( $values ) { ?>
    											<span class="post-comments">
    												<?php $values = get_field('tech_choices'); ?>
    												<span class="title">Tech used in this project:</span>
    												
    												<?php if(in_array('WordPress', $values )){ ?>
    												<i class="fa fa-wordpress fa-2x" data-toggle="tooltip" data-placement="bottom" title="WordPress"></i>
    												<? }
    													
    												if(in_array('HTML-5', $values )){ ?>
    												<i class="fa fa-html5 fa-2x" data-toggle="tooltip" data-placement="bottom" title="HTML5"></i>
    												<? }
    													
    												if(in_array('CSS-3', $values )){ ?>
    												<i class="fa fa-css3 fa-2x" data-toggle="tooltip" data-placement="bottom" title="CSS3"></i>
    												<? }
    													
    												if(in_array('Sportswear', $values )){ ?>
    												<i class="fa fa-bicycle fa-2x" data-toggle="tooltip" data-placement="bottom" title="Sportswear"></i>
    												<? }
    												
    												if(in_array('Adobe', $values )){ ?>
    												<i class="fa fa-paint-brush fa-2x" data-toggle="tooltip" data-placement="bottom" title="Illustrator and/or Photoshop"></i>
    												<? }
    													
    												if(in_array('Code', $values )){ ?>
    												<i class="fa fa-code fa-2x" data-toggle="tooltip" data-placement="bottom" title="PHP, JQuery, etc."></i>
    												<? }
    													
    												if(in_array('Ecommerce', $values )){ ?>
    												<i class="fa fa-shopping-cart fa-2x" data-toggle="tooltip" data-placement="bottom" title="ECommerce"></i>
    												<? }
    												
    												if(in_array('MySQL', $values )){ ?>
    												<i class="fa fa-database fa-2x" data-toggle="tooltip" data-placement="bottom" title="MySQL"></i>
    												<? } ?>
    											</span>
    											<?php } ?>
    										</p>
    									</div>
    									
    									<?php echo the_content(); ?>
    									
    									<blockquote class="testimonial">
    									
    										QUOTE GOES HERE
    								
    									</blockquote>
    								</div>
    								
    								<div class="col-md-4">
    									<div class="gallery">
    									
    									<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'darwin-modal-main' );
    									$url = $thumb['0'];
    									$thumb2 = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
    									$url2 = $thumb2['0'];
    									$c = 0; ?>
    									<a href="<?=$url2?>" class="group" id="port_gal" rel="group<?php echo $c; ?>">
    										<img src="<?=$url?>" width="300" height="300">
    									</a>
    									
    									<?php $images = get_field('gallery_images');
    										if( $images ) {
    											foreach( $images as $image ) {
    									?>
    									<a href="<?php echo $image['sizes']['large'] ?>" class="group" id="port_gal"  rel="group<?php echo $c; ?>">
    										<img src="<?php echo $image['sizes']['darwin-modal-thumb'] ?>" alt="<?php $image['alt'] ?>" />
    									</a>
    											<?php }
    										} ?>
    									</div>
    									
    									<div class="website-button">
    									
    									<?php if ( $project_url ): ?>
    									<a class="btn btn-default" href="<?=$project_url?>" target="_blank" role="button">Visit Website</a>
    									<?php endif; ?>
    									</div>
    								</div>
    								
    							</div>
    						</div>
    					</div>
    					<div class="modal-footer">
    						<button type="button" class="close" data-dismiss="modal">&times;</button>
    					</div>
    				</div>
    			</div>
    		</div>
    		
    		<?php endwhile; ?>
    		
    	</div>
    	<?php wp_reset_query(); ?>
    </div>
    
    <?php return ob_get_clean(); } ?>
  • Hi @brotsky_pixie

    You can export field group from “Custom Fields > Tools” (PRO version) or “Custom Fields > Export” (Free version).

    Also, could you please share the full code of your current code (including the first code) here: https://gist.github.com/?

    Thanks 🙂

  • This reply has been marked as private.
  • Hi @brotsky_pixie

    I’m sorry I thought you’re using the PRO version, which uses JSON as the export file.

    Regarding your issue, I’m afraid I don’t quite understand your setup, so please forgive me if I’m wrong.

    Looking at your code and the export file, I believe you should do it like this:

    <?php
    $post_quote = get_field('associated_quote');
    if( $post_quote ): ?>
        <blockquote class="testimonial">
            <?php
            $post = $post_quote;
            setup_postdata( $post );
            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post_quote->ID), 'thumbnail' );
            $url = $thumb['0'];
    
            the_content();
            the_title(); ?>
            <img src="<? echo $url; ?>" title="<?php the_title(); ?>" style="height: auto; width: auto;" class="testimonial-image">
        </blockquote>
    <?php endif;
    wp_reset_query(); ?>

    If that doesn’t work, I think you can do it like this:

    <?php
    $post_quote = get_field('associated_quote');
    if( $post_quote ): ?>
        <blockquote class="testimonial">
            <?php
            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post_quote->ID), 'thumbnail' );
            $url = $thumb['0'];
    
            echo $post_quote->post_content;
            echo $post_quote->post_title;
            ?>
            <img src="<? echo $url; ?>" title="<?php echo $post_quote->post_title; ?>" style="height: auto; width: auto;" class="testimonial-image">
        </blockquote>
    <?php endif; ?>

    If still no joy, could you please open a new ticket and provide temporary admin credentials to your site? You can open a new ticket here: https://support.advancedcustomfields.com/new-ticket. Also, please don’t forget to explain the issue again and provide the link to this thread.

    Thanks 🙂

  • (I do have another client who purchased the Pro version and I’ve needed assistance with that before, so perhaps that’s why. No worries at all)

    Ah HA! The 2nd one worked great! Thank you SO very very much!

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

The topic ‘Post Object, not consistent…’ is closed to new replies.