如何在WPML当前语言的基础上获得创新主题的近期帖子?

时间:2016-02-03 09:59:16

标签: wordpress wordpress-theming

我正在使用“创新”主题和“WPML”插件。我希望根据当前语言获得最新帖子。

我尝试了this solution,但它不能与我合作。

2 个答案:

答案 0 :(得分:0)

我找到了名为“wpml-recent-posts-by-language”的插件,并获取其SQL代码并获取此代码:

    <?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 3/4/15
 * Time: 2:41 PM
 */
if (!defined('ABSPATH'))
    die('-1');
if (!class_exists('g5plusFramework_Shortcode_Post')):

    class g5plusFramework_Shortcode_Post {

        function __construct() {
            add_shortcode('innovation_post', array($this, 'post_shortcode'));
        }

        function post_shortcode($atts) {
            $display = $column = $is_slider = $item_amount = $html = $el_class = $g5plus_animation = $css_animation = $duration = $delay = $styles_animation = '';
            extract(shortcode_atts(array(
                'display' => '',
                'item_amount' => '',
                'is_slider' => false,
                'column' => '',
                'el_class' => '',
                'css_animation' => '',
                'duration' => '',
                'delay' => ''
                            ), $atts));

            $g5plus_animation .= ' ' . esc_attr($el_class);
            $g5plus_animation .= g5plusFramework_Shortcodes::get_css_animation($css_animation);

            $query['posts_per_page'] = $item_amount;
            $query['no_found_rows'] = true;
            $query['post_status'] = 'publish';
            $query['ignore_sticky_posts'] = true;
            $query['post_type'] = 'post';

            $query['tax_query'] = array(
                array(
                    'taxonomy' => 'post_format',
                    'field' => 'slug',
                    'terms' => array('post-format-quote', 'post-format-link', 'post-format-audio'),
                    'operator' => 'NOT IN'
                )
            );
            if ($display == 'random') {
                $query['orderby'] = 'rand';
            } elseif ($display == 'popular') {
                $query['orderby'] = 'comment_count';
            } elseif ($display == 'recent') {
                $query['orderby'] = 'post_date';
                $query['order'] = 'DESC';
            } else {
                $query['orderby'] = 'post_date';
                $query['order'] = 'ASC';
            }




            global $wpdb;
// empty array to put the IDS of Recent Posts based on WPML current language 
            $latest_post_ids = array();

            $querystr = "
          SELECT wposts.*
          FROM $wpdb->posts wposts, wp_icl_translations icl_translations
          WHERE wposts.ID = icl_translations.element_id
          AND icl_translations.language_code = '" . ICL_LANGUAGE_CODE . "'
          AND wposts.post_status = 'publish'
          AND wposts.post_type = 'post'
          ORDER BY wposts.post_date DESC LIMIT " . intval($item_amount) . "

        ";

            $results = $wpdb->get_results($querystr);
//foreach throw $results and fill of latest_post_ids with IDS
            foreach ($results as $result) {  
                array_push($latest_post_ids, $result->ID);
            }


// using post__in to pass array of selected IDs
            $query['post__in'] = $latest_post_ids;

            $r = new WP_Query($query);
            ob_start();
            $class_col = 'col-lg-' . (12 / esc_attr($column)) . ' col-md-' . (12 / esc_attr($column)) . ' col-sm-6  col-xs-12';
            if ($r->have_posts()) :
                ?>
                <div class="innovation-latest-post <?php echo esc_attr($g5plus_animation) ?>" <?php echo g5plusFramework_Shortcodes::get_style_animation($duration, $delay); ?>>
                <?php if ($is_slider) : ?>
                        <div class="innovation-latest-post-slider">
                            <div data-plugin-options='{"items" : <?php echo esc_attr($column) ?>,"pagination":false, "autoPlay": true }' class="owl-carousel">
                        <?php while ($r->have_posts()) : $r->the_post(); ?>
                                    <div class="latest-post-item">
                        <?php
                        $thumbnail = g5plus_post_thumbnail('blog-grid');
                        if (!empty($thumbnail)) :
                            ?>
                                            <div class="innovation-latest-post-image">
                                            <?php echo wp_kses_post($thumbnail); ?>
                                            </div>
                                            <?php endif; ?>
                                        <div class="innovation-latest-post-content">
                                            <a class="innovation-latest-post-title" href="<?php the_permalink(); ?>"
                                               rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                        <?php
                        $excerpt = get_the_excerpt();
                        $excerpt = g5plusFramework_Shortcodes::substr($excerpt, 180, ' [...]');
                        ?>
                                            <p><?php echo($excerpt); ?></p>
                                            <div class="post-meta-line"></div>
                                            <div class="innovation-latest-post-author"><?php _e("By : ", 'g5plus-framework'); ?><span><?php printf('<a href="%1$s">%2$s</a>', esc_url(get_author_posts_url(get_the_author_meta('ID'))), esc_html(get_the_author())); ?></span></div>|
                                            <a class="innovation-latest-post-readmore" href="<?php the_permalink(); ?>"><?php _e("Read more", 'g5plus-framework'); ?></a>
                                        </div>
                                    </div>
                    <?php endwhile; ?>
                            </div>
                        </div>
                            <?php else: ?>
                        <div class="row">
                        <?php while ($r->have_posts()) : $r->the_post(); ?>
                                <div class="<?php echo esc_attr($class_col); ?>">
                                    <div class="latest-post-item">
                                <?php
                                $thumbnail = g5plus_post_thumbnail('blog-grid');
                                if (!empty($thumbnail)) :
                                    ?>
                                            <div class="innovation-latest-post-image">
                                            <?php echo wp_kses_post($thumbnail); ?>
                                            </div>
                                            <?php endif; ?>
                                        <div class="innovation-latest-post-content">
                                            <a class="innovation-latest-post-title" href="<?php the_permalink(); ?>"
                                               rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                        <?php
                        $excerpt = get_the_excerpt();
                        $excerpt = g5plusFramework_Shortcodes::substr($excerpt, 180, ' [...]');
                        ?>
                                            <p><?php echo($excerpt); ?></p>
                                            <div class="post-meta-line"></div>
                                            <div class="innovation-latest-post-author"><?php _e("By : ", 'g5plus-framework'); ?><span><?php printf('<a href="%1$s">%2$s</a>', esc_url(get_author_posts_url(get_the_author_meta('ID'))), esc_html(get_the_author())); ?></span></div>|
                                            <a class="innovation-latest-post-readmore" href="<?php the_permalink(); ?>"><?php _e("Read more", 'g5plus-framework'); ?></a>
                                        </div>
                                    </div>
                                </div>
                    <?php endwhile; ?>
                        </div>
                        <?php endif; ?>
                </div>
                    <?php
                endif;
                wp_reset_postdata();
                g5plus_archive_loop_reset();
                $content = ob_get_clean();
                return $content;
            }

        }

        if (!function_exists('init_post_shortcode')) {

            function init_post_shortcode() {
                new g5plusFramework_Shortcode_Post();
            }

            init_post_shortcode();
        }
endif;

对于这个问题,任何人都有更好的解决方案吗?

答案 1 :(得分:0)

如果无效,请添加:

var editTag: String!

之前:

global $sitepress;
$sitepress->switch_lang(ICL_LANGUAGE_CODE);