admin管理员组文章数量:1794759
wordpress站外调用某个分类下的6个随机文章
要在WordPress中调用所有页面并排除指定的ID,您可以使用`WP_Query`类。以下是一个示例代码,它将查询所有页面并排除指定的页面ID:
代码语言:javascript代码运行次数:0运行复制<?php
// Page ID array to exclude
$exclude_ids = array( 6, 66, 666 ); //Replace here with the page ID you want to exclude
//Create a new WP_Query object
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post__not_in' => $exclude_ids,
);
$custom_query = new WP_Query($args);
// 开始循环
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
// wodepress Write your loop content here, for example:
echo '<h2>' . get_the_title() . '</h2>';
echo '<div>' . get_the_content() . '</div>';
endwhile;
endif;
//Reset article data
wp_reset_postdata();
?>
将上述代码添加到您的主题模板文件(如`page.php`、`index.php`等)中,或者创建一个自定义模板文件并在其中使用这段代码。这段代码将查询所有页面并排除ID为5、10和15的页面。您可以根据需要修改`$exclude_ids`数组中的ID。
请注意,如果您使用的是自定义查询,确保在使用完查询后调用`wp_reset_postdata()`函数,以便恢复全局`$post`变量。
原文
/?p=7980
本文标签: wordpress站外调用某个分类下的6个随机文章
版权声明:本文标题:wordpress站外调用某个分类下的6个随机文章 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1755011743a1709397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论