WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] show posts, based similar category as the listed pages category (8 posts)

  1. c001os
    Member
    Posted 1 year ago #

    Hi!

    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....

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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)

    http://codex.wordpress.org/Function_Reference/get_the_category ;
    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    ideally, use WP_Query() instead of query_posts() - http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1

  3. c001os
    Member
    Posted 1 year ago #

    OMG!

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

    Big thanx!!!!

  4. c001os
    Member
    Posted 1 year ago #

    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... :)

  5. c001os
    Member
    Posted 1 year ago #

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

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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.

    http://codex.wordpress.org/Function_Reference/post_class

    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
    </div>
  7. c001os
    Member
    Posted 1 year ago #

    Thanx, i give a try!

  8. c001os
    Member
    Posted 1 year ago #

    Worked perfectly! You are my prophet! :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags