Php 使用 WordPress 如何调出指向两个单独帖子的永久链接

Php with WordPress how to call up permalinks to two separate posts

我正在将一个最近的 posts 功能构建到一个 wordpress 网站中,我可以调用两个不同的特色图片,但它们都链接到相同的 post,可以有人看到我哪里出错了吗?

<?php
        $args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);

$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
    ?>
    <div class="grid_24">
        <div class="grid_12">
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <?php
    echo get_the_post_thumbnail($page->ID, 'medium');
    ?>
    </a>
        </div>
        <div class="grid_12">
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <?php
    echo get_the_post_thumbnail( $post_id,'medium');
    ?>
        </a>
        </div>

    </div>
    <?php

    }
}
    ?>  

您可以使用 echo get_the_permalink($post->ID) 获取帖子的 uri

看来你的情况需要

echo get_the_permalink($post[0]->ID);

echo get_the_permalink($post[1]->ID);

在 href

但是,您最好创建一个 foreach 循环来遍历来自 get_posts 函数的帖子

https://developer.wordpress.org/reference/functions/get_the_permalink/

https://developer.wordpress.org/reference/functions/get_posts/

好吧,首先,你没有循环你所做的查询(例如 $posts = get_posts( $args ); )你只是显示第一个 post的缩略图和当前页面的缩略图。

你需要像这样循环 post :

<?php
$args = array(
    'posts_per_page' => 2,
    'order_by' => 'date',
    'order' => 'desc'
);

$posts = get_posts( $args );
?>

<?php if ( !empty( $posts ) ) :?>
    <div class="grid_24">
        <?php foreach ( $posts as $post ) : ?>\
            <?php if( has_post_thumbnail( $post->ID ) ) ?>
                <div class="grid_12">
                    <a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
                        <?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
                    </a>
                </div>
            <?php endif; ?>
        <?php endforeach?>
    </div>
<?php endif;