最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to change category id on get_posts array

programmeradmin2浏览0评论

I am making a carousel for my AMP page, and I want to re-use the code, so I made it into a template, however, I am having the problem with changing ID on the array for category value, I would like to be able to insert the template code, and just select the category, so I can re-use it nicely, instead of copy-pasting it like a maniac. I tried some things in the last few days, and most of them work when the code is on the same page as the carousel, however, when it's as a template, it breaks it. This is the code I used to build it, and I would like to learn how to change the "category" => 1 values.

<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>

I am making a carousel for my AMP page, and I want to re-use the code, so I made it into a template, however, I am having the problem with changing ID on the array for category value, I would like to be able to insert the template code, and just select the category, so I can re-use it nicely, instead of copy-pasting it like a maniac. I tried some things in the last few days, and most of them work when the code is on the same page as the carousel, however, when it's as a template, it breaks it. This is the code I used to build it, and I would like to learn how to change the "category" => 1 values.

<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>
Share Improve this question asked May 11, 2020 at 17:23 AcidBurnAcidBurn 227 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

If you are looking to change value of categories, you can just take a vairable instead of taking constant value and increment based on category!

发布评论

评论列表(0)

  1. 暂无评论