$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Remove category from pagination|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Remove category from pagination

matteradmin10PV0评论

I'm trying to modify a old theme but im getting stuck on this code

$post_type = 'portfolio' == get_post_type() ? 'portfolio' : 'post';

if ( ! themify_check( "setting-{$post_type}_nav_disable" ) ) :

    $in_same_cat = themify_check( "setting-{$post_type}_nav_same_cat" )? true: false;
    $this_taxonomy = 'post' == get_post_type() ? 'category' : get_post_type() . '-category';
    $previous = get_previous_post_link( '<span class="prev">%link</span>', '<span class="arrow">' . _x( '&laquo;', 'Previous entry link arrow','themify') . '</span> %title', $in_same_cat, '', $this_taxonomy );
    $next = get_next_post_link( '<span class="next">%link</span>', '<span class="arrow">' . _x( '&raquo;', 'Next entry link arrow','themify') . '</span> %title', $in_same_cat, '', $this_taxonomy );

    if ( ! empty( $previous ) || ! empty( $next ) ) : ?>

        <div class="post-nav clearfix">
            <?php echo $previous; ?>
            <?php echo $next; ?>
        </div>
        <!-- /.post-nav -->

    <?php endif; // empty previous or next

endif; // check setting nav disable

This part specifically. Currently it is listing all of my articles but I cant figure out how to list only category id 1 2 3

$this_taxonomy = 'post' == get_post_type() ? 'category' : get_post_type() . '-category';

I'm trying to modify a old theme but im getting stuck on this code

$post_type = 'portfolio' == get_post_type() ? 'portfolio' : 'post';

if ( ! themify_check( "setting-{$post_type}_nav_disable" ) ) :

    $in_same_cat = themify_check( "setting-{$post_type}_nav_same_cat" )? true: false;
    $this_taxonomy = 'post' == get_post_type() ? 'category' : get_post_type() . '-category';
    $previous = get_previous_post_link( '<span class="prev">%link</span>', '<span class="arrow">' . _x( '&laquo;', 'Previous entry link arrow','themify') . '</span> %title', $in_same_cat, '', $this_taxonomy );
    $next = get_next_post_link( '<span class="next">%link</span>', '<span class="arrow">' . _x( '&raquo;', 'Next entry link arrow','themify') . '</span> %title', $in_same_cat, '', $this_taxonomy );

    if ( ! empty( $previous ) || ! empty( $next ) ) : ?>

        <div class="post-nav clearfix">
            <?php echo $previous; ?>
            <?php echo $next; ?>
        </div>
        <!-- /.post-nav -->

    <?php endif; // empty previous or next

endif; // check setting nav disable

This part specifically. Currently it is listing all of my articles but I cant figure out how to list only category id 1 2 3

$this_taxonomy = 'post' == get_post_type() ? 'category' : get_post_type() . '-category';
Share Improve this question asked Aug 29, 2016 at 1:45 NooBskieNooBskie 1015 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You are looking at the wrong line. $this_taxonomy is telling what taxonomy type (category, tag, custom tax, etc.) to look for, not what actual term in that taxonomy type.

That being said, you should look at $previous and $next. More specifically, get_previous_post_link() and get_next_post_link(). The fourth parameter is $excluded_terms which is currently empty.

The codex says you can supply an array or comma-separated list of term ids.

This would seem cumbersome to maintain because you would have to update that parameter each time a new term is added to your taxonomy.

But technically, if you have categories 1,2,3,4 and 5, then supplying $excluded_terms = '4,5'; to your previous and next function would exclude those terms from your list.

So, if you want a method that won't need updating each time a new category is added, I would try to do it by modifying the main query instead. Add this to your functions.php

add_action( 'pre_get_posts', 'my_allowed_cats' );
function my_allowed_cats( $query ){

  $post_type = get_post_type();

  if( $post_type == 'post' || $post_type == 'portfolio' ){

    // Use category ids
    $allowed_cats = array(
      1,
      2,
      3,
    );

    $query->set( 'category__in', $allowed_cats );

  }

}

This is untested, but it should work

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far