WordPress.org

Ready to get started?Download WordPress

Forums

Can I set a *single* default Category on homepage? (21 posts)

  1. mriannnnnn
    Member
    Posted 9 years ago #

    I want to set a single category to show up on the home page and only have the posts to the other categories show up when a user goes directly to that category.
    Has anyone done this? If not, can someone point me in the right direction to do it myself?
    Ian

  2. Jack
    Member
    Posted 9 years ago #

    I don't know if there are nicer ways of doing that, but I use this at the top of my page (even before the first php-call from WP) in case only index.php is called without variables:
    <?php
    if ($cat == "" || $p == "") {
    $cat = numberofthatonecategory;
    }
    ?>

  3. TechGnome
    Moderator
    Posted 9 years ago #

    moxie's got it. That's the best way to set it up.
    TG

  4. mriannnnnn
    Member
    Posted 9 years ago #

    That worked perfectly. I was thinking along those lines but I figured that, "why recreate the wheel?" so I asked here.
    Thanks again!
    Ian

  5. rthak
    Member
    Posted 9 years ago #

    Tried this but then links on the side of the page stop working......all take you to default catergory which is selected....anyway to astop this happening? link on side of page is to http://myhompage/index.php?cat=x
    Is this correct?

  6. Root
    Member
    Posted 9 years ago #

    I agree. When I did it my cats worked but not the archives.

  7. Anonymous
    Unregistered
    Posted 9 years ago #

    From: MajorHart
    My category list on the right brings up a popup with the original post on it, but no fullscreen original post - how can I make it go to fullscreen posts so it's easy to read?
    Thanks MajorHart major@planetkc.com

  8. bmccullo
    Member
    Posted 9 years ago #

    I tried the code provided by Moxie and it did the job...it limited the posts shown on my homepage to one single category.
    I have a problem, though...It overrides the "Show the most recent: X posts" setting found in the wp-admin under Options>>Reading. I want to only show 1 post by default. Any suggestions.
    (I've removed the code snippet)

  9. motoed
    Member
    Posted 9 years ago #

    I tried this code verbatim and ran into problems...(is it possible this isn't compatible with Version 1.3. a5 (I'm also using Kubrick Template ported by Ryan Boren.)
    I get this error:
    Parse error: parse error, unexpected ')' in /nfs/2/bushey/busheyscycling.com/htdocs/wp-content/themes/kubrick/index.php on line 1
    and the exact code I posted to the first line of my index.html file read:
    <?php if ($cat == "" || $p == "") {$cat = 24) ?>

  10. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    For 1.3, see my response here.

  11. Anonymous
    Unregistered
    Posted 9 years ago #

    In regards to the first post, I find that the f ollowing code works best for setting a default category in index.php.
    <?php
    if (empty($cat)) {
    $cat = 'numberofcategory';
    }
    ?>
    Paste this at the top of your template....index.php.
    See it work here - http://www.mcleanusa.com/
    Jon

  12. nolageek
    Member
    Posted 9 years ago #

    Trying to dp this in 1.3 as well. Jesus, this seems like it should be so basic. Nothing I've tried works, in any version of WordPress I try it in.

  13. iand
    Member
    Posted 9 years ago #

    Saw this, haven't tried it though: http://www.schempp.ch/index.php?p=25

  14. JimClark
    Member
    Posted 9 years ago #

    I solved this problem by adding a little snippet of code to the wp-blog-header.php file. There is a section that tests for command line variables that are passed using superglobals and other variables. I added a test after each pass of this for/next loop to see if the $_GET[cat] was null, and if so, set it to my preferred category. The block of code is below with the obvious hack between comments:


    $wpvarstoreset = array('m','p','posts','w', 'cat','withcomments','s','search','exact', 'sentence','poststart','postend','preview','debug', 'calendar','page','paged','more','tb', 'pb','author','order','orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name');

    for ($i=0; $i<count($wpvarstoreset); $i += 1) {
    $wpvar = $wpvarstoreset[$i];
    if (!isset($$wpvar)) {
    if (empty($_POST[$wpvar])) {
    if (empty($_GET[$wpvar]) && empty($path_info[$wpvar])) {
    $$wpvar = '';
    } elseif (!empty($_GET[$wpvar])) {
    $$wpvar = $_GET[$wpvar];
    } else {
    $$wpvar = $path_info[$wpvar];
    }
    } else {
    $$wpvar = $_POST[$wpvar];
    }
    }

    // add this test to set force the category value of your
    // preferred category when no category is selected - like when
    // you first visit the blog. This is the *cat_ID* field.
    // I used a value of 1 which is my General category.
    if ('' == $_GET[cat]) {
    $_GET[cat] = '1';
    }
    // end of inserted category force-feeding.

    }

    If there is a better way, I'm all ears. I haven't found this to break anything - yet. I'm using version 1.2.

    Good Luck!

  15. JimClark
    Member
    Posted 9 years ago #

    Ok - I found out how my previously posted hack breaks the app. While it works fine for the general viewing of your site, when you log in and go to the 'Edit' area, you can only see the one category ID you forced the $_GET[cat] value to. You can still see your drafts, and if you want to edit the published content you can do that by clicking on the 'Edit This' link in the post itself.

    Its not totally broken, but I will be working on this more to find a better way to do this.

  16. o-juice
    Member
    Posted 9 years ago #

    I realise I'm a little late to this discussion, but I found this plugin helpful: http://www.schempp.ch/index.php?p=25

  17. brianv
    Member
    Posted 9 years ago #

    Why hasn't this been coded into the latest WP, is there a definitive way of doing this yet? It was one of the first things I went looking for.

    Brian

  18. Root
    Member
    Posted 9 years ago #

    Yes there is. Use home.php for your front page. Then use query post. Simple.

  19. brianv
    Member
    Posted 9 years ago #

    Hey Root, thanks for quick reply but.......

    I just migrated from Drupal yesterday - I'm still not 100% with WP, could you elucidate for me please?

  20. Root
    Member
    Posted 9 years ago #

    Well on another thread we are just getting into running the loop twice. Fortunately that is not necessary here. I would save index.php as home.php. That will load by default on the front page only . Then edit the loop.
    It needs to be altered from returning all posts to returning a single post in a single cat. The WP tag query post is tailor made. Most of the other stuff could be deleted.

  21. mzieg
    Member
    Posted 8 years ago #

    Here is the top of my "home.php", which now only displays posts in the "Announcements" category:

    1 <?php get_header(); ?>
    2
    3 <div id="content" class="narrowcolumn">
    4
    5 <?php if (have_posts()) : ?>
    6
    7 <?php while (have_posts()) : the_post(); ?>
    8
    9 <?php
    10 if( !preg_match( '/announcements/i', get_the_category_list() ) )
    11 {
    12 continue;
    13 }
    14 ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.