Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9012017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:53:34+00:00 2026-06-16T02:53:34+00:00

I wish to insert a line of code inside my wordpress main page index.php

  • 0

I wish to insert a line of code inside my wordpress main page index.php which would display all posts except for posts that fall in ‘Diary’ category.

the original line of code is as follows

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <div <?php post_class(); ?>>
    <span class="postlabel"><a href="<?php the_permalink() ?>"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></a></span>
        <h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
            <p class="info">
                <span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span>
                <span class="cmtinfo"><?php comments_popup_link( __( 'Leave a comment', 'Mflat' ), __( '1 Comment', 'Mflat' ), __( '% Comments', 'Mflat' ) ); ?><?php edit_post_link(' Edit', ' &#124; ', ''); ?></span>
            </p>
        <div class="entry">
            <?php $Mflat_options = get_option('Mflat_theme_options'); ?>
            <?php if ( $Mflat_options['home_excerpt_check']== 1 ) { the_excerpt(__('&raquo; Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?>
         </div>
        <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
            <p class="infobottom">
                <?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?>
                <?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?>
                </p>
    </div>
        <?php endwhile; ?>
    <div class="flip">
        <div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div>
        <div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div>
    </div>
<?php else : ?>
<div id="main">
 <h2><?php _e('Not Found', 'Mflat'); ?></h2>
</div>
    <?php endif; ?>

I tried the following codes but to no avail

<?php if(have_posts()) : ?><?php while(have_posts()){
    <if(!in_category('Diary')){
        : the_post(); 
        }
    }?>

Is there any better way i can get this done?
I am new to PHP and though i know the logic of what has to be done, i cant quite generate the code to make this work. I think boolean would be a little too much work and found a similar (http://stackoverflow.com/questions/13587958/display-recent-posts-based-on-their-category-in-wordpress) thread on stark overflow but it doesnt quite suit my needs.

Every contribution is dearly appreciated. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:53:35+00:00Added an answer on June 16, 2026 at 2:53 am

    just run the query_posts() function before the loop – it alters the loop

    <?php
    query_posts(array(
        'category_name' => 'my-category-slug', // get posts by category name
        'posts_per_page' => -1 // all posts
    ));
    ?>
    
    <?php while(have_posts()): the_post(); ?>
        <h1><?php the_title(); ?></h1>
    
        <?php the_content(); ?>
    <?php endwhile; ?>
    

    My bad, I just noticed you want to exclude a category. Same function, different arguments:

    $category = get_term_by('name', $cat_name, 'category'); // get category
    $id = $category->term_id; // get the category ID
    
    query_posts('cat=-' . $id); // exclude the found ID
    

    <?php
    $category = get_term_by('name', 'Diary', 'category'); // get category data
    $id = $category->term_id; // get the category ID
    
    // USE THIS IF YOU WANT TO _EXCLUDE_ ALL POSTS FROM "DIARY"
    query_posts($query_string . '&cat=-' . $id);
    
    // USE THIS IF YOU WANT TO GET ALL POSTS FROM "DIARY" ONLY
    // query_posts($query_string . '&cat=' . $id);
    ?>
    
    <?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
            <div <?php post_class(); ?>>
                <span class="postlabel"><a href="<?php the_permalink() ?>"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></a></span>
    
                <h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
    
                <p class="info">
                    <span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span>
                    <span class="cmtinfo">
                        <?php comments_popup_link( __( 'Leave a comment', 'Mflat' ), __( '1 Comment', 'Mflat' ), __( '% Comments', 'Mflat' ) ); ?>
                        <?php edit_post_link(' Edit', ' &#124; ', ''); ?>
                    </span>
                </p>
    
                <div class="entry">
                    <?php $Mflat_options = get_option('Mflat_theme_options'); ?>
                    <?php if ( $Mflat_options['home_excerpt_check']== 1 ) { the_excerpt(__('&raquo; Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?>
                 </div>
    
                <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
                <p class="infobottom">
                    <?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?>
                    <?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?>
                </p>
            </div>
        <?php endwhile; ?>
    
        <div class="flip">
            <div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div>
            <div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div>
        </div>
    <?php else : ?>
        <div id="main">
            <h2><?php _e('Not Found', 'Mflat'); ?></h2>
        </div>
    <?php endif; ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My 1st table would be port which has columns status,destination,ferry that i wish to
I have a list of numbers between 1-20,000. I wish to insert all of
I wish to insert a Date value in DataRow[] and find a DateTime value
I wish to DELETE the data from a table before performing an INSERT INTO,
I can insert annotations on specific datasets on the graph but I wish to
I wish to use sp_start_job to start a job from within an insert trigger
I wish to add the mouseover events to a panel which contains more than
I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
I wish to Insert or Update a row in a table - so I
I have two dataframes and I wish to insert the values of one dataframe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.