• Resolved YtW

    (@ytw)


    Hi.

    I’m workling on a real estate website, using MLP (for German and French) and Advanced Custom Fields to create and display image galleries and energy performance certificates for the objects.

    Both image gallery and energy performance certificate are shown in the original blog (DE). Now I’m wondering if it is possible to get the content from here to display on the second blog (FR). I have worked with switch_to_blog on other multisite instances, but now I need to get the related post and display its content.

    Here is the code from the original blog (just the image gallery):

    <?php
    $images = get_field('images');
    
    if( $images ): ?>
    
      <hr>
        <div class="image-gallery clearfix">
            <?php foreach( $images as $image ): ?>
                    <a title="<?php echo get_the_title(); ?>" class="fancybox" data-fancybox-group="post-<?php get_the_ID(); ?>" href="<?php echo $image['url']; ?>">
                         <img class="alignleft" src="<?php echo $image['sizes']['thumbnail']; ?>" alt="" />
                    </a>
                    <div class="wpcasa-caption"><?php echo $image['caption']; ?></div>
            <?php endforeach; ?>
        </div>
      <hr>
    <?php endif; ?>

    What I want to reach is a solution like that:

    <?php
    $original_blog_id = get_current_blog_id(); // get current blog
    
    $bids = array(1); // blog_id of original blog
    foreach($bids as $bid):
            switch_to_blog($bid); // switched to blog with blog_id $bid
    ?>
    
    // How can I get the related post here ????
    
    <?php
    $images = get_field('images');
    
    if( $images ): ?>
    
      <hr>
        <div class="image-gallery clearfix">
            <?php foreach( $images as $image ): ?>
                    <a title="<?php echo get_the_title(); ?>" class="fancybox" data-fancybox-group="post-<?php get_the_ID(); ?>" href="<?php echo $image['url']; ?>">
                         <img class="alignleft" src="<?php echo $image['sizes']['thumbnail']; ?>" alt="" />
                    </a>
                    <div class="wpcasa-caption"><?php echo $image['caption']; ?></div>
            <?php endforeach; ?>
        </div>
      <hr>
    <?php endif; ?>
    
    <?php
    endforeach ;
    
    switch_to_blog( $original_blog_id ); // switched back to current blog
    ?>

    Thanks for all your help.
    Ralf

    https://www.ads-software.com/plugins/multilingual-press/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi Ralf,

    I’m sorry, but I don’t understand what you want to achieve. And your code doesn’t make much sense either, to be honest. ??

    You have posts in German, which have additional metadata (image gallery and energy certificate) that seems to be language-independent, and that’s why you want to switch to the German site (or to be more precise: post), fetch the according data, display it, and then switch back and continue with the original (French) post. Is this right so far?

    Kind regards,
    Thorsten

    Thread Starter YtW

    (@ytw)

    Hi Thorsten,

    sorry for being unprecise. What I want to reach is that:

    • Display French Content, e.g. description, price an so on
    • Image gallery: no content found on French site, so switch to German blog
    • Get images from German blog and display them on the French site
    • switch back to French site and display the rest of the French content

    This is the code used on the German site to get the images from my custom field “Image gallery”:

    <?php
    $images = get_field('images');
    
    if( $images ): ?>
    
      <hr>
        <div class="image-gallery clearfix">
            <?php foreach( $images as $image ): ?>
                    <a title="<?php echo get_the_title(); ?>" class="fancybox" data-fancybox-group="post-<?php get_the_ID(); ?>" href="<?php echo $image['url']; ?>">
                         <img class="alignleft" src="<?php echo $image['sizes']['thumbnail']; ?>" alt="" />
                    </a>
                    <div class="wpcasa-caption"><?php echo $image['caption']; ?></div>
            <?php endforeach; ?>
        </div>
      <hr>
    <?php endif; ?>

    The second code mentioned above was just an example what I am planning. I know that this one doesn’t make any sense ??

    Thanks again for your help.
    Ralf

    Hi Ralf,

    I don’t have ACF, so I couldn’t test this, but here’s how it should be done:

    // This is the ID of your "base" site, which is the German one with ID 1.
    $base_site_id = 1;
    
    $switched_site = false;
    
    $translation = null;
    
    $is_base_site = get_current_blog_id() === $base_site_id;
    if ( ! $is_base_site ) {
    	$translations = mlp_get_linked_elements( get_the_ID() );
    	if ( ! empty( $translations[ $base_site_id ] ) ) {
    		switch_to_blog( $base_site_id );
    		$switched_site = true;
    
    		$translation = get_post( $translations[ $base_site_id ] );
    		if ( $translation ) {
    			$GLOBALS['post'] = $translation;
    		}
    	}
    }
    
    if ( $is_base_site || $translation ) {
    
    	// Okay, we either are on the German (base) site, or we are on the French one AND found a valid German translation.
    	// Here, you can also fetch and display the energy certificate...
    
    	$images = get_field( 'images' );
    	if ( $images ) : ?>
    		<hr>
    		<div class="image-gallery clearfix">
    			<?php foreach ( $images as $image ) : ?>
    				<a href="<?php echo esc_url( $image['url'] ); ?>" title="<?php the_title_attribute(); ?>" class="fancybox" data-fancybox-group="post-<?php the_ID(); ?>">
    					<img src="<?php echo esc_url( $image['sizes']['thumbnail'] ); ?>" class="alignleft" alt="">
    				</a>
    				<div class="wpcasa-caption"><?php echo $image['caption']; ?></div>
    			<?php endforeach; ?>
    		</div>
    		<hr>
    	<?php endif;
    }
    
    if ( $switched_site ) {
    	restore_current_blog();
    	wp_reset_postdata();
    }

    I also improved your code: use the_title_attribute() instead of the_title(), escape data (!), use the_ID() instead of get_the_ID(), which doesn’t echo, …

    Kind regards,
    Thorsten

    Thread Starter YtW

    (@ytw)

    Hi Thorsten,

    it seems that we both have written at the same time ?? Thanks for your code example, I will check this one out and reply soon.

    Regards
    Ralf

    Thread Starter YtW

    (@ytw)

    This piece of code works great ??
    Thanks for your help, Thorsten.

    Regards
    Ralf

    Hi Ralf,

    you’re welcome. Glad it worked for you.

    Would you mind writing a short review for MultilingualPress?
    This would mean a lot to us. And with better (and more) reviews, MultilingualPress might get more popular, which in turn means more users, and thus eventually highly motivated developers and supporters. ??

    Best regards,
    Thorsten

    Thread Starter YtW

    (@ytw)

    Hi Torsten,

    done ??
    Keep your good work and your excellent support.

    Regards
    Ralf

    Wow, thanks a lot! ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get content from related post’ is closed to new replies.