• Hi Guys,

    I’m still new with WordPress and I have the following issue. I bought a theme in which I had a shortcode to display blog post on the home page. Problem is, it uses IDs to identify Post and my site is bilingual (EN & FR), therefore I have all post in all language on each homepage.

    Is there a way to identify the language within the shortcode so it displays EN post on EN homepage and FR post on FR homepage?

    Here’s the code :

    function trend_show_blog_post_shortcode( $params, $content ) {
        extract( shortcode_atts(
            array(
                'number'       => '',
                'columns'    => ''
               ), $params ) );
        $args_posts = array(
                'posts_per_page'        => $number,
                'post_type'             => 'post',
                'post_status'           => 'publish'
            );
        $posts = get_posts($args_posts);
        $shortcode_content = '<div class="trend_shortcode_blog vc_row sticky-posts">';
        foreach ($posts as $post) {
            $excerpt = get_post_field('post_content', $post->ID);
            $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'portfolio_pic700x450' );
            $author_id = $post->post_author;
            $url = get_permalink($post->ID); /* ISSUE HERE SINCE ID DIFFERENT EACH LANG */
            $shortcode_content .= '<div class="'.$columns.' post">';
                $shortcode_content .= '<a href="'.$url.'" class="relative">';
                    if($thumbnail_src) {
                        $shortcode_content .= '<img src="'. $thumbnail_src[0] . '" alt="'. $post->post_title .'" />';
                    }else{
                        $shortcode_content .= '<img src="https://placehold.it/700x450" alt="'. $post->post_title .'" />';
                    }
                    $shortcode_content .= '<div class="post-date absolute rotate45">';
                        $shortcode_content .= '<span class="rotate45_back">'.get_the_date( "j M" ).'</span>';
                    $shortcode_content .= '</div>';
                    $shortcode_content .= '<div class="thumbnail-overlay absolute">';
                        $shortcode_content .= '<i class="fa fa-plus absolute"></i>';
                    $shortcode_content .= '</div>';
                $shortcode_content .= '</a>';
                $shortcode_content .= '<h3 class="post-name"><a href="'.$url.'">'.$post->post_title.'</a></h3>';
                $shortcode_content .= '<div class="post-author">by '.get_the_author_meta( 'display_name', $author_id ).'</div>';
                $shortcode_content .= '<div class="post-excerpt">'.trend_excerpt_limit($excerpt,10).'</div>';
            $shortcode_content .= '</div>';
        }
        $shortcode_content .= '</div>';
        return $shortcode_content;
    }
    add_shortcode('trend-blog-posts', 'trend_show_blog_post_shortcode');

    Thanks in advance.

    JP

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m sure there’s a way, but I don’t know how your bilingual site manages languages, so I cannot be too specific. First, I suggest you create your own bilingual version of your theme’s shortcode so that if your theme is updated you will not lose your modifications. You can place your version in a simple plugin you create for your site. This plugin can contain any other little mods you happen to come up with as well.

    Copy the theme’s shortcode handler function to your plugin’s file and assign a new name to it. Register your custom shortcode with add_shortcode(). The shortcode name itself needs to be different too. You’ll need to replace any occurrences of the original shortcode on existing pages with your new shortcode.

    The issue isn’t so much the IDs as the query that fetches the IDs from the DB. You can modify the query by editing the array assigned to $args_posts. This array can accept any WP_Query argument, you’ll need to add whatever criteria is used to distinguish languages.

    Naturally, this language criteria needs to be assigned whatever language the page is using. This is probably the same for pages as for posts. For example, if the post language is kept in a custom field, you should be able to get the page’s language from its own custom field. Assuming that the page language is in the custom field ‘language’, the $args_posts array assignment might look like this:

    global $post;
    $args_posts = array(
                'posts_per_page'        => $number,
                'post_type'             => 'post',
                'post_status'           => 'publish',
                'meta_key'              => 'language',
                'meta_value'            =>  get_post_meta( $post->ID, 'language', true),
            );

    Note that the $post->ID here is from the global $post object of the current page, not the $post used to loop through posts. Because $post was declared global in my example, the foreach loop using $post as well corrupts the global value, which may still be required in the page’s main loop. It’d be best to use a different variable through out the foreach loop. Alternately, call wp_reset_postdata() after the loop concludes.

    This solution is of course entirely speculative, but it illustrates the general approach you need to use. You’ll need to adapt it to however your site handles languages. Good luck!

    Thread Starter stdesigner

    (@jplavoie)

    Thanks for the reply, much appreciated. I’ll follow your suggestion of making a double of the theme shortcode and try to modify the query as suggested.

    FYI, I’m using WPML for translation if that’s of any help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcode mod request – Display Post by language on homepage’ is closed to new replies.