Wordpress显示来自自定义查询的最新类别帖子

时间:2015-09-14 14:33:10

标签: php mysql wordpress

在我的category.php模板中,我想只显示相同类别的最新条目,并且当我执行自定义查询时显示每个条目。

这是我的代码,我该如何修复错误? 代码:

<?php
/**
 * The template for displaying all single posts.
 *
 * @package Mundo Geek
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main-single" class="site-main" role="main">

            <div id="row">
                <?php
                $ultimas = new WP_Query();
                $ultimas -> query('showposts=3');
                while($ultimas -> have_posts()) : $ultimas ->the_post();
                ?>
                    <div class="category_page_last col-xs-12 col-sm-4">
                        <a href="<?php the_permalink(); ?>">
                            <div class="thumb">
                                <?php 
                                if(has_post_thumbnail()){ 
                                //echo '<img src="'.$url.'"/>';x
                                $backgroundImageUrl = "('".wp_get_attachment_url( get_post_thumbnail_id($post->ID) )."')";
                                echo '<div class="category_page_img-background" style="background-image: url'.$backgroundImageUrl.'"></div>';}
                                else{
                                $default_thumb = "('".get_bloginfo( 'stylesheet_directory' )."/images/default-thumbnail.jpg')";
                                echo '<div class="category_page_img-background" style="background-image: url'.$default_thumb.'"></div>';
                                }
                                ?>
                                <div class="encimaimagen"></div>
                            </div>
                            <div class="meta"><h1><?php the_title(); ?></h1></div>
                        </a>
                    </div>
                <?php endwhile; ?>
            </div>

            <div class="row">
                <div class="col-sm-8">
                    <section id="category_page">
                    <?php
                        $ultimas = new WP_Query();
                        $ultimas -> query('showposts=40');
                        while($ultimas -> have_posts()) : $ultimas ->the_post();
                    ?>
                        <article class="col-xs-12 archivo row">
                            <a href="<?php the_permalink(); ?>">
                                <h1><?php the_title(); ?></h1>
                            </a>
                            <div class="hidden-xs col-sm-4 col-md-3"><?php the_post_thumbnail( 'thumbnail' ); ?></div>
                            <div class="col-sm-8 col-md-9"><?php the_excerpt(); wp_reset_postdata();?></div>
                        </article>
                    <?php endwhile; ?>

                    </section>
                </div>
                <div class="col-sm-4 hidden-xs"><?php get_sidebar(); ?></div>
                <div class="index_archivo col-xs-12">
                    <a href="<?php $url = home_url( $path = 'index.php/archivo', $scheme = relative ); echo $url;?>">
                        <h2>Ver todas las entradas</h2>
                    </a>
                </div>
            </div>
        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_footer(); ?>

谢谢你们!

1 个答案:

答案 0 :(得分:1)

要显示特定类别的最新帖子,您的代码需要如下所示:

<ul>
<?php
 global $post;
 $myposts = get_posts('numberposts=5&offset=1&category=3');
 foreach($myposts as $post) :
 setup_postdata($post);
 ?>

<li><?php the_title(); ?> </li>
<?php endforeach; ?>
</ul>

来源:https://wordpress.org/support/topic/recent-posts-from-specific-category