• Hello,

    I am developing customized theme wherein :

    $child_pages=get_pages( array('child_of'=>0,'include'=>$parent_three_page->ID,'number'=>3, 'sort_column'=>rand) );

    I am including total 5 page ids but need to show only 3 pages. Is it possible? Please guide me through.

    Thanks,
    Vikram

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    rand needs to be a string: 'sort_column'=>'rand'

    I think the ‘include’ argument overrides number and rand arguments, so you would need a different criteria than ‘include’ to indicate from what pages to randomly get three of them. ‘include’ certainly is incompatible with ‘child_of’.

    Could you make the pool of pages to draw from all children of one particular page? Then your code with a non-zero ID for ‘child_of’ and no ‘include’ argument will work the way you want. Make the parent slug brief so the permalink isn’t too confusing. For example, ‘pg’ could be an acceptable slug for the parent.

    Thread Starter Vikram S Sangotra

    (@vikkram)

    Hello @bcworkz,

    Thanks for your detailed reply. Things at my ends are like :

    a) 2 Main Categories
    b) Both these categories have 4-4 sub categories
    c) These sub categories are having content pages.
    e) I want to show 3 random content pages (from random sub categories) in 2 different sections ie. main categories.
    f) In future Main Categories &/or Sub Categories may increase.

    I may be making it more complex. And I also think there should be simpler way to present it.

    Please advise.

    Thanks again,
    Vikram

    Moderator bcworkz

    (@bcworkz)

    Your get_pages() modified as I suggested can manage this with one small adjustment. For the ‘child_of’ argument, supply an array of IDs representing the parents of all categories which you wish to draw from. You can always add additional IDs as needed in the future.

    You could even have a custom options field or something that supplies the IDs so the code itself does not need to be edited in the future.

    Thread Starter Vikram S Sangotra

    (@vikkram)

    Hi @bcworkz – good morning,

    Ok – I got third level parents and have used it in ‘child_of’ like :

    $parent_four=get_pages( array('parent'=>$parent_three_id, 'child_of'=>$parent_three_id, 'number'=>2,'sort_column'=>rand) );

    Now what happens is number->2 gives me 2 child pages of each parent. So I made little adjustments by displaying 4 columns – till this it is absolutely fine.

    But what if in future when there’ll be 3 or more parents? ?? ?? ??

    Please advise.

    Thanks again,
    Vikram

    Moderator bcworkz

    (@bcworkz)

    So you are getting 4 total results when you want 2? That’s quite odd! I suppose it’s because ‘child_of’ and ‘parent’ arguments were not intended to be used together, so it’s returning two of each. You only need one or the other. ‘parent’ returns only immediate children. ‘child_of’ returns any descendant, no matter how many generations down. Try removing one argument or the other.

    Thread Starter Vikram S Sangotra

    (@vikkram)

    Hello @bcworkz,

    As per your suggestions I tried :

    1) child_of & it doesn’t work with number.
    2) parent it ignores number

    ?? Any more suggestions.

    Thanks again,
    Vikram

    Moderator bcworkz

    (@bcworkz)

    It depends on which version you prefer. If child_of, go ahead and get all pages that match, then limit which ones are used with code within the loop.

    For parent, also pass 'hierarchical' => false, (the default true does not even make sense with parent)

    One or the other should work, but if you still have trouble, all I can think of is to use the more generic WP_Query object to get pages. The arguments are a little different (as well as how the loop works), so review the docs.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_pages & number’ is closed to new replies.