is_child

Usage:

bool is_child ( mixed parent [, bool recursive] )

  • mixed parent can be either integer or string, submitting a post/page/category ID or title or slug you want to check the relationship to.
  • bool recursive can be either true for a recursive search or false, so only the direct parents of the current item will be checked. Default is true.

The returned value will be true if the current item is a child of mixed parent or false if it’s not.

What it does:
is_child offers the possibility to check out if the current post, page or category is a child item of a specific category or page. This comes in handy if you i.e. want to have a special sidebar throughout a category and all of its subs as well as all containing postings in there. Or maybe you’d want to turn your WordPress installation into a CMS using Pages, offering some special features throughout a hierarchy. WordPress currently does not offer this function (but sure will somewhere in the future), so you might find this plugin useful until then.

Examples:

is_child( 10 )
Checks if the current page, posting or category is somehow related to category- (or page-)ID 10.

is_child( "Example Category", false )
Checks if the current element is a direct child of “Example Category”.

is_child( "some-page", 1 )
Checks if the current page is somehow related to a page with the slug “some-page”.

A more detailed example:

Assume you’re here:
Music -> CDs -> Artists -> JAMMIN*INC -> Mit Anlauf

Now you want to place some Amazon Ads throughout the whole Music category and all of it’s content. Easy as pie:
if( is_child( "Music" ) ) include( "amazon-ads.php" );

There you go. Guess there are even more possibilites which I haven’t thought of. If so, please share your ideas in the comment section.

Download:
Go ahead and fetch it here: is_child.zip

Installation:
Extract is_child.php from the .zip-archive and place it in your WordPress plugin-folder. Then browse to your administration menu named “Plugins” and activate the plugin. That’s it.

History:

1.1.2 Fixed a bug which ended up in seldomly sending an invalid SQL-query when browsing archives.
1.1.1 Fixed a bug that resulted in getting wrong page IDs from SQL-queries.
1.1.0 Added checking for parental pages.
1.0.0 Final Release.
0.5.0 No more catlooping. All categories in queue are now handled with one single query to boost up performance. Catching IDs if zero, as they’re always true.
0.4.0 Now being able to look for category name or slug instead of the plain ID.
0.3.0 Automated detection of category, page or single posting.
0.2.0 Added recursive search.
0.1.0 Initial Test Release.

  • #1 geschrieben von Nimue Sionnach
    vor 4 Jahren

    hmmm…. könnte sein, daß das genau das ist, was ich gesucht habe… für die blondinen: wo genau stell ich denn da die otionen ein?

    zb, wenn ich für die Kat. 1 eine andere sidebar möchte?

    wäre nett, wenn du mir helfen könntest.

  • #2 geschrieben von Erik Range
    vor 4 Jahren

    Ist eigentlich ganz einfach, auch, wenn es sinngemäß leider keine Optionen zum Einstellen gibt. Aber in Deinem Falle könntest Du zum Beispiel in Deinem Template in der sidebar.php Folgendes einbauen:

    if( is_child( 1 ) )
    include( 'sidebar-musik.php' )
    else
    include( 'sidebar-normal.php' );

    Voraussetzung ist hier, dass sich die “normale” Sidebar in sidebar-normal.php befindet, und eine spezielle Sidebar beispielsweise in sidebar-musik.php.

    Theoretisch lässt sich das ganze auch noch mit switch/case-Anweisungen bis in’s Ultimo vertiefen. Wenn Du sowas brauchst, sag einfach Bescheid. Wobei sich da vielleicht auch ein Plugin draus machen liesse.. :-)

  • #3 geschrieben von Elaine
    vor 4 Jahren

    Happy see this on the support forum and I’ve been playing with it a bit. My problem is that it seems to return inaccurate results:

    if (is_page('about') || is_child(2)){
    do whatever
    }

    returns true on my about page but not on any of the children of it.

    if (is_page('about') || is_child("about")){
    do whatever
    }

    returns true for *all* my pages. Any thoughts?

  • #4 geschrieben von Nimue Sionnach
    vor 4 Jahren

    Bescheid!

    Ich geh mal in medias res… mir geht es darum, die hauptkategorie von den unterkategorien zu trennen.

    ich habe die kat. “lieblingsdinge” Klicke ich die an, zeigt es mir auch die inhalte der unterkategorien an.
    diese tuttifrutti will ich vermeiden.
    und -wenn da möglich ist- möchte ich zu bestimmten kategorien die passenden links dazupacken.

    also Kategorie “zauberhaftes” alle möglichen links, die sich mit magischen beschäftigen.

    die sache mit der sauberen trennung von haupt- und unterkategorie ist von wp bisher sträflich!! (für eine steinzicke!) vernachlässigt worden. ich habe es mit widgets versucht /www.spiegelland.de.hm) aber da wird die sidebar unerträglich lang.

    habe ich mich verständlich ausgedrückt?

    vielen dank für die prompte antwort!

  • #5 geschrieben von Erik Range
    vor 4 Jahren

    @Elaine:
    I’ll backcheck the page functionality in a minute with a fresh WP installation just to check if there’s maybe a glitch in it. Didn’t have the time or possibility yet to do some heavy testing with the parentals for pages, as this was a requested overnight-feature which worked by now flawlessly on that installation.

    @Nimue:
    Ich musste erstmal grübeln, was Du mit dem Tutti Frutti meintest. Aber ich vermute mal, das wird so sein: Du klickst auf “Lieblingsdinge”, aber links die Postings sind auch jene aus den Unterkategorien von “Lieblingsdinge”, wobei Du das aber lieber separieren würdest, richtig?

    In dem Falle wird das Plugin nicht unbedingt weiterhelfen, aber ich versuch’s mal auf blauen Dunst und ohne Gewähr.

    In Deinem Template innerhalb der index.php müsste Folgendes stehen:

    if (have_posts()) :
    und kurz danach auch
    while (have_posts()) : the_post();

    Davor müsstest Du jetzt noch einen Befehl absetzen, in der Richtung wie

    if( is_category() )
        query_posts('category_name='.$cat.'&showposts=10');

    Das bewirkt, dass wenn man sich in einer Kategorieübersicht befindet, der sogenannte “Loop” mit Postings gefüllt wird, die auch wirklich nur aus dieser Kategorie stammen. Ich hoffe, das stimmt so, denn es kann auch sein, dass query_posts trotzdem auch Einträge aus untergeordneten Kategorien mit ausgibt. In diesem Falle müsste man wohl eher ein eigenes Query senden, was aber auch ziemlich einfach geht. Aber probier’ das da oben mal aus und sag’ dann mal Bescheid. :-)

  • #6 geschrieben von Erik Range
    vor 4 Jahren

    @Elaine:
    Thanks for the info! I found (and fixed) the bug which made it possible to get wrong parental IDs when pages had to be queried. Please download the new version which is already on the server and give it another try.

  • #7 geschrieben von Elaine
    vor 4 Jahren

    Excellent! Thank you very much! It seems to work just as it should now.

  • #8 geschrieben von Nimue Sionnach
    vor 4 Jahren

    moimoin,

    ich hab’s grad in meiner buddelkiste ausprobiert, leider ohne sichtbare resultate.
    da ich bereits seit moanten auf der suche nach einer lösung bin und nichtsgefunden habe, dachte ich eben an die zweite sidebar, deren einbindung mit deinem plugin leichter wäre.

    solltest du natürlich eine bessere lösung haben, ich bin ganz ohr und auge :-)

    schade, daß amn das nicht sozusagen über die “tiefe” machen kann…. das mama-kat. immer nur die eigenen inhalte zeigt, nicht die der kinder-kat. mit.

    ich bin ja auch nicht die einzige, die dieses problem hat, aber bisher ist in keinem forum eine lösung gefunden worden.

    leider versteh ich zuwenig von php.

  • #9 geschrieben von Erik Range
    vor 4 Jahren

    Na, so aus der Not heraus hier mal eben ein fix zusammengeschustertes Plugin. Momentan ist’s noch nackt und neugeboren, kriegt hier dann aber auch noch eine eigene Seite und – irgendwann mal – zwei, drei Features bzw. Einstellungs-Möglichkeiten spendiert.

    Derzeit funktioniert das Ganze erstmal so: Plugin (“CleanCat”) aktivieren und fertig. In Kategorien werden dann keine Beiträge mehr aus Unterkategorien angezeigt.

    Downloaden kann man das hier:

  • #10 geschrieben von Nimue Sionnach
    vor 4 Jahren

    ja, wie geil ist das den?!?!

    ich teste es gleich aus und bau dir den “Hero of the month”-altar :-)

    vielen, vielen dank!

  • #11 geschrieben von Nimue Sionnach
    vor 4 Jahren

    kleiner Nachtrag… irgendwie verträgt sich das Plugin nicht mit den Pages, die werde nicht mehr angezeigt.

    Aber da Prinzip ist so korrekt, funzt einwandfrei. *froi*

  • #12 geschrieben von Erik Range
    vor 4 Jahren

    Na, dann zieh’s Dir lieber nochmal. Hab’ da grad noch was gefixed. Von dem Bug sollte zuvor auch die Startseite betroffen gewesen sein. Das kommt davon, wenn man auf die Schnelle was zwischendurch so hinklatscht. ;-)

    Wenn noch Fehler sind, Vorschläge oder sowas, dann bitte hier lieber hier rein, sonst überschneidet sich das so. :-)

  • #13 geschrieben von Jaks
    vor 4 Jahren

    I installed your plugin so I can display content in the sidebar for specific categories and all posts in that category.

    Here is the code I’m trying to use back I get database errors.

    Here is the error I am getting
    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 ')' at line 3]
    SELECT category_parent FROM wp_categories WHERE cat_ID IN ()

    Warning: Invalid argument supplied for foreach() in /home/myaccount/public_html/stories/wp-content/plugins/is_child.php on line 156

    I’m using the latest stable version of WP. It would be great if I could get this to work so for any help I thank you.

    Jaks

  • #14 geschrieben von Jaks
    vor 4 Jahren

    Hmmm… it didn’t show the php code so i will leave the php tags of this time.

    Here is the code I’m using once more.

    if (is_child ('category-name')) include(TEMPLATEPATH . '/content-for-category.php');

  • #15 geschrieben von Erik Range
    vor 4 Jahren

    The funny thing about this is that I experienced the same error a few minutes ago while doing some extended testing. Occured to me while browsing the archives of a customer’s WP. On which site did this happen to you? A regular category/page/posting? If so, I’d had to dig some deeper into this. If it was the archives, too, then just go ahead and download the fixed 1.1.2 version which is live now. Well, either way you should do this as it fixes that error once and for all. ;-)

  • #16 geschrieben von Jaks
    vor 4 Jahren

    Awesome Erik, That did the trick. :D

    If there was only a better way to work with and manage javascript ads and other includes instead of hardcoding every time.:(

    Anyway, thank you very much my friend.
    Jaks

  • #17 geschrieben von Erik Range
    vor 4 Jahren

    No sweat. ;-) Regarding javascript ads, I know there are some plugins for Google’s AdSense around, but seems not everybody wants Google on his site (for obvious reasons). For ads in the sidebar you might try using widgets, but I don’t know if there’s been already some ad-widget released. If on the other side you want to do some serious ads-management, you should definetly choose PHPAdsNew. All free, open source and all features you could possibly want or need or even think of. ;-)

  • #18 geschrieben von Jaks
    vor 4 Jahren

    Ya I have a couple ad scripts, PHPAdsNew is definitely not my first choice. Way too complicated to set up proper zones etc.. I’ve tried it out quite a few times hoping they woould simplify it but they never seem to do it. I have Revsense and another one that I can’t remember the name of Ad Manager Pro I think. Both are very simple to use. The only drawback is the cost, since PHPAdsNew is free.

    Anyway, This is my first WP site so I’m just getting the feel of it and figuring out how to customize it to my needs for erotic stories. So far so good.

    Thanks again for the Plugin and the help. :D
    Jaks

  • #19 geschrieben von Angelle
    vor 4 Jahren

    Omg das ist DER Plugin, den ich seit einem halben Jahr suche *vor Freude umkippt* Habe echt schon die Hoffnung aufgegeben.
    Danke, danke danke danke danke danke danke….danke!!! :)

  • #20 geschrieben von jens
    vor 4 Jahren

    hallo, das klingt ja alles ganz gut.. ich weiß nur nicht ob das plugin die lösung meines problems ist.. ich suche schon ewig nach einer lösung um eine child cat zu verbergen und sie erst sichtbar zu machen wenn ich auf die parent cat klicke… dann kann man die child cat anklicken/browsen..
    und umgekehrt nach verlassen der parent cat werden die childs wieder unsichtbar…

    ich hoffe das war verständlich? wäre prima wenn mir geholfen werden kann…
    (das Fold Category List plugin hilft mir leider nicht, da ich .current_page_item a:before, .current-cat a:before dann nicht mehr im css ansprechen kann)

    jens

  • #21 geschrieben von Val
    vor 4 Jahren

    History says the latest is 1.1.2, but the file I downloaded from the link just now says 1.1.1 in its header comment blcok. Is the comment out-of-date, or the file?

    Thanks!

  • #22 geschrieben von Casey
    vor 3 Jahren

    I’m trying to dynamically determine if the page is a child by getting the id of the page at run-time. The problem is that it’s always returning false, unless I manually type the ID to pass to the function. Here’s some of my code:


    post->ID;

    if (is_child($page_id)){
    echo "True";
    }
    }
    ?>

    Any ideas on why it always returns false when I know for a fact that the page is a child?

  • #23 geschrieben von Birgit
    vor 3 Jahren

    Thank you so much for this! Goes perfectly along with the dynamic highlighted menu. Happy New Year!

  • #24 geschrieben von Rirath
    vor 3 Jahren

    Thanks man, I’ll probably be using this to get only certain categories to display in ascending order.

  • #25 geschrieben von Jessica
    vor 3 Jahren

    Hi,

    Thanks for this fantastic plugin! It’s exactly what I need for building my navigation menus.

    One thing — I think I may have found a bug when running is_child() on a page. It was always returning true on grandchild pages, even when I had set recursive to FALSE. I changed 2 lines in your code, and it now seems to work correctly:

    line 97:

    $allCats[] = $post->ID;

    changed to:

    $allCats[] = $post->post_parent;

    line 113:

    if( ( is_single() ) && !$doRecursive )

    changed to:

    if( ( is_single() || is_page() ) && !$doRecursive )

  • #26 geschrieben von Daniel Hardy
    vor 3 Jahren

    Hi,

    The plugin is great, it works as it’s supposed to. Although can I suggest one alteration, or ask how to make such an alteration myself.

    If I have the code is_child ( 10 ), it will return true on the page ‘10′ itself, I’d like it be be false on the parent page itself. If at all possible.

  • #27 geschrieben von docflo
    vor 3 Jahren

    Oh Mann! Danke Danke Danke! 1000 Dank!

  • #28 geschrieben von yahoo
    vor 3 Jahren

    um… buoni, realmente buoni luogo e molto utile;)

  • #29 geschrieben von Dan
    vor 3 Jahren

    Thanks for this plugin! It does exactly what I needed to get my navigation working properly across the whole site.

    If anyone is having problems setting up a multi-page site with a static front page and a separate Blog section, and having the “current_page” classes added to the right menu items, let me know. I can probably help out. I’ve been trying to get this working for weeks now and this plugin was the final piece I needed to get it working.

    You can contact me here:
    http://www.sinch.net/contact

  • #30 geschrieben von Green
    vor 3 Jahren

    Nice plugin! Thank you!

  • #31 geschrieben von Ricky
    vor 3 Jahren

    This is PERFECT. Exactly what I was looking for. Implemented on my under construction website very easily and quickly!

  • #32 geschrieben von 3stripe
    vor 3 Jahren

    Brilliant work. This should be part of the WP core :)

  • #33 geschrieben von Adrian
    vor 3 Jahren

    Hi,

    I am new to blogging in general and have been trying to hack my way through creating my own blog.

    I am a teacher and have set up categories for all my individual classes. I am now moving into a new Academic year and want to move the cateogories into another part of my sidebar to make room for my new classes.

    All the current cateogires are children of “2006-7″ category.

    I need the code for displaying only categories of the “2006-7″ category – the category id is 20.

    I would really apprchiate it if you could help – this plugin is very promising – I just lack the know-how!

    Thanks for your time

    Adrian

  • #34 geschrieben von Chris
    vor 3 Jahren

    This is an absolutely great plugin, but I am wondering if you could post the 1.1.2 version? The zip file still contains the 1.1.1 version and I am getting the archives error that commenter #14 also had. Thanks for the great plugin.

  • #35 geschrieben von delook
    vor 2 Jahren

    Nice plugin, Erik!
    but unfortunately it breaks in WP2.3 because there’s a HUGE restructuring made on the *_categories table (splitted in _terms, _term_relationships and _term_taxonomy)
    I’ll try to fix that because I definitely need that plugin – or is there already someone donig this work?!

  • #36 geschrieben von paul
    vor 2 Jahren

    i will pay you to update is_child to work with WP2.3. please contact me if you’re interested. i love the plugin and i need it for some projects i’m doing right now. thanks!

  • #37 geschrieben von wickedbob
    vor 2 Jahren

    I have updated is_child script to work with wordpress 2.3 and it is available for download here:
    http://rapidshare.com/files/77088380/is_child.php.html

    NB: I haven’t tested this extensively and am not a php or wordpress expert. I currently have it running on my wordpress 2.3 installation and things appear to be working fine.

  • #38 geschrieben von szymon
    vor 2 Jahren

    How to disable checking category id? (I need only to check page id.)