0

I don't know exactly where this is going wrong, but what I figure is happening is that the category I am dynamically obtaining based not the BuddyPress Group slug (post categories have matching names) is being converted to an integer instead of a string and as such isn't looping them in.

function chapter_posts() {
    //Get Group Slug
    $chapterSlug    = bp_get_current_group_slug();

    $chapterCatID   = get_cat_id( $chapterSlug );

    //Get posts
    global $post;

    $args = array(
        'cat'           =>  '$chapterCatID,15',
        'posts_per_page'    =>  5
        );
    $posts = get_posts( $args ); ?>

<div class="group_post_start">
    <ul>
        <?php
        foreach( $posts as $post ): setup_postdata($post);
        ?>
        <li>
            <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><h2><?php the_title(); ?></h2></a>
            <div class="group_post_display">
                <p><?php the_excerpt(); ?></p>
                <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>" class="button">Read More</a></p>
            </div>
        </li>
        <?php
        endforeach;
        wp_reset_postdata(); // reset the query ?>
    </ul>
</div><!--.group_post_start-->
<div class="group_post_end">
    <a href="<?php echo get_site_url();?>/blog/category/mompreneurs/<?php echo $postCategory; ?>/" class="button">More</a>
</div><!--.group_post_end-->

<?php
}
add_action( 'bp_before_group_home_content', 'chapter_posts', 50);

On every BuddyPress group page I want to display a blend of both the primary post category (ID = 15) and the category whose slug matches the slug of the BP group.

To test this out what I do is find a place to basically echo out what the $chapterCatID ends up as and when I do this:

<?php echo $chapterCatID; ?> 

...it always ends up as 0, which, according to the codex, is a failure. So what I need to know is how do I get the result of the get_cat_id() function to remain as a string?

  • the function is get_cat_ID, capital ID. also, variables aren't expanded inside single quotes, you are passing literally $chapterCatID, not the value of that variable. – Milo Nov 16 '15 at 19:56
  • Thanks Milo... ...I had actually just added the single quotes in a furious attempt to get it to work. My original attempts passed the variable without the single quotes. I can't believe I messed up the 'get_cat_ID'. slaps forehead Although, I'm pretty sure that even with that it still didn't work... ...it was originally an uppercase ID. – Tony Djukic Nov 16 '15 at 20:31
0

So, I think I've got it figured out... ...get_cat_ID didn't seem to be the correct function. I swapped it out with get_category_by_slug and that eventually got it up and running.

//LINK IN BLOG POSTS THAT MATCH CHAPTER SLUG
function chapter_posts() {
    //Get Group Slug
    $postCategory   = bp_get_current_group_slug();

    //Get posts
    global $post;

    $chapterCatID   = get_category_by_slug($postCategory)->term_id;

    $args = array(
        'cat'           =>  ''. $chapterCatID . ',15',
        'posts_per_page'    =>  4
        );
    $posts = get_posts( $args ); ?>

<div class="group_post_start">
    <ul>
        <?php
        foreach( $posts as $post ): setup_postdata($post);
        ?>
        <li>
            <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><h2><?php the_title(); ?></h2></a>
            <div class="group_post_display">
                <p><?php the_excerpt(); ?></p>
                <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>" class="button">Read More</a></p>
            </div>
        </li>
        <?php
        endforeach;
        wp_reset_postdata(); // reset the query ?>
    </ul>
</div><!--.group_post_start-->
<div class="group_post_end">
    <a href="<?php echo get_site_url();?>/blog/category/mompreneurs/<?php echo $postCategory; ?>/" class="button">More</a>
</div><!--.group_post_end-->

<?php
}
add_action( 'bp_before_group_home_content', 'chapter_posts', 50);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.