• Hello dear wordpress people,

    I have several posts with two categories (1 Parent & 1 child(the child is always different))
    What I need to do:

    – get the post childs category
    and!
    – display the rest of the posts in the childs category

    I want to display them on the single.php template

    I am stuck with this

    `<?php
    $recentPosts = new WP_Query();
    $recentPosts->query( array ( ‘category_name’ => ‘my-category-slug’, ‘posts_per_page’ => -1 ) );
    ?>

    <div>
    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php echo get_the_excerpt(); ?>
    </div>
    <?php endwhile; ?>`

    which of course doesn′t give me the current posts child category.

    help is much appreciated

Viewing 11 replies - 1 through 11 (of 11 total)
  • use the function get_the_category to get all categoy list for that post. Non 0 parents will be the child category.

    Uge this go all posts for that child category

    $args = array( 'posts_per_page' => -1, 'offset'=> 1, 'category' => THE ID YOU WILL GET );
    
    $myposts = get_posts( $args );
    Thread Starter nicoretina

    (@nicoretina)

    Great thank you for your help!

    But unfortunately I don′t quite understand what this means
    “Non 0 parents”

    zero parents IS the parent category…
    or am I misunderstanding you?

    If the “category_parent” value is non zero, then its a child category.

    Thread Starter nicoretina

    (@nicoretina)

    yes ok….I manage to list all child categories of the posts parent.

    But I didn′t arrive at listing ONLY the posts of the childscategory of the post (hope this is clear).

    My structure ist like this

    -parentcat

    – childcat (with 8 post) —> in each of these posts I want to display all the other 7 excerpts

    – childcat (with 6post)—> in each of these posts I want to display all the other 5 excerpts

    – childcat (with 11post)—> in each of these posts I want to display the other 10 excerpts

    By the code I shared, you can see the posts from child category only.

    Thread Starter nicoretina

    (@nicoretina)

    maybe then I have a syntax problem…hang on! Thanks for helping me! …. you put some count or unequal 0 snippet to it???

    sorry I am not such a pro, to just code perfect loops….

    Thread Starter nicoretina

    (@nicoretina)

    ok I have the list of only the other childs category…GREAT…. but I end up with errors when trying to call the other posts in that cat. The only non error comes up when I simply list that categories name…well its my first more complicated loop….

    <?php
    $args = array(

    ‘parent’ => 1
    );
    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
    echo ‘term_id ) . ‘”>’ . $category->name . ‘‘;
    }
    ?>

    Kindly check the flow I already shared.

    Thread Starter nicoretina

    (@nicoretina)

    yes sorry…but I only get errors/white page…I simply don′t have enough knowledge of php to replace THE ID YOU WILL GET and output anything

    Thread Starter nicoretina

    (@nicoretina)

    I don′t seem to get it to work, over and over again I only get the category/all posts the other posts in the childs category also have. But that is not what I need.

    I ONLY need the posts of the SAME child-category.

    Maybe I put it in wrong words? Any Ideas?

    <?php
    $args=array(
    
      'parent' => '1',
      'order' => 'ASC',
      'hide_empty' => '0',
      'posts_per_page' => -1,
      'offset'=> 1,
      'category' => $category->term_id,
    
      );
    $categories=get_categories($args);
      foreach($categories as $category) {
        $output = '
    
                    <h3>' . $category->name . '</h3>
    
       ';
    echo $output;
    }
    ?>
    Thread Starter nicoretina

    (@nicoretina)

    Hello dear wordpress Members,

    I took the turn for dummies, as I apparently dont get the simplest things to work.
    Or I don′t seem to understand what is been told here…

    I used advanced custom plugin, this does it manually…well not the best, but with the hidden cryptic for insiders I unfortunately didn′t get anywhere and just felt like an idiot, reading a post 20 times….

    if anyone might be less cryptic, I still very much appreciate it to get a hint what the second post here tries to imply.

    greets!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘get current post child category display all of this category’ is closed to new replies.