WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get category ID in function.php (6 posts)

  1. globalreporter
    Member
    Posted 1 year ago #

    Hi, I'm modifying my function.php theme file to show posts that match these criterias:
    1) posts of the user logged in
    2) posts of the same category of the post shown
    I managed to match the first criteria, but not the second one.
    This is the code of the function:

    <?php
    function get_related_author_posts() {
        global $authordata, $post, $current_user;
        get_currentuserinfo();
        $authors_posts = get_posts( array( 'author' => $current_user->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10, 'category' => ?????? ) );
        $output = '<ul>';
        foreach ( $authors_posts as $authors_post ) {
            $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
        }
        $output .= '</ul>';
        return $output;
    }
    ?>

    I should pass the ID of the category, into the get_posts parameters, but I don't know how to get the category ID of the post that the user is viewing.

    So, simplyfing, what I substitute the question marks with?
    $authors_posts = get_posts( array( 'author' => $current_user->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10, 'category' => ?????? ) );

    Thank you very much! :)

    Rocco

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    I don't know how to get the category ID of the post that the user is viewing

    Assuming you have the post ID from the original Loop, you could use http://codex.wordpress.org/Function_Reference/get_categories

  3. globalreporter
    Member
    Posted 1 year ago #

    thank you, but I can't understand how to use this tag!
    I tried to add this, but it doesn't work!

    $categories=get_categories($post->ID);
    
        $authors_posts = get_posts( array( 'author' => $current_user->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 15, 'category' => $categories->cat_ID ) );

    What's wrong?
    Thank you again.

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

    different approach:
    http://codex.wordpress.org/Function_Reference/wp_get_post_categories

    you could try, for instance:

    $authors_posts = get_posts( array( 'author' => $current_user->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10, 'category__in' => wp_get_post_categories($post->ID) ) );
  5. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    My bad! Use http://codex.wordpress.org/Function_Reference/get_the_category instead. There are some examples on that page that should give you a head start.

  6. globalreporter
    Member
    Posted 1 year ago #

    I tried both with get_categories and get_the_category but don't work (and it's likely that I don't know how to use them).

    the solution from alchymyth is simple and effective: works like a charm!

    thank you all!

Topic Closed

This topic has been closed to new replies.

About this Topic