WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Querying page - how to display page excerpt in the correct language (6 posts)

  1. wodieskodie
    Member
    Posted 1 year ago #

    Hello,

    First off, I think Polylang is brilliant. I am using it to develop GĂ idhlig language websites.

    I have pretty permalinks enabled and am running a static homepage on a custom template. All is working well. Apart from one thing.

    I am using the following code to publish the excerpt of the "about us" page on the static homepage. It is working perfectly in English, because I am querying that exact page. However, I can't work out how to write the query in order to pull the extract of the page depending on which language is selected. The ID of the gd page is 26. I guess I am missing something obvious, but I have no idea what it is.

    Thanks in advance

    <?php query_posts( 'page_id=27' );?>
        <?php while ( have_posts() ) : the_post(); ?>
            <p><?php the_excerpt(); ?></p>
    		<p><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >>></a></p>
        <?php endwhile; ?>
    <?php wp_reset_query(); ?>

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    You have at least two choices:

    $page_id = pll_current_language() == 'gd' ? 26 : 27;

    or:

    $page_id = pll_get_post(27); // automatically gets the right translation

    don't forget to test if functions exists (in case Polylang is deactivated).

  3. wodieskodie
    Member
    Posted 1 year ago #

    Wow! Thank you for the quick response.

    So, I now have

    <?php query_posts( $page_id = pll_get_post(27)) ;?>	
    
        <?php while ( have_posts() ) : the_post(); ?>
            <p><?php the_excerpt(); ?></p>
    		<p><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >>></a></p>
        <?php endwhile; ?>
    <?php wp_reset_query(); ?>

    Which is pulling the correct language, but not page id 27 - in fact it is pulling all posts...

    I checked that the function existed.

    Apologies. Getting into functions in this depth is new to me...

  4. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    In fact, you should write:

    query_posts( 'page_id=' . pll_get_post(27));
  5. wodieskodie
    Member
    Posted 1 year ago #

    Superb. That's it! Thank you very much for your help. I hope this plugin goes from strength to strength - it deserves to!

    Code in case anyone else has the same issue:

    <?php query_posts( 'page_id=' . pll_get_post(27));?>	
    
        <?php while ( have_posts() ) : the_post(); ?>
            <p><?php the_excerpt(); ?></p>
    		<p><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >>></a></p>
        <?php endwhile; ?>
    <?php wp_reset_query(); ?>
  6. imagevo
    Member
    Posted 8 months ago #

    Thank you Wodleskodie for your feedback and Chouby to help!
    I needed this function to display the content of a post and it works perfectly.
    Yes, Polylang is a great plugin.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic