也想出现在这里?联系我们

WordPress调用相同文章类型文章列表

2024.02.25 wordpress教程
  • 文章介绍
  • 升级版本
  • 评价&建议

要在WordPress中调用相同文章类型的文章列表,您可以使用WP_Query或get_posts函数来完成这个任务。以下是一个使用WP_Query的示例:

<?php
$args = array(
    'post_type' => 'your_post_type', // 替换为您的文章类型名称
    'posts_per_page' => 1, // 如果要显示所有相关文章,请将此值设置为1
);

$related_posts = new WP_Query($args);

if ($related_posts>have_posts()) :
    while ($related_posts>have_posts()) : $related_posts>the_post();
        // 在这里输出相关文章的内容
        the_title();
        the_content();
    endwhile;
    wp_reset_postdata();
else :
    // 如果没有相关文章
    echo '没有相关文章';
endif;
?>

确保将'your_post_type'替换为您实际的文章类型名称。这段代码将列出与指定文章类型相同的所有文章。

如果您只需要获取文章的信息而不需要自定义查询,您还可以使用get_posts函数:

<?php
$args = array(
    'post_type' => 'your_post_type', // 替换为您的文章类型名称
    'posts_per_page' => 1, // 如果要显示所有相关文章,请将此值设置为1
);

$related_posts = get_posts($args);

foreach ($related_posts as $post) :
    setup_postdata($post);
    // 在这里输出相关文章的内容
    the_title();
    the_content();
endforeach;

wp_reset_postdata();
?>

这两种方法都可以用来调用相同文章类型的文章列表,具体取决于您的需求和代码结构。

有用0
  • 2024.02.25初次和大家见面了!

等待您对该主题的建议

发表评论

还能输入240个字

Hi, 欢迎加入Wordpress技术交流群,带你装逼带你飞!

我要入群
也想出现在这里?联系我们
wordpress加速

我来推荐一个更牛逼的给你看看?

  • 猛戳我吧