Wordpress最近的帖子/项目

Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.

I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.

<?php
            // Default arguments
            $args = array(
                'posts_per_page' => 3, // How many items to display
                'post__not_in'   => array( get_the_ID() ), // Exclude current post
                'no_found_rows'  => true, // We don't ned pagination so this speeds up the query
            );

            // Check for current post category and add tax_query to the query arguments
            $cats = wp_get_post_terms( get_the_ID(), 'category' ); 
            $cats_ids = array();  
            foreach( $cats as $wpex_related_cat ) {
                $cats_ids[] = $wpex_related_cat->term_id; 
            }
            if ( ! empty( $cats_ids ) ) {
                $args['category__in'] = $cats_ids;
            }

            // Query posts
            $wpex_query = new wp_query( $args );

            // Loop through posts
            foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
            <div class="col-md-4 related-post">
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>
                <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a>
            </div>    
            <?php
            // End loop
            endforeach;

            // Reset post data
            wp_reset_postdata(); ?>

In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.

Here is my code for new post type:

// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );    
add_post_type_support( 'bw_projects', 'custom-fields' );    
function create_post_type() {
  register_post_type( 'bw_projects',
    array(
      'labels' => array(
        'name' => __( 'Projects' ),
        'singular_name' => __( 'Projects' )
      ),
      'public' => true,
      'has_archive' => true,
      'taxonomies' => array( 'category','post_tag'),
    )
  );
}

What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?

Thanks in advance.

You can get any post type that you require using get_posts();

<?php $args = array(
'posts_per_page'   => 5,
'offset'           => 0,
'category'         => '',
'category_name'    => '',
'orderby'          => 'date',
'order'            => 'DESC',
'include'          => '',
'exclude'          => '',
'meta_key'         => '',
'meta_value'       => '',
'post_type'        => 'projects',
'post_mime_type'   => '',
'post_parent'      => '',
'author'       => '',
'author_name'      => '',
'post_status'      => 'publish',
'suppress_filters' => true 
);

$posts_array = get_posts( $args ); ?>

Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.

You can find more info in the codex.

Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.

To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:

$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
  echo "<h1>" . $post->post_title . "</h1><br>";
  echo "<p>" . $post->post_content . "</p><br>";
} 

Or a really concise way of doing all of the above would be something like:

$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
   echo "<h1>" . $post->post_title . "</h1><br>";
   echo "<p>" . $post->post_content . "</p><br>";
 }