• September 8, 2018
    Excluding categories in the_category

    โค๊ด: [Select]

    <?php the_category() ?>
    <?php
    wp_list_categories('orderby=name&include=3,5,9,16'); ?>

    <?php foreach((get_the_category()) as $cat) {
    if (!($cat->cat_ID=='37')) echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. $cat->cat_name . '</a>' . ', ';
    } ?>


    Hide / Exclude Categories From Posts Page

    โค๊ด: [Select]

    <?php if (is_front_page() && !is_paged() )
    $posts = query_posts($query_string . '&cat=-35'); ?>

    <?php
    $cat_to_exclude = 35;
    query_posts($query_string . '&cat=-'.$cat_to_exclude');
    ?>

    <?php if (in_category('35')) continue; ?>
    or

    โค๊ด: [Select]


    function exclude_category_home( $query ) {
    if ( $query->is_home ) {
    $query->set( 'cat', '-5, -34' );
    }
    return $query;
    }

    add_filter( 'pre_get_posts', 'exclude_category_home' );
    Exclude Category from Homepage

    Placing this code in your index.php file will cause your home page to display posts from all categories except category ID 3.

    โค๊ด: [Select]

    <?php
    if (is_home()) {
    query_posts("cat=-3");
    }
    ?>

    You can also add some more categories to the exclude-list(tested with WP 2.1.2):

    โค๊ด: [Select]

    <?php
    if (is_home()) {
    query_posts("cat=-1,-2,-3");
    }
    ?>

    or

    โค๊ด: [Select]

    function excludeCat($query) {
    if ( $query->is_home ) {
    $query->set('cat', '-3,-5,-23');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'excludeCat');

    โค๊ด: [Select]

    <?php query_posts("cat=-25,35"); ?>
    Look for this on your main theme index page:

    โค๊ด: [Select]

    <?php if (have_posts()) : ?>
    Insert this right before (or above) it:

    โค๊ด: [Select]

    <?php query_posts('cat=-49'); ?>
    Change the category ID to whatever it is you want to exclude.

    โค๊ด: [Select]

    <?php if (is_home()) {
    query_posts($query_string . "&cat=-33");
    } ?>
    <?php if(is_home()) {query_post("cat=-39");}?php>

    โค๊ด: [Select]

    <?php
    $excluded_cats = array( 44, 55 );  // ids to exclude
    $args = array(
    'caller_get_posts' => 1,  // Don't show stickies at top
    'posts_per_page' => 5,    // The number of latest to show
    'category__not_in' => $excluded_cats,
    );
    query_posts($args);

    Display all of a post’s categories except the current category

    โค๊ด: [Select]

    if(is_category()):// only run this on category page
    $cc = get_queried_object();
    $exclude = $cc->cat_name; //get the name of current category page
    endif;

    โค๊ด: [Select]

    <?php
    if(is_category()):// only run this on category page
    $cc = get_queried_object();
    $exclude = $cc->cat_name; //get the name of current category page
    endif;

    $categories = get_the_category();
    foreach($categories as $category) :

    if(is_category() && $category->name==$exclude)
    continue; //skip current category from being displayed only on category pages

    $category_link = get_category_link( $category->cat_ID );
    echo '<li><a href="'.$category_link.'">'.$category->cat_name.'</a></li>';
    endforeach;
    ?>

    โค๊ด: [Select]

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <!-- If the post is in the category we want to exclude, we simply pass to the next post. -->
    <?php if (in_category('3')) continue; ?>

    <div class="post">

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

    <small><?php the_time('F jS, Y'); ?></small>

    <div class="entry">
    <?php the_content(); ?>
    </div>

    <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
    </div> <!-- closes the first div box -->

    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>



เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories


Uncategorized