带有类别的WP查询只显示一个帖子,并忽略该类别

时间:2018-01-31 作者:Andrea G

我正试图用WordPress制作以下模型:https://imgur.com/a/Sem7y

我创建了一个自定义帖子类型,并创建了两个类别(常规和填充说明)。我使用ACF插件为帖子类型创建自定义字段:https://imgur.com/a/o2QJV

然而,当我创建循环时,我无法让它仅显示一般类别的帖子,它将显示任何类别。此外,它只显示一个帖子(最近的),我希望它显示一个类别的所有帖子。

        <ul class="pdfLinks">
                <?php

                    //The Arguments
                    $args = array(
                        \'posts_per_page\' => 50,
                        \'post_type\' => \'documents\',
                        \'category_name\' => \'general\'
                    );

                    //The Query
                    $genral_documents = new WP_Query ( $args ); ?>

                <?php

                    //If we have the posts...
                    if (the_field (\'title\')) :
                        while(the_field(\'title\')) :
                            the_field(\'title\');
                    endwhile;
                    endif;
                ?>
                    <?php
                        $pdf1 = get_field(\'pdf_1\');
                    ?>
                        <a class="download_button" target="_blank" href="<?php echo $pdf1[\'url\']; ?>">
                            <li>
                                Download File
                            </li>
                        </a>
                </ul> 
我对WordPress的编码非常陌生,所以我不确定我做错了什么。感谢您的帮助:)

2 个回复
SO网友:Galgóczi Levente

尝试以下操作:

`

                $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;

                $genral_args = array(
                    \'posts_per_page\' => \'50\',
                    \'post_type\' => \'documents\',
                    \'cat\' => \'your-category-id\',
                    \'paged\' => $paged
                );

                $genral_query = new WP_Query( $genral_args );

                while ( $genral_query->have_posts() ) : $genral_query->the_post(); ?>

            <?php

                //If we have the posts...
                if (the_field (\'title\')) :
                    while(the_field(\'title\')) :
                        the_field(\'title\');
                endwhile;
                endif;
            ?>
                <?php
                    $pdf1 = get_field(\'pdf_1\');
                ?>
                    <a class="download_button" target="_blank" href="<?php echo $pdf1[\'url\']; ?>">
                        <li>
                            Download File
                        </li>
                    </a>

                    <?php endwhile; wp_reset_postdata(); ?>
            </ul> `
(将“您的类别id”更改为id,或者如果您想选择更多类别,请使用:\'cat\' => \'your-category-id, your-category-id2\')

SO网友:Babulal N Choudhary

尝试以下操作:

<?php
$taxonomy="category";
$postData = new WP_Query(
  array(
    \'post_type\' => \'documents\',
    \'posts_per_page\'=> 50,
    \'post_status\' => \'publish\',
    \'tax_query\' => array(
      array(
        \'taxonomy\' => $taxonomy, // your custom taxonomy name here
        \'field\' => \'slug\',  // you can specify field like id,name and slug.
        \'terms\' => \'general\' // you can specify category id, name and slug
      )
  ) 
));
if($postData->have_posts()):
  while ($postData->have_posts()): $postData->the_post();
    echo the_title()."<br>";
    $pdf1 = get_field(\'pdf_1\'); ?>

    <a class="download_button" target="_blank" href="<?php echo $pdf1[\'url\']; ?>">
      <li>
          Download File
      </li>
    </a>
  <?php endwhile;
  wp_reset_postdata();
endif; ?>
谢谢

结束

相关推荐

从gensis_loop中删除gensis_404挂接

我正在使用genesis主题,我想定制一个404页面。为此,我希望删除默认的404钩子并实现我自己的钩子。在我的插件函数中。php我添加了我的操作:add_action( \'genesis_loop\', \'genesis_customizations_404\' ); 在函数中,我添加了删除:function genesis_customizations_404() { echo \'test\'; remove_action(\'genesis_loop\',