如何在wordpress博客的单页中显示所有帖子的精选图像

问题描述 投票:0回答:2

我正在制作一个WordPress主题,现在我想在一个名为gallery的页面中显示每个帖子的精选图像,并应根据帖子的类别进行排序。

wordpress custom-wordpress-pages
2个回答
0
投票

将以下代码添加到模板文件中,并根据需要进行更改。

<?php
//loop through category
$cat_args=array(
    'orderby' => 'name',
    'order' => 'ASC'
);
$categories=get_categories($cat_args);
foreach($categories as $category) { 
    //loop through posts of category
    $args=array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'category__in' => array($category->term_id)
    );
    $posts=get_posts($args);
    if ($posts) {
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '">' . $category->name.'</a> </p> ';
        foreach($posts as $post) {
            setup_postdata($post);
            // if only featured image set
            if ( has_post_thumbnail() ) {
            ?>
            <a href="<?php the_permalink() ?>"><img src="<?php the_post_thumbnail_url('full'); ?>" title="<?php the_title(); ?>" alt="<?php the_title(); ?>" /><br /><?php the_title(); ?></p>
            <?php
            }
        }
    }
}
?>

0
投票

以下代码仅显示特色图像(称为the_post_thumbnail()):

    <?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        the_post_thumbnail();
    }  //end while
} //end if
?>
© www.soinside.com 2019 - 2024. All rights reserved.