Wordpress 最近 Posts/Projects

Wordpress Recent Posts/Projects

您好,我在 Wordpress 中创建了自己的自定义 post 类型,以包含我可以通过我的主题文件调用的项目。

我不熟悉创建自己的主题。我目前在 single.php 文件中使用以下代码来根据博客 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(); ?>

在我的新 post 中输入 "projects" 我想调用相关项目。我假设这将是非常相似的代码,除了我需要停止它寻找 posts 而不是寻找我的项目。

这是我的新 post 类型的代码:

// 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'),
    )
  );
}

我需要在第一个代码片段中更改什么才能查找 bw_projects 而不是再查找 'posts'。我尝试自己玩弄并更改某些行,但我引起了更多问题并停止了页面加载。这是否正确我可以使用相同的代码,稍作改动还是我需要完全不同的东西?

提前致谢。

您可以使用 get_posts();

获得您需要的任何 post 类型
<?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 ); ?>

只需将 'post_type' 参数设置为您自定义的 post 类型,即可仅获取这些 post。还可以设置数量post,分类筛选等

您可以在 codex 中找到更多信息。

或者,如果您想保留与现有代码类似的内容,您可以尝试使用“pre_get_posts”将查询过滤到您的项目。但是,您需要记住添加/删除此过滤器,以便它仅对需要它的查询进行操作。

要显示 post,您可以使用简单的 foreach 来生成它们。你#d 显然想要做一些样式来使布局正确:

$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>";
} 

或者完成上述所有操作的一种非常简洁的方法如下:

$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>";
 }