WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] get category ID in function.php

[Resolved] get category ID in function.php

  • 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

Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] get category ID in function.php’ is closed to new replies.
Skip to toolbar