我一直在努力在标题中创建链接,应该说“请参见此处输入下一个类别名称”
我一直在使用这个代码,发现here (我想再次发表评论,直接问这个问题,但我不被允许)
function get_adjacent_category($category_slug,$taxonomy,$type){
global $wpdb;
if($type=="next"){
$operater=" > ";
$orderby=" ORDER BY tt.`term_id` ASC ";
}else{
$operater=" < ";
$orderby=" ORDER BY tt.`term_id` DESC ";
}
$query="SELECT *,(SELECT `term_id` FROM wp_terms WHERE `slug`=\'".$category_slug."\') AS given_term_id,
(SELECT parent FROM wp_term_taxonomy WHERE `term_id`=given_term_id) AS parent_id
FROM `wp_terms` t
INNER JOIN `wp_term_taxonomy` tt ON (`t`.`term_id` = `tt`.`term_id`)
HAVING tt.taxonomy=\'".$taxonomy."\' AND tt.`parent`=parent_id AND tt.`term_id` $operater given_term_id $orderby LIMIT 1";
return $wpdb->get_row($query);
}
$next_category = get_adjacent_category($slug,$taxonomy,"next");
$previous_category = get_adjacent_category($slug,$taxonomy,"previous");
虽然我没有犯错误,但它并没有为我带来下一个类别。只是一个空白。下面是我用来调用函数的代码:
<div style="float:right;">See<?php get_adjacent_category($slug,$taxonomy,"next"); ?></div>
我叫错了吗?如果有帮助的话,我正在研究论文主题,下面是
website 我和他一起工作。我试图将其放在存档页面上,镜像到顶部的“返回博客”链接。
如果您有任何见解,我们将不胜感激!!
最合适的回答,由SO网友:TheDeadMedic 整理而成
这里的问题是,您没有正确使用该函数-它返回一个对象,本身不会输出任何内容。您还需要确保$slug
您的其他参数已设置&;有值,否则只会触发一个错误。
就我个人而言,我会更进一步,改用以下函数——参数更灵活,根本不需要自定义SQL查询;您可以利用现有的WordPress功能,并增加对象缓存的好处。
/**
* Get adjacent category or term.
*
* @link http://wordpress.stackexchange.com/q/137203/1685
*
* @param string $direction "next" or "previous"
* @param mixed $term Term ID, slug or object. Defaults to current tax archive, if applicable.
* @param string $taxonomy Optional. Not required if $term is object.
* @return object|null
*/
function get_adjacent_term( $direction = \'next\', $term = null, $taxonomy = null ) {
if ( $term ) {
// Bit of type checking as we want the term object.
if ( is_numeric( $term ) )
$term = get_term( $term, $taxonomy );
elseif ( ! is_object( $term ) )
$term = get_term_by( \'slug\', $term, $taxonomy );
} elseif ( is_category() || is_tag() || is_tax() ) {
// Default to current term object if $term was omitted.
$term = get_queried_object();
}
if ( ! $term || is_wp_error( $term ) )
return null;
// Get all siblings of term.
$terms = get_terms(
$term->taxonomy,
array(
\'parent\' => $term->parent,
)
);
// Find index of current term in stack.
foreach ( $terms as $index => $_term ) {
if ( $_term->term_id == $term->term_id )
break;
}
if ( $type === \'next\' ) {
if ( ! isset( $terms[ ++$index ] ) )
return null;
} elseif ( ! isset( $terms[ --$index ] ) ) {
return null;
}
// $index will now be the adjacent term.
return $terms[ $index ];
}
至少在分类法归档页面上,使用的是:
<?php if ( $next = get_adjacent_term() ) : ?>
<div style="float: right;">
See <a href="<?php echo get_term_link( $next ) ?>"><?php echo $next->name ?></a>
</div>
<?php endif ?>