【问题标题】:Wordpress Recent Posts/ProjectsWordpress 最近的帖子/项目
【发布时间】:2017-05-11 14:15:04
【问题描述】:

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

我不熟悉创建自己的主题。我目前在我的single.php文件中使用以下代码,根据博文的类别调用相关文章。

<?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(); ?>

在我的新帖子类型“项目”中,我想调用相关项目。我假设这将是非常相似的代码,除了我需要停止它寻找帖子而是寻找我的项目。

这是我的新帖子类型代码:

// 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 而不再查找“帖子”,我需要在我的第一个代码 sn-p 中进行哪些更改。我尝试自己玩弄并更改某些行,但我引起了更多问题并停止了页面加载。这是否正确,我可以使用相同的代码,稍微改变一下,还是我需要完全不同的东西?

提前致谢。

【问题讨论】:

    标签: php html arrays wordpress blogs


    【解决方案1】:

    您可以使用 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 ); ?>
    

    只需将“post_type”参数设置为您的自定义帖子类型,即可仅获取这些帖子。还可以设置发帖数、分类过滤等。

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

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

    要显示帖子,您可以使用简单的 foreach 将它们大量生成。 You#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>";
     }
    

    【讨论】:

    • 感谢您的回复。我用 get_posts();带有所需的参数,但什么也没有出现。所以我尝试了调用普通帖子的示例,其中再次返回空白。我错过了什么吗?
    • 如果这不起作用,那么您的网站有问题。那是直接来自法典的基本代码。当您使用此代码时,您所做的就是复制 WP 已经在幕后所做的事情。你能用你的完整代码制作一个codepen或gist吗?
    • 是的,这就是我的假设:(。好的,我会这样做,谢谢。
    • 非常感谢@Phill 的所有帮助,问题现在解决了。
    • 我提出了一个新问题。 [链接] (stackoverflow.com/questions/43977069/…) 如果您有任何贡献,我们将不胜感激。
    猜你喜欢
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    相关资源
    最近更新 更多