• barbje

    (@barbje)


    hello,

    on the homepage near the bottom shows latest posts, but i want the script recognise the language the visitor is using in order to show the correct title and buttontext.
    now i typed all 3 languages on 1 row as u can see

    the script behind this is:

    <?php 
    
    // WP_Query arguments
    $args = array (
    	'post_type'              => array( 'post' ),
    	'posts_per_page'         => '1',
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post(); ?>
    			<div class="main-content-inner col-md-7">
    				<div class="home-block animate">
    				<hr class="b1">
    				<h2><?php echo pll__('Nieuws/News/Nouvelles'); ?></h2>
    				
    				
    				<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    				<p><?php the_excerpt(); ?></p>
    				<a href="<?php if( get_option( 'show_on_front' ) == 'page' ) echo get_permalink( get_option('page_for_posts' ) );
    else echo bloginfo('url');?>" class="btn"><?php echo pll__('Meer Nieuws/More News/Encore des Nouvelles'); ?></a>
    				</div>
    			</div>
    
    <?php 	}
    } else {
    	// no posts found
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    
     ?>

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • corrinarusso

    (@corrinarusso)

    What plugin are you using to create and toggle the language?

    Thread Starter barbje

    (@barbje)

    i have polylang as plugin

    corrinarusso

    (@corrinarusso)

    Seems hacky to me, but you can get the current language using pll_current_language like this :
    https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/

    Then pass the language parameter as an argument to your WP_Query, like this :
    https://codepad.co/snippet/wordpress-polylang-wp-query-by-language

    And end up with something like:

    // get current lang 
    $langis=pll_current_language($value);
    
    // WP_Query arguments
    $args = array (
    	'post_type'              => array( 'post' ),
    	'posts_per_page'         => '1',
    	'lang'					 => $langis;
    );

    Or you could just try :
    'lang' => pll_current_language($value);

    Thread Starter barbje

    (@barbje)

    thx i am trying to implement it but no luck so far
    so any help is appreciated

    here is the full content of the page-home.php

    <?php
    /**
     * Template Name: Homepage
     */
    
    get_header(); ?>
    
    <section class="slider">
    	<?php if (get_field('slider')): ?>
    		<?php the_field('slider'); ?>
    	<?php else: ?>
    		<img src="<?php echo get_template_directory_uri(); ?>/assets/slider.jpg" alt="">
    	<?php endif; ?>
    	
    </section> 
    
    <div class="main-content">
    	<div class="container">
    		<div class="row">
    			<div class="main-content-inner col-sm-12">
    
    	<div class="row">
    		<div class="col-md-6">
    			
    			<?php 
    				$foto1 = get_field('foto_1'); 
    				$alt = $foto1['alt'];
    				$thumb1 = $foto1['sizes'][ 'regular' ];
    			 ?>
    			<?php if ($foto1): ?>
    			<div class="animate">
    				<hr class="b1">
    				<img src="<?php echo $thumb1; ?>" alt="<?php echo $alt; ?>">
    			</div>
    			<?php endif; ?>
    			
    			<div class="bbg2 hblock2 home-block animate module ">
    				<hr class="b1">
    				<?php the_field('block_2'); ?>
    			</div>
    		</div>
    		<div class="col-md-6">
    			<div class="bbg1 hblock1 home-block animate ">
    				<hr class="b1">
    				<?php the_field('block_1'); ?>
    			</div>
    		</div>
    	</div>
    
    			</div><!-- close .*-inner (main-content or sidebar, depending if sidebar is used) -->
    		</div><!-- close .row -->
    	</div><!-- close .container -->
    </div><!-- close .main-content -->
    
    			<?php 
    				$foto2 = get_field('foto_2'); 
    				$alt = $foto2['alt'];
    				$thumb2 = $foto2['sizes'][ 'home-bg' ];
    			 ?>
    <div class="main-content parallax white"  style="background-image:url(<?php echo $thumb2; ?>);">
    	<div class="container">
    		<div class="row">
    			<div class="main-content-inner col-md-6 col-md-offset-4">
    				<div class="bbg3 hblock3 home-block animate ">
    					<hr class="b1">
    					<?php the_field('block_3'); ?>
    				</div>
    					
    			</div><!-- close .*-inner (main-content or sidebar, depending if sidebar is used) -->
    		</div><!-- close .row -->
    	</div><!-- close .container -->
    </div><!-- close .main-content -->
    
    <div class="main-content">
    	<div class="container">
    
    <?php 
    
    // WP_Query arguments
    $args = array (
    	'post_type'              => array( 'post' ),
    	'posts_per_page'         => '1',
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post(); ?>
    			<div class="main-content-inner col-md-7">
    				<div class="home-block animate">
    				<hr class="b1">
    				<h2><?php echo pll__('Nieuws/News/Nouvelles'); ?></h2>
    				
    				
    				<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    				<p><?php the_excerpt(); ?></p>
    				<a href="<?php if( get_option( 'show_on_front' ) == 'page' ) echo get_permalink( get_option('page_for_posts' ) );
    else echo bloginfo('url');?>" class="btn"><?php echo pll__('Meer Nieuws/More News/Encore des Nouvelles'); ?></a>
    				</div>
    			</div>
    
    <?php 	}
    } else {
    	// no posts found
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    
     ?>
    
    <?php get_footer(); ?>
    corrinarusso

    (@corrinarusso)

    But I don’t see where you have entered in the argument into the wp_query?

    It should be like this :

    <code style="max-height: none; height: 2070px;"><?php
    /**
     * Template Name: Homepage
     */
    
    get_header(); ?>
    
    <section class="slider">
    	<?php if (get_field('slider')): ?>
    		<?php the_field('slider'); ?>
    	<?php else: ?>
    		<img src="<?php echo get_template_directory_uri(); ?>/assets/slider.jpg" alt="">
    	<?php endif; ?>
    	
    </section> 
    
    <div class="main-content">
    	<div class="container">
    		<div class="row">
    			<div class="main-content-inner col-sm-12">
    
    	<div class="row">
    		<div class="col-md-6">
    			
    			<?php 
    				$foto1 = get_field('foto_1'); 
    				$alt = $foto1['alt'];
    				$thumb1 = $foto1['sizes'][ 'regular' ];
    			 ?>
    			<?php if ($foto1): ?>
    			<div class="animate">
    				<hr class="b1">
    				<img src="<?php echo $thumb1; ?>" alt="<?php echo $alt; ?>">
    			</div>
    			<?php endif; ?>
    			
    			<div class="bbg2 hblock2 home-block animate module ">
    				<hr class="b1">
    				<?php the_field('block_2'); ?>
    			</div>
    		</div>
    		<div class="col-md-6">
    			<div class="bbg1 hblock1 home-block animate ">
    				<hr class="b1">
    				<?php the_field('block_1'); ?>
    			</div>
    		</div>
    	</div>
    
    			</div><!-- close .*-inner (main-content or sidebar, depending if sidebar is used) -->
    		</div><!-- close .row -->
    	</div><!-- close .container -->
    </div><!-- close .main-content -->
    
    			<?php 
    				$foto2 = get_field('foto_2'); 
    				$alt = $foto2['alt'];
    				$thumb2 = $foto2['sizes'][ 'home-bg' ];
    			 ?>
    <div class="main-content parallax white"  style="background-image:url(<?php echo $thumb2; ?>);">
    	<div class="container">
    		<div class="row">
    			<div class="main-content-inner col-md-6 col-md-offset-4">
    				<div class="bbg3 hblock3 home-block animate ">
    					<hr class="b1">
    					<?php the_field('block_3'); ?>
    				</div>
    					
    			</div><!-- close .*-inner (main-content or sidebar, depending if sidebar is used) -->
    		</div><!-- close .row -->
    	</div><!-- close .container -->
    </div><!-- close .main-content -->
    
    <div class="main-content">
    	<div class="container">
    
    <?php 
    
    // WP_Query arguments
    $args = array (
    	'post_type'              => array( 'post' ),
    	'posts_per_page'         => '1',
    	'lang' => pll_current_language($value),
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post(); ?>
    			<div class="main-content-inner col-md-7">
    				<div class="home-block animate">
    				<hr class="b1">
    				<h2><?php echo pll__('Nieuws/News/Nouvelles'); ?></h2>
    				
    				
    				<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    				<p><?php the_excerpt(); ?></p>
    				<a href="<?php if( get_option( 'show_on_front' ) == 'page' ) echo get_permalink( get_option('page_for_posts' ) );
    else echo bloginfo('url');?>" class="btn"><?php echo pll__('Meer Nieuws/More News/Encore des Nouvelles'); ?></a>
    				</div>
    			</div>
    
    <?php 	}
    } else {
    	// no posts found
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    
     ?>
    
    <?php get_footer(); ?></code>
    Thread Starter barbje

    (@barbje)

    thx Corrina

    but what about this line <h2><?php echo pll__(‘Nieuws/News/Nouvelles’); ?></h2>

    how does he get the right echo to display on the page ?
    as if i only leave ‘News’ on this line he only displays News on every language

    thx

    corrinarusso

    (@corrinarusso)

    I’ve never used Polylang – I thought that was an example in your code.

    Did you already run the string translation for everything like this:
    https://polylang.pro/doc/strings-translation/

    Then you just need to echo like this :
    pll_e( $string );

    See:
    https://polylang.pro/doc/function-reference/#pll_register_string

    Thread Starter barbje

    (@barbje)

    i implemented your code but it still does not change the language
    can u check pls ?

    thx

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘how to specify language in script’ is closed to new replies.