尽管我在这里和其他地方读到和发现了一些东西,但我仍然很难弄清楚如何让上一篇/下一篇文章在网站上使用关系字段,我希望能得到一些指导。
我有一个艺术家的自定义分类的艺术作品的CPT。然后我又为艺术家们准备了另一个CPT。艺术家页面基本上是页面标题(艺术家姓名)和一个关系字段,您可以从artworks CPT中添加艺术家的艺术作品,您可以在其中根据自定义分类进行过滤(因为有50+艺术家和100个艺术作品)。
正如所提到的,每一件艺术品都是它自己的页面。当您单击艺术家页面上的艺术作品时,我们希望根据艺术家页面上显示的顺序(由您在其关系字段中设置的顺序确定),选择转到下一个或上一个艺术作品。然而,按照我现在设置的方式,它显然只是基于分类法和发布日期来导航循环,而不是由艺术家页面上的关系字段设置的顺序。
<div class="previous-post-link">
<?php previous_post_link('%link', '<div class="prev-link"><i class="fas fa-caret-left"></i></div>', $in_same_term = true, $excluded_terms = '', $taxonomy = 'assignedartist'); ?>
</div>
<div class="next-post-link">
<?php next_post_link('%link', '<div class="next-link"><i class="fas fa-caret-right"></i></div>', $in_same_term = true, $excluded_terms = '', $taxonomy = 'assignedartist'); ?>
</div>由于某些原因,我无法理解如何从single-artist.php (“artist”帖子类型)查询到single-artwork.php (“artwork”帖子类型-关系字段,“artworks”)页面和关系字段,这样我就可以创建前一个和下一个链接来在artworks之间导航。我可以设法拉出页面信息,如标题(艺术家姓名),但不是关系字段或它的顺序。
我回顾了这篇文章,Next post in relationship field和像ACF Relationship Field Prev/Next Buttons这样的页面,但它们的设置完全不同,以至于我迷失了方向。后者似乎仅限于一个页面,但我有几个艺术家页面。艺术作品只需要导航的艺术作品在任何艺术家的页面上出现。我也觉得后者可以工作,但我被困在了第一部分,即如何从所有艺术家页面或single-artist.php模板中拉出,而不仅仅是…中的页面ID
// This should be the ID of the page the relationship field is set up on,
// NOT this page’s ID
$page_id = 5;
// Get the projects from the relationship field
$project_listing = get_field('project_listing', $page_id);任何帮助或指导都将不胜感激。
发布于 2021-07-22 09:37:30
我尝试了许多解决方案,但都还没有奏效。我获得成功提示的唯一方法是向artworks post类型添加一个ACF字段,该字段通过post object字段选择艺术家post类型。正如@chris-haas建议的那样,这个提示是通过This Link找到的
下面的代码似乎有问题。我注意到$current_post_id是如何应用于array_search的。如果I print_r $current_index,则不会出现任何内容。$prev_module和$next_module也是如此。但是,如果I print_r $module_ids,则阵列显示为应有的样子。$first_module和$last_module也显示了正确的ID。我似乎无法获得当前的帖子ID,然后确定下一个和上一个。
// create empty array for module ids
$module_ids = array();
// loop through modules and add them to array
foreach( $modules as $module ) :
$module_ids[] = $module->ID;
endforeach;
// get the current index
$current_index = array_search( $current_post_id, $module_ids );
// find the prev/next items
$prev_module = $current_index - 1;
$next_module = $current_index + 1;
// find first and last modules
$first_module = $module_ids[0];
$last_module = end($module_ids);我有点不知所措。
发布于 2021-07-23 03:01:09
对于那些来寻找答案的人,他们面临着和我类似的问题。它最终只需在获得当前索引之前添加以下行即可。
$current_post_id = get_the_ID();https://stackoverflow.com/questions/68470357
复制相似问题