Forums

[resolved] Getting post category to filter to parent page name (8 posts)

  1. kwalsh121
    Member
    Posted 11 months ago #

    Hi All,

    I was wondering if someone could point me in the right direction to find a solution to the following:

    I am using the following bit of code:

    <?php $loop = new WP_Query( array( 'post_type' => 'portfolio', 'category_name' => 'project_photos', 'posts_per_page' => 5 ) ); ?>

    What I would like to do is amend the code so it selects custom posts that have a category that is equal to the parent page name. So for example if the parent page is 'fruit' and I have a child page with the name 'bananas', I can create a template for the 'bananas' page that shows all the custom posts I have assigned the category 'fruit'.

    I would like to use this template for several pages and would prefer not to create a tempalte for each one. I've looked for ages but I'm not really sure what I'm searching for.

    Any assistance would be greatly appreciated :o)

  2. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    Try:

    <?php
    $my_page_parent = $post->post_parent;
    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'category_name' => $my_page_parent, 'posts_per_page' => 5 ) );
    ?>
  3. kwalsh121
    Member
    Posted 11 months ago #

    Hi Esmi,

    Thank you so much for your reply.

    The code doesn't work for me unfortunately - it returns 0 posts. If I amend the code to:
    $my_page_parent = get_the_title($post->post_parent);
    that gets me the information that I need (in the form of 'Project Photos'), however 'category_name' refers to the cateogory's slug (in this case 'project_photos') so they don't match.

    I just need the tag that refers to the category's actual name, not the category's slug. I'm sure it's around here somewhere...

  4. keesiemeijer
    moderator
    Posted 11 months ago #

  5. kwalsh121
    Member
    Posted 11 months ago #

    Hi keesiemeijer,

    Thanks for taking a look at this for me.

    I looked at the Codex, but the description for get_cat_name is:"Retrieve the name of a category from its ID".

    I don't have the category ID, so I don't think this is going to work for me. Tried using get_the_cateogry but that doesn't seem to work either. It's so annoying that this is probably soooooo simple - I just don't know where to look!

  6. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    <?php
    $my_page_parent = get_the_title($post->post_parent);
    $category_id = get_cat_ID($my_page_parent);
    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'cat' => $category_id, 'posts_per_page' => 5 ) );
    ?>
  7. kwalsh121
    Member
    Posted 11 months ago #

    OMG - I was so close to that yesterday (admittedly I took me the best part of the day!). I got so obsessed with getting the names to match, it didn't occur to me to get the ID and match that...genius!

    You are extremely helpful Esmi and also a prolific poster on this site, I’ve seen your name a lot!

    Thank you very, very much. I had just given up, accepted that this was above my skill level and was about to create a separate page for each category. I’ve genuinely learnt from your posts too (on this and others) so thanks again :o)

  8. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    Glad I could help :-)

Reply

You must log in to post.

About this Topic