• I have need for a setup that will display posts from a specific category “Announcements” on the main page, and mask all others. Other posts must still be accessible through direct linking and through the sidebar. I have not been able to develop a fix for the current setup. Is there a straightforward solution to this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter subjectego

    (@subjectego)

    If anyone has a suggestions as to where I should be looking, or any third-party material I should look into, please let me know. This is somewhat of an immediate issue.

    Category Visibility plugin.
    Ultimate Category Excluder plugin.

    One of those help?

    Thread Starter subjectego

    (@subjectego)

    Well, it’s a start. They both perform roughly the same function, but not without substantial bugs.

    The “Category Visibility” plugin gives the most options for excluding categories from various areas of the site, but destroys the formatting in the sidebar.
    The “Ultimate Category Excluder” plugin does not harm formatting in any obvious way, but it only allows a single category to be excluded.

    I’ve written to the authors with the issues, but am still left without a solution for the time being.

    I find adding this code to my main page helps me to see what information I have to work with that is already available:

    <?php print_r ($wp_query); ?>

    Once you see the results of that print, you will see that there is absolutely no mention of a category on the front page. Argh. You’ll have to query the database with
    something like:

    $one_cat_post = $wpdb->get_results('select post_id from $wpdb->post2cat where category_id =\"the category number you want\"', ARRAY_N);

    Then on the main page after:
    <?php while (have_posts()) : the_post(); ?>

    load up a variable with the current post id:
    <?php $curr_ID = the_ID();?>

    use a conditional statement to select the posts you want to display

    <?php if ( (isset($one_cat_post)) && (in_array($curr_ID,$one_cat_post)) ){
    
    enclose all the regular front page info in here
    
    }?>
     

    EDIT: there should not be backslashes where you insert your own category id in the sql statement. The editor keeps putting them in

    Thread Starter subjectego

    (@subjectego)

    First, when I load the page with :

    $one_cat_post = $wpdb->get_results(‘select post_id from $wpdb->post2cat where category_id =\”the category number you want\”‘, ARRAY_N);

    I get the error :

    WordPress database error: [You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ‘->post2cat where category_id = “3”‘ at line 1]
    select post_id from $wpdb->post2cat where category_id = “3”

    Then, when I insert :

    <?php if ( (isset($one_cat_post)) && (in_array($curr_ID,$one_cat_post)) ){

    I get a Parse error, which points to the start of the next line of code.

    I guarantee I am doing something wrong. I’d just like to know what it is.

    Sorry, that was untested and just an idea to try. It produced unexpected results when I tried it. Give me a bit of time and I’ll have something for you.

    I’m back with a tested method – and no additional queries needed – the info is coming from the category cache.

    This has been tested and it works – no errors.

    In your theme/***/index.php page find:
    <?php while (have_posts()) : the_post(); ?>

    directly after that place this:

    <?php
      $the_cat = get_the_category();
      foreach ($the_cat as $uh_huh){
      if ($uh_huh->cat_ID == 13){
      echo 'Yuppers';
      }else{
      echo 'Nopers';
      }
      ?>

    This is just a demo code. You would change the category id 13 to whatever category you want to display posts for, as well as putting whatever you want on the “Yuppers” line.

    Just plug this in and test it to make sure it works for you. It should display one of the two echos depending on the category id of the post.

    If you need any more info or whatever, reach me here or over at my blog .

    Best, Sheri

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Masking Categories in main page.’ is closed to new replies.