gpt4 book ai didi

wordpress - 通过自定义分类类型遍历自定义帖子类型? (按类别订购wordpress帖子,或按分类术语显示自定义帖子类型)

转载 作者:行者123 更新时间:2023-12-04 03:13:32 30 4
gpt4 key购买 nike

我想要一个显示所有帖子的页面,按类别分开。想法是获取类别,然后遍历每个类别的所有帖子。我想使用自定义分类法作为类别遍历给定自定义类型的所有帖子,从而使问题变得复杂。 (运行Wordpress 3)

在我的functions.php中,我的自定义帖子类型注册为“视频”,而自定义分类法注册为“video_types”。

在我的自定义页面模板中,该模板应该显示按类别排列的所有视频,这是不返回任何帖子的代码(我检查了它们的位置):

<?php 
$categories = get_categories(array(
'taxonomy' => 'video_types'
));
foreach ($categories as $cat):
?>
<section id="<?php $cat->slug ?>" class="video-category">
<?php
query_posts(array(
'cat' => $cat->cat_ID,
'posts_per_page' => -1
));
?>
<h2><?php single_cat_title(); ?></h2>
<p class="description"><?php echo category_description($cat->cat_ID); ?></p>
<?php while (have_posts()) : the_post(); ?>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="video">
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>
</article>
<?php endwhile; ?>
</section>
<?php endforeach; ?>

最佳答案

耶兹(Jeez),一旦您发现自定义分类法的每个项目都被称为术语(在noob的wordpress文档中不是立即显而易见),则搜索起来将非常容易。如果没有所有自定义查询内容,此解决方案将更易于理解。

<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
<section class="category-<?php echo $category ?>">
<h2><?php echo $category->name; // Print the cat title ?></h2>
<p class="description"><?php echo $category->description ?></p>
<div class="<?php echo $category->post_type ?>-list">
<?php
//select posts in this category (term), and of a specified content type (post type)
$posts = get_posts(array(
'post_type' => 'custom_post_type_name',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
// Now you can do things with the post and display it, like so
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<?php
// Getting custom field data example
echo get_post_meta($post->ID, 'field_key', true);
?>
<?php the_content() ?>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>

然后,可以通过在wordpress Codex中搜索以上功能来填补理解上的任何空白。在上面的代码中,对于我的特定应用程序,custom_post_type_name将是video,而taxonomy_name将是video_type(或者我忘记了video_types)。

关于wordpress - 通过自定义分类类型遍历自定义帖子类型? (按类别订购wordpress帖子,或按分类术语显示自定义帖子类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3848608/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com