douzhangkui2467 2017-05-11 14:15
浏览 30
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dougan1205 2017-05-11 14:21
    关注

    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>";
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?