如果page_id是3,则在页面上显示一些帖子。其他显示一些不同的帖子

时间:2013-03-02 06:29:04

标签: php wordpress-theming wordpress

我只使用page.php这是wordpress中页面的默认模板,我的页眉页脚和侧边栏在所有页面上都是相同的,但主要区域内容正在改变,我希望wordpress为每个特定页面提取。例如,如果页面是关于我们..应该显示关于我们类别的帖子等等...


我想知道我是否可以通过条件语句实现这一点,或者我必须为每个页面制作单独的模板。

我尝试了类似......

<?php if (page_id ==11 ):
 // post loop to display all fetch all post of category id 4
  $page_query = new WP_Query('post_type=post&cat=4'); ?>
<?php  while ($page_query->have_posts()) :
 $page_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
<?php if ( has_post_thumbnail() ) {
    the_post_thumbnail('thumbnail');
} ?>
<?php the_content(); ?>
<?php endwhile;  endif;

wp_reset_postdata();

?>

这是我在page.php上的循环,但它似乎不起作用!

2 个答案:

答案 0 :(得分:0)

哇...不确定这是否有帮助,但比你现在的更整洁,更干净

<?php
if (page_id == 11){
   $page_query = new WP_Query('post_type=post&cat=4');
    while ($page_query->have_posts()){
        $page_query->the_post();
        echo "<a href=/"". the_permalink() ."/" rel=/"bookmark/"><".the_title()."</a>";
        if ( has_post_thumbnail() ) {
            the_post_thumbnail('thumbnail');
        }
        the_content();
    }
}
wp_reset_postdata();
?>

尽管如此,你还不清楚哪些不起作用,所以很难说出什么不起作用......

答案 1 :(得分:0)

我看到这从未解决过,我无事可做,所以我希望这会在不久的将来对某人有所帮助。

如果您要显示与“关于我们”页面相关的帖子,则应使用该页面名称来命名。这样,您可以将上面的代码更改为以下代码:

    global $post;
    $pagename = get_the_title($post->ID);

    $args = array(
        'post_type' => 'post',
        'category_name' => $pagename,
    );
    $page_query = new WP_Query($args);
         while ( $page_query->have_posts() ) : $page_query->the_post();?>
                <a href="<?php the_permalink(); ?>" rel="bookmark">
                    <?php the_title()?>
                </a>
             <?php
                if ( has_post_thumbnail() ) {
                     the_post_thumbnail('thumbnail');
                }
                the_content();

          endwhile;