如何获得同一分类法中的下一个前一类别?

时间:2013-05-15 作者:M Khalid Junaid

如WPget_adjacent_post 函数根据参数提供下一个和上一个帖子数据我想获取下一个/上一个类别数据是否有WP 内置函数,可以完成正确的工作,否则我必须为此编写自定义查询。

// Next/previous post example
$in_same_cat = false;
$excluded_categories = \'\';
$previous = true;
$previous_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
我想要下一个类别category slug 或由category id

任何帮助都将不胜感激

6 个回复
最合适的回答,由SO网友:M Khalid Junaid 整理而成

这就是我提出的自定义查询解决方案,希望它能帮助其他人我为此编写了一个自定义函数它适用于所有帖子类型类别(帖子,自定义帖子类型)

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");

SO网友:Charles Clarkson

是否有任何WP内置功能可以完成正确的工作?

[我]必须为此编写自定义查询吗?

编号使用get_terms(). 这里有一个例子。

添加wpse_99513_adjacent_category 类的函数。php主题文件,并按如下方式调用:

$category_ids = new wpse_99513_adjacent_category( \'category\', \'id\', false );
--“category”是分类法,“id”是数据库查询的排序依据字段,false显示空类别

要获取下一个分类,请使用以下内容:

$next_category = $category_ids->next( $category );
--$category是您正在检查的类别的id,
--$next\\u category如果有错误,则设置为false,否则设置为next id。

以前的作品也是这样:

$previous_category = $category_ids->previous( $category );
--$category是您要检查的类别的id,
--$previous\\u category如果有错误,则设置为false,否则设置为previous id。

对于跳过空类别的Slug,请使用:

$category_ids = new wpse_99513_adjacent_category( \'category\', \'slug\' );

class wpse_99513_adjacent_category {

    public $sorted_taxonomies;

    /**
     * @param string Taxonomy name. Defaults to \'category\'.
     * @param string Sort key. Defaults to \'id\'.
     * @param boolean Whether to show empty (no posts) taxonomies.
     */
    public function __construct( $taxonomy = \'category\', $order_by = \'id\', $skip_empty = true ) {

        $this->sorted_taxonomies = get_terms(
            $taxonomy,
            array(
                \'get\'          => $skip_empty ? \'\' : \'all\',
                \'fields\'       => \'ids\',
                \'hierarchical\' => false,
                \'order\'        => \'ASC\',
                \'orderby\'      => $order_by,
            )
        );
    }

    /**
     * @param int Taxonomy ID.
     * @return int|bool Next taxonomy ID or false if this ID is last one. False if this ID is not in the list.
     */
    public function next( $taxonomy_id ) {

        $current_index = array_search( $taxonomy_id, $this->sorted_taxonomies );

        if ( false !== $current_index && isset( $this->sorted_taxonomies[ $current_index + 1 ] ) )
            return $this->sorted_taxonomies[ $current_index + 1 ];

        return false;
    }

    /**
     * @param int Taxonomy ID.
     * @return int|bool Previous taxonomy ID or false if this ID is last one. False if this ID is not in the list.
     */
    public function previous( $taxonomy_id ) {

        $current_index = array_search( $taxonomy_id, $this->sorted_taxonomies );

        if ( false !== $current_index && isset( $this->sorted_taxonomies[ $current_index - 1 ] ) )
            return $this->sorted_taxonomies[ $current_index - 1 ];

        return false;
    }
}

SO网友:akmur

这就是我想到的。也许不是最好的,但很有效:)

假设我们在自定义分类类别页面上,并且希望转到下一个

// get the term used on this current taxonomy page
$term = get_term_by( \'slug\', get_query_var(\'term\'), get_query_var(\'taxonomy\') );

//set your arguments
$args = array(
     \'hide_empty\' => 0,
     \'orderby\' => \'name\',
     \'order\' => \'DESC\',
);

    //set your vars
$cycletaxonomy = \'author\';
    // get all terms in a custom taxonomy

$cycleterms = get_terms($taxonomy, $args);

    // A for loop to cycle through all terms
for ($x=0; $x<count($cycleterms); $x++){
            // assign current cycled category slug - i could have used id too actually
    $thisslug = $cycleterms[$x]->slug;

    if ($curslug == $thisslug) {
        $nextslug = $cycleterms[$x+1]->slug;
        $prevslug = $cycleterms[$x-1]->slug;
        echo $nextslug;
        echo $prevslug;
                    // now do what you want with this slug - like putting it into a link tag
    }
};

SO网友:Core

我不确定WordPress中是否有bulit函数,但您可以

使用下一个和上一个帖子ID获取下一个和上一个页面及其类别。

SO网友:Morskia

基于Akmur答案的小更新

$term = get_term_by(\'slug\', get_query_var(\'term\'), get_query_var(\'taxonomy\'));
$curslug = $term->name;
$args = array(
    \'hide_empty\' => 0,
);
$cycletaxonomy = \'YOUR_TERM\';
// get all terms in a custom taxonomy
$cycleterms = get_terms($taxonomy, $args);
// A for loop to cycle through all terms
for ($x = 0; $x < count($cycleterms); $x++) {
    // assign current cycled category slug - i could have used id too actually
    $thisslug = $cycleterms[$x]->name;

    if ($curslug == $thisslug) {
        $nextslug = $cycleterms[$x + 1]->name;
        $prevslug = $cycleterms[$x - 1]->name;
        echo \'<ul class="post-nav">\';
        if (!empty($nextslug)) {
            echo \'<li class="next"><a href="\' . get_term_link(intval($cycleterms[$x + 1]->term_id), \'YOUR_TERM\') . \'">\' . $nextslug . \'</a><li>\';
        }
        if (!empty($prevslug)) {
            echo \'<li class="previous"><a href="\' . get_term_link(intval($cycleterms[$x - 1]->term_id), \'YOUR_TERM\') . \'">\' . $prevslug . \'</a><li>\';
        }
        echo \'</ul>\';
        // now do what you want with this slug - like putting it into a link tag
    }
}

SO网友:Grandy

将此函数放在函数中。php:

function adjacent_post_by_category( $category, $direction, $content ) {

    // Info
    $postIDs = array();
    $currentPost = get_the_ID();


    // Get posts based on category
    $postArray = get_posts( array(

        \'category\' => $category,
        \'orderby\'=>\'post_date\',
        \'order\'=> \'DESC\'

    ) );


    // Get post IDs
    foreach ( $postArray as $thepost ):

        $postIDs[] = $thepost->ID;

    endforeach;


    // Get prev and next post ID
    $currentIndex = array_search( $currentPost, $postIDs );
    $prevID = $postIDs[ $currentIndex - 1 ];
    $nextID = $postIDs[ $currentIndex + 1 ];


    // Return information
    if( $direction == \'next\' AND $nextID ):

        return \'<a rel="next" href="\' . get_permalink( $nextID ) . \'">\' . $content . \'</a>\';

    elseif( $direction == \'prev\' AND $prevID ):

        return \'<a rel="prev" href="\' . get_permalink( $prevID ) . \'">\' . $content . \'</a>\';

    else:

        return false;

    endif;

}
这是您的上一个/下一个链接(函数参数是示例):

<?php echo adjacent_post_by_category( 10, \'prev\', \'Show previous post\' ); ?>

<?php echo adjacent_post_by_category( 10, \'next\', \'Show next post\' ); ?>

结束

相关推荐

Custom Post taxonomy template

朋友们,我需要帮助。我正在为朋友扩展Mayashop主题&;我遇到了一个问题。创建了一个名为watchdog的自定义帖子类型,为其创建了一个名为watchdog brand的分类法。。。现在是唯一的看门狗。php模板工作得很好,但我似乎找不到适合这个术语本身的模板,其中列出了它下面的所有看门狗项。已尝试存档监视程序。php,存档看门狗品牌。php但运气不好。。。有什么想法吗?我还发现它甚至没有回退到归档。php。。直接进入索引。php文件&;以下是custom\\u post的代码(&U);分