我为附件注册了自定义分类法。在管理方面一切都很好:我可以用自定义分类法的类别标记媒体,它们显示在我的图像中,等等。
然后,我为相册页面构建一个模板文件,该页面过滤已标记有我的自定义taxo类别(按类别分组)的图像。
以下是我在本页的代码示例:
$post_type = \'attachment\';
$tax = \'albums\';
$tax_terms = get_terms($tax,array(\'orderby\' => \'id\', \'order\' => \'DESC\') );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
\'post_type\' => $post_type,
\'albums\' => $tax_term->slug,
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$terme = $tax_term->slug;
$lien_album = \'http://www.example-site.com/wp/section/albums-photo/album-individuel/?album=\'.$terme;
echo \'<a href="\'.$lien_album.\'"><h3 style="margin-bottom:-8px;">\'.$tax_term->name.\'</h3></a><br/>\';
while ($my_query->have_posts()) : $my_query->the_post();
$image_id = get_the_ID();
$image_adresse = wp_get_attachment_image_src( $image_id, \'medium\');
?>
<div class="vignettes">
<img src="<?php echo $image_adresse[0]; ?>" class="vignette" alt="<?php the_title_attribute(); ?>" />
</div>
<?php
endwhile;
}
wp_reset_query();
?>
<a class="bouton-albums" href="<?php echo $lien_album; ?>">Voir les photos de cet album</a>
<p style="clear:both;"> </p>
<?php
}
}
我注意到只有在实际页面(或帖子)中使用的图像才会显示在我的模板页面中。
在分析了Codex的register\\u taxonomy函数后,我了解到一个名为“update\\u count\\u callback”的参数,并且在对帖子以外的内容使用自定义taxo时,它应该使用值“\\u update\\u generic\\u term\\u count”。然后,我将其添加到我的register\\u taxonomy函数参数中,如下所示:
\'update_count_callback\' => \'_update_generic_term_count\',
不幸的是,这似乎不起作用。或者这场争论与我想要达到的目标无关?
我不希望我的每个相册的图像都出现在一个页面或帖子中。我只想在媒体库中添加一些图像,用我的自定义taxo标记它们,并使它们显示在我的相册页面(使用我的特殊模板)。
我希望有人能告诉我哪里错了,或者如何得到我想要的!
最合适的回答,由SO网友:Fabien Quatravaux 整理而成
当您查询时attachments, 你必须注意post_status 与其他帖子类型具有不同含义的参数。Attachments have the status of inherit 这意味着他们的状态应该与他们的父帖子(即他们上传的帖子)相同。这会使与未发布帖子关联的附件以及已直接上载到媒体库的附件丢失。
因此,为了使代码正常工作,您可以替换
\'post_status\' => \'publish\',
使用
\'post_status\' => \'any\',
关于
update_count_callback 参数,它与显示在管理中某个位置的计数有关。您可以使用此回调自己计算此数字。所以这与你的问题无关。