Support » Fixing WordPress » show posts, based similar category as the listed pages category

  • Resolved c001os



    I want to show posts, that are in a similar category as the listed pages. Here is my code (just the php not the html). What is wrong?

    <?php $pages = get_pages(array('child_of' => 13)); ?>
        <?php foreach ($pages as $page): ?>
            <?php echo $page->post_title; ?>
            <?php echo $page->post_excerpt; ?>
            <?php echo $page->post_content; ?>
                <?php $categories = get_the_category($page->ID);?>
                    <?php query_posts('post_type=projekt&category_name=.$categories.&showposts=1');?>
                        <?php while (have_posts()) : the_post(); ?>
                            <?php the_title(); ?>
        <?php endwhile; endforeach; ?>

    Thanx for the help, I investigate this all the day and i can’t found a solution….

Viewing 7 replies - 1 through 7 (of 7 total)
  • Michael


    Forum Moderator

    the string concatenation is wrong;

    get_the_category() returns an object (array), not a string;

    ‘showposts’ is deprecated, and should be replaced with ‘posts_per_page’;

    you could
    either only use the ‘first’ category, like so:

    <?php $categories = get_the_category($page->ID); $cat_name = $categories[0]->slug; ?>

    and use that with the query, like so:

    <?php query_posts('post_type=projekt&category_name='.$cat_name.'&posts_per_page=1'); ?>

    or collect the category ids in an array, like so:

    <?php $categories = get_the_category($page->ID);
    $cat_ids = array(); foreach( $categories as $cat ) { $cat_ids[] = $cat->term_id; } ?>

    and use that in the query like so:

    <?php query_posts( array('post_type' => 'projekt', 'category__in' => $cat_ids, 'posts_per_page' =>1 ) ); ?>

    (untested) ;

    ideally, use WP_Query() instead of query_posts()


    Its just perfect! You are my saver! Thanx for the explanation too i get lights now!

    Big thanx!!!!

    One last question…

    Can i somehow add a class for these posts dynamicaly, based on the category?

    Its my shame, but i needed to ask this… 🙂

    Anyway my basic question is resolved!!! Thanx again!



    Forum Moderator

    you will get a few css classes including category classes if you use post_class() within the post div; for more detailed suggestions, I would need to see the full code inclusive the html.

    if you want to add cat related css classes (like .cat-this-cat-slug) for the whole div of related posts, you can use the following code somewhere between <?php $categories = get_the_category($page->ID);?> and <?php while (have_posts()) : the_post(); ?>

    example (your existing div might be different):

    <div class="related-posts<?php foreach( $categories as $cat ) { echo ' cat-' . $cat->slug; } ?>">
    the loop code here

    Thanx, i give a try!

    Worked perfectly! You are my prophet! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘show posts, based similar category as the listed pages category’ is closed to new replies.