自定义 post 类型的 WP 导航 post
WP navigation post of a custom post type
我需要在 single.php 中包含一个名为 'works' 的自定义 post 类型的上一个和下一个 post 导航按钮。
我包括这个
<?php echo get_next_posts_link('Go to next post'); ?>
<?php echo get_previous_posts_link('Go to prev post');?>
或这个
<?php previous_post_link( $taxonomy = 'works' ); ?>
但不要什么都不显示,否则导航会包含所有 post 和页面。例如,只需要将此 CUT 的 post 分页为轮播画廊。
我想你搜索这个:
https://codex.wordpress.org/Pagination
此 Post 类型的最后 Post 的分页。
你不需要特殊的 "Menü"。
如果使用相同的类别不起作用,那么您应该试试这个。将标签 "works" 添加到自定义 post 类型中的 post。然后就可以得到这个标签的previous_和next_post_link:
<?php previous_post_link( '%link', 'Previous in works', TRUE, ' ', 'post_tag' ); ?>
<?php next_post_link( '%link', 'Next in works', TRUE, ' ', 'post_tag' ); ?>
试试这个
<?php
$term_list = wp_get_post_terms($post->ID, 'TAXONOMY', array("fields" => "slugs"));
if (empty($term_list[1])) {
print_r($term_list[0]);
$termo = $term_list[0];
} else {
print_r($term_list[1]);
$termo = $term_list[1];
}
// get_posts in same custom taxonomy
$postlist_args = array(
'posts_per_page' => -1,
'orderby' => 'menu_order title',
'order' => 'ASC',
'post_type' => 'CUSTOM-POST-TYPE',
'taxonomy'=>'TAXONOMY',
'term'=>$termo,
);
$postlist = get_posts( $postlist_args );
// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}
// get and echo previous and next post in the same taxonomy
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if ( !empty($previd) ) {
echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}
if ( !empty($nextid) ) {
echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
}
?>
祝你好运。
此致
我需要在 single.php 中包含一个名为 'works' 的自定义 post 类型的上一个和下一个 post 导航按钮。
我包括这个
<?php echo get_next_posts_link('Go to next post'); ?>
<?php echo get_previous_posts_link('Go to prev post');?>
或这个
<?php previous_post_link( $taxonomy = 'works' ); ?>
但不要什么都不显示,否则导航会包含所有 post 和页面。例如,只需要将此 CUT 的 post 分页为轮播画廊。
我想你搜索这个:
https://codex.wordpress.org/Pagination
此 Post 类型的最后 Post 的分页。
你不需要特殊的 "Menü"。
如果使用相同的类别不起作用,那么您应该试试这个。将标签 "works" 添加到自定义 post 类型中的 post。然后就可以得到这个标签的previous_和next_post_link:
<?php previous_post_link( '%link', 'Previous in works', TRUE, ' ', 'post_tag' ); ?>
<?php next_post_link( '%link', 'Next in works', TRUE, ' ', 'post_tag' ); ?>
试试这个
<?php
$term_list = wp_get_post_terms($post->ID, 'TAXONOMY', array("fields" => "slugs"));
if (empty($term_list[1])) {
print_r($term_list[0]);
$termo = $term_list[0];
} else {
print_r($term_list[1]);
$termo = $term_list[1];
}
// get_posts in same custom taxonomy
$postlist_args = array(
'posts_per_page' => -1,
'orderby' => 'menu_order title',
'order' => 'ASC',
'post_type' => 'CUSTOM-POST-TYPE',
'taxonomy'=>'TAXONOMY',
'term'=>$termo,
);
$postlist = get_posts( $postlist_args );
// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}
// get and echo previous and next post in the same taxonomy
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if ( !empty($previd) ) {
echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}
if ( !empty($nextid) ) {
echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
}
?>
祝你好运。
此致