首页 > 解决方案 > Wordpress - 使用 qtranslate 获取帖子

问题描述

我有一个 wordpress 网站,在我的新页面中,我只看到主要语言的新闻。如果我更改语言,我将看不到帖子。并且帖子是用qtranslate插件翻译的。

这是我的代码以及我如何获得帖子:

<div class="infinite-scroll">
<?php
   $category = get_field('nome', get_the_ID());
   $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

   $custom_args = array(
                  'post_type' => 'post',
                  'posts_per_page' => 6,
                  'paged' => $paged,
                  'category_name' => $category,
                );

    $articles = new WP_Query( $custom_args );
    if ( $articles->have_posts() ) : while ( $articles->have_posts() ) : $articles->the_post(); // run the loop ?>
      <div class="col-xs-12 col-sm-6 col-md-4">
         <a class="news-link" href="<?php the_permalink() ?>">
          <div class="news">
            <div class="image">
              <?php the_post_thumbnail('thumbnail') ?>
               <div class="mask">
                <div class="icon">
                    <i class="icon-plus"></i>
                 </div>
              </div>

              </div>

             </div>
           </a>
          </div>
      <?php endwhile; ?>
      <?php
         if (function_exists(custom_pagination)) {
           custom_pagination($articles->max_num_pages,"",$paged);
         }
       ?>
       <?php else: ?>
       <article>
         <h1>Sorry...</h1>
         <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        </article>
            <?php endif; ?>
        </div>

使用主要语言,它可以很好地列出帖子,如果我更改为另一种语言,它会返回消息错误: Sorry, no posts matched your criteria.

问题是什么?网站的其他内容以多种语言运行良好。这个问题只发生在新闻上。

标签: phpwordpressqtranslate

解决方案


您从 ACF 字段中获取类别名称,因此如果正在翻译,则可能是问题所在。与其获取名称,不如获取类别的 ID 并使用它(记得将'category_name'​​ $custom_args 更改为 just 'cat'


推荐阅读