【问题标题】:How can I get have_posts() to look for posts in all languages?如何让 have_posts() 查找所有语言的帖子?
【发布时间】:2020-03-11 14:21:05
【问题描述】:

我们有一个多语言网站,其中包含多种不同语言的帖子。如何以作者发表的所有语言显示所有作者帖子的摘录? have_posts() 似乎只从网站的默认语言中提取。这是我目前在自定义 author.php 模板中的内容:

<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
...

<?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?>                       
            <?php get_template_part( 'content', 'index' ); ?>
        <?php endwhile; ?>
            <?php else : ?>
        <p><i><?php esc_html_e( 'This person has not authored any articles.' ); ?></i></p>
        <?php endif; ?>

例如,仅以法语发表的作者的 var_dump 返回 FALSE。

我也尝试过使用 get_posts(),但正如您所见,这需要指定语言,并且我希望能够以任何语言提取帖子。对此的摘录也无法正常工作。

    <?php 
                $args = array( 'post_type' => 'post', 'order' => 'DESC', 'author' => $curauth->ID, 'lang' => 'fr', 'post_status' => 'publish');
                $posts = get_posts($args);
                if (!empty($posts)) {
                    foreach($posts as $post => $post_val){ ?>
                        <h1 class="entry-title textcenter-xs"><?php echo $post_val->post_title ?></h1>
                    <?php
                        echo wp_trim_excerpt($post_val->post_content); //This is returning ALL the content, not the excerpt
                    }
                } 
                ?>

我还是 php 的新手,所以任何帮助表示赞赏!

【问题讨论】:

    标签: php wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    通过删除 'lang' =&gt; 'fr' 参数,您应该得到一个显示所有可用帖子的循环。 GL.

    【讨论】:

      猜你喜欢
      • 2014-10-03
      • 1970-01-01
      • 2019-12-08
      • 2019-04-28
      • 2014-01-19
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2020-02-18
      相关资源
      最近更新 更多