要在WordPress中调用自定义文章分类中的文章,您可以使用WP_Query或get_posts函数。以下是一些示例代码,假设您有一个名为"customcategory"的自定义文章分类:
使用WP_Query:
$args = array( 'post_type' => 'post', // 文章类型 'posts_per_page' => 1, // 获取所有文章 'tax_query' => array( array( 'taxonomy' => 'customcategory', // 自定义文章分类的名称 'field' => 'slug', // 根据分类别名进行查询 'terms' => 'yourtermslug', // 替换为您要调用的分类别名 ), ), ); $custom_query = new WP_Query($args); if ($custom_query>have_posts()) : while ($custom_query>have_posts()) : $custom_query>the_post(); // 在这里显示文章内容 the_title(); the_content(); endwhile; endif; wp_reset_postdata();
使用get_posts:
$custom_category_posts = get_posts(array( 'post_type' => 'post', // 文章类型 'posts_per_page' => 1, // 获取所有文章 'tax_query' => array( array( 'taxonomy' => 'customcategory', // 自定义文章分类的名称 'field' => 'slug', // 根据分类别名进行查询 'terms' => 'yourtermslug', // 替换为您要调用的分类别名 ), ), )); foreach ($custom_category_posts as $post) : setup_postdata($post); // 在这里显示文章内容 the_title(); the_content(); endforeach; wp_reset_postdata();
确保替换示例代码中的"customcategory"和"yourtermslug"为您的自定义分类名称和术语别名。这将帮助您调用特定分类中的文章。
还没有评论,来说两句吧...