WordPress.org

Support

Support » Plugins and Hacks » Can I set a *single* default Category on homepage?

Can I set a *single* default Category on homepage?

  • 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

Viewing 15 replies - 1 through 15 (of 20 total)
  • 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;
    }
    ?>

    TechGnome

    @techgnome

    Moderator

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

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

    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?

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

    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

    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)

    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) ?>

    Moderator Ryan Boren

    @ryan

    For 1.3, see my response here.

    Anonymous

    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

    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.

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

    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!

    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.

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

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Can I set a *single* default Category on homepage?’ is closed to new replies.
Skip to toolbar