开发者

WordPress get_pages Taxonomy Parameter

开发者 https://www.devze.com 2023-02-28 17:35 出处:网络
Curious if anyone has tried filtering content via get_pages using a custom taxonomy. I\'ve got a custom post type labeled \"clothing\" and I\'m trying to create a \"Previous & Next\" navigation by

Curious if anyone has tried filtering content via get_pages using a custom taxonomy. I've got a custom post type labeled "clothing" and I'm trying to create a "Previous & Next" navigation by pulling only the "clothing" pieces that have the slug "female" in the "gender" taxonomy.

However, the Previous and Next links are pulling all clothing pieces, including my "mens" pieces.. Anyone have experience with custom taxonomy and get_pages?

<?php

$args = array(
        'sort_column' => 'menu_order',
        'sort_order' => 'desc',
        'post_type' => 'clothing',
        'post_status' => 'publish',
        'gender'=> 'female'
);

$list = get_pages($args);
$pages = array();
foreach ($list as $page) {
   $pages[] += $page->ID;
}

$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div class="post-nav">
    <?php if (!empty($prevID)) { ?>
    <a href="?pID=<?p开发者_Python百科hp echo $prevID ?>" title="<?php echo get_the_title($prevID); ?>" class="left">&#171; PREVIOUS</a>
    <?php }
    if (!empty($nextID)) { ?>
    <a href="?pID=<?php echo $nextID ?>" title="<?php echo get_the_title($nextID); ?>" class="right">NEXT &#187;</a>
    <?php } ?>
</div>


Used your code in conjunction with this post on advance taxonomy querying, and came up with this:

<?php 

$myquery = array(
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_key' => 'whatever',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'post_type' => 'your_post_type',
);

$myquery['tax_query'] = array(
    array(
        'taxonomy' => 'your_tax',
        'terms' => array('your_term'),
        'field' => 'slug',
    ),
    array(
        'taxonomy' => 'your_other_tax',
        'terms' => array('your_other_term'),
        'field' => 'slug',
    ),
);

$queryObject = new WP_Query($myquery); 

while( $queryObject->have_posts() ) : $queryObject->the_post();

$posts_array[] = $post->ID;

endwhile;

wp_reset_query();

$current = array_search($post->ID, $posts_array); // returns key
$prevID = $posts_array[$current - 1]; // get value from key 1 before
$nextID = $posts_array[$current + 1]; // get value from key 1 after

$prevLINK = get_permalink( $prevID );
$nextLINK = get_permalink( $nextID );

$prevTITLE = get_the_title( $prevID );
$nextTITEL = get_the_title( $nextID );

?>

<div class="post-nav">
    <?php if (!empty($prevID)) { ?>
        <a href="<?php echo $prevLINK; ?>" title="<?php echo $prevTITLE; ?>" class="left">&#171; PREVIOUS</a>
    <?php }
    if (!empty($nextID)) { ?>
        <a href="<?php echo $nextLINK; ?>" title="<?php echo $nextTITLE; ?>" class="right">NEXT &#187;</a>
    <?php } ?>
</div>

Works for me and it's pretty freaking awesome... with the advanced taxonomy querying Otto explains, you can really control those next/previous links quite well. I wouldn't have been able to figure this out without your original code though, so mad props to you and Otto!

I'm going to try and fine tune this even further by auto detecting the terms from the page... get_the_term_list() might work.

0

精彩评论

暂无评论...
验证码 换一张
取 消