如何更改GET_POSTS数组上的类别ID

时间:2020-05-11 作者:AcidBurn

我正在为我的AMP页面制作一个旋转木马,我想重用代码,所以我将其制作成了一个模板,但是,我在更改类别值数组上的ID时遇到了问题,我希望能够插入模板代码,只需选择类别,这样我就可以很好地重用它,而不是像疯子一样复制粘贴它。在过去的几天里,我尝试了一些方法,当代码与旋转木马位于同一页面时,大多数方法都会起作用,然而,当它作为模板时,它会破坏它。这是我用来构建它的代码,我想学习如何更改“category”=>1值。

<div class="category-slider">
<?php
$args = array( \'posts_per_page\' => -1, \'offset\'=> 1, \'category\' => 1);
$pageposts = get_posts( $args ); ?>
    <amp-base-carousel id="cat-carousel-8"
    attr-name="(min-width: 1200px) valueOne, (min-width: 300px) valueTwo, defaultValue"
    layout="responsive"
    width="1200px"
    height="500px"
    visible-count="3"
    advance-count="1" >
        <?php foreach ( $pageposts as $post ) : setup_postdata ( $post ); ?>
        <div layout="flex-item">
        <div class="category-slider-containter">
            <a class="category-slider-image" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
            <h2 class="category-slider-item"><?php the_title(); ?></h2>
                <div class="category-slider-content"><?php the_excerpt(); ?></div>
                <p><a class=\'read-more\' href="<?php the_permalink(); ?>">Read More &raquo;</a></p>
        </div>
        </div>
        <?php 
        endforeach;
        wp_reset_postdata();?>
        <button slot="next-arrow" class="carousel-next btn-next" aria-label="Next"></button>
        <button slot="prev-arrow" class="carousel-prev btn-prev" aria-label="Previous"></button>
    </amp-base-carousel>
</div>

1 个回复
SO网友:Mr Robot

如果您希望更改类别的值,您可以只取一个VARABLE,而不是基于类别取常数值和增量!