我试图从我的下一个和上一个链接中排除其他帖子格式,但出于某些原因,使用get_term_by
不会把term_id
对于post_format
分类学我看到Wordpress核心正在使用这种分类法here, 所以我知道我这样做是对的。根据this posting, 我需要确保分类法已正确注册。打印调试我从中看到以下输出get_taxonomies()
.
Array
(
[category] => category
[post_tag] => post_tag
[nav_menu] => nav_menu
[link_category] => link_category
[post_format] => post_format
[tagportfolio] => tagportfolio
)
这样我就能看到
post_format
分类法已注册。这是我的代码:
<?php print_r(get_taxonomies()); ?>
<?php print_r(get_term_by(\'id\', \'post-format-aside\', \'post_format\')->term_id); ?>
<?php previous_post_link(\'‹ %link\', \'%title\', false, array(get_term_by(\'id\', \'post-format-aside\', \'post_format\')->term_id )); ?>
<?php next_post_link(\'%link ›\', \'%title\', false, array(get_term_by(\'id\', \'post-format-aside\', \'post_format\')->term_id )); ?>
你知道为什么这样不行吗?为什么这里不排除旁白?
UPDATE 1
下面的代码从中返回term\\u id
get_term_by
, 但会导致下一个/上一个链接消失:
<?php print_r(get_taxonomies()); ?>
<?php print_r(get_term_by(\'slug\', \'post-format-aside\', \'post_format\')->term_id); ?>
<?php previous_post_link(\'‹ %link\', \'%title\', false, array(get_term_by(\'slug\', \'post-format-aside\', \'post_format\')->term_id )); ?>
<?php next_post_link(\'%link ›\', \'%title\', false, array(get_term_by(\'slug\', \'post-format-aside\', \'post_format\')->term_id )); ?>
返回的对象
get_term_by
是:
STDCLASS OBJECT ( [TERM_ID] => 10 [NAME] => ASIDE [SLUG] => POST-FORMAT-ASIDE [TERM_GROUP] => 0 [TERM_TAXONOMY_ID] => 10 [TAXONOMY] => POST_FORMAT [DESCRIPTION] => [PARENT] => 0 [COUNT] => 0 [FILTER] => RAW )
UPDATE 2
当我启用
Wordpress query debugging, 我看到下面的查询被触发,它是空集。
mysql> SELECT P.ID FROM WP_POSTS AS P INNER JOIN WP_TERM_RELATIONSHIPS AS TR ON P.ID = TR.OBJECT_ID INNER JOIN WP_TERM_TAXONOMY TT ON TR.TERM_TAXONOMY_ID = TT.TERM_TAXONOMY_ID WHERE P.POST_DATE < \'2015-06-01 20:10:00\' AND P.POST_TYPE = \'PROJECT\' AND TT.TAXONOMY = \'CATEGORY\' AND P.ID NOT IN ( SELECT TR.OBJECT_ID FROM WP_TERM_RELATIONSHIPS TR LEFT JOIN WP_TERM_TAXONOMY TT ON (TR.TERM_TAXONOMY_ID = TT.TERM_TAXONOMY_ID) WHERE TT.TERM_ID IN (10) ) AND ( P.POST_STATUS = \'PUBLISH\' OR P.POST_STATUS = \'PRIVATE\' ) ORDER BY P.POST_DATE DESC ;
Empty set (0.01 sec)
上述内容的奇怪之处在于,查询似乎试图根据类别分类法查找上一篇文章,我已明确要求函数不要通过查询参数执行此操作。在我看来,这似乎是Wordpress中的一个bug。