在WordPress 6.4上的页面中显示来自特定标签的博客文章?

hgc7kmma  于 5个月前  发布在  WordPress
关注(0)|答案(1)|浏览(87)

我怎样才能得到所有的博客文章标记为说徒步旅行,并显示在一个我最近的远足页面在网格显示?

<?php $args = array(
    'post_status' => 'publish',
    'tag' => 'hiking'
);

$recent_posts = get_posts($args);?>

字符串
或者有什么方法可以处理WordPress 6.4的查询循环块?

8ehkhllq

8ehkhllq1#

在WordPress中,您可以通过使用WP_Query类来创建自定义查询或使用Query Loop块(如果您使用的是WordPress 5.8或更高版本)来实现这一点。下面是使用这两种方法的示例:

方法一:使用WP_Query:

<?php
$args = array(
    'post_status' => 'publish',
    'tag' => 'hiking',
    'posts_per_page' => -1, // Set to -1 to retrieve all posts
);

$recent_posts = new WP_Query($args);

if ($recent_posts->have_posts()) :
    while ($recent_posts->have_posts()) : $recent_posts->the_post();
        // Display your post content here
        ?>
        <div class="grid-item">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <div class="post-content">
                <?php the_excerpt(); ?>
            </div>
        </div>
        <?php
    endwhile;
    wp_reset_postdata(); // Reset the post data
else :
    echo 'No posts found';
endif;
?>

字符串

**方法二:使用查询循环块(WordPress 5.8或更高版本):**创建新页面或编辑现有页面。添加查询循环块:在块编辑器中,添加一个新的块,搜索“Query Loop”,并将“Query Loop”块添加到您的页面中。在块设置中,您可以指定查询参数,包括标签'hiking'。配置块以显示所需的内容。此块将根据指定的查询动态获取和显示帖子参数,包括“hiking”标签。

选择最适合您的需求和您使用的WordPress版本的方法。方法1为您提供更多的控制和灵活性,而方法2更用户友好,如果您想通过块编辑器管理内容,可能会首选。

相关问题