【问题标题】:WP Single.php If and elseif statement executionWP Single.php If 和 elseif 语句执行
【发布时间】:2012-11-25 15:50:00
【问题描述】:
<?php if (in_category('3')) {
        $args = array(
        'cat' => 'Japan',
        'orderby' => 'meta_value_num', 
        'meta_key' => 'japan_id',
        'order' => 'ASC',
        );
        $the_query = new WP_Query( $args );

        } elseif (in_category('5')) {
        $args = array(
        'cat' => 'Borneo',
        'orderby' => 'meta_value_num', 
        'meta_key' => 'borneo_id',
        'order' => 'ASC',
        );
        $the_query = new WP_Query( $args );}?>

        <?php while ( $the_query->have_posts() ) : $the_query->the_post();?> 
        <?php $status = get_post_meta($post->ID, 'status', true); ?><?php $finishdate = get_post_meta($post->ID, 'finishdate', true); ?>
        <a href="<?php the_permalink(); ?> " rel="favourite" title="<?php the_title(); ?>"><?php the_post_thumbnail('featured-thumbnail'); ?></a>
        <?php endwhile; ?>
        <?php
       // Reset Post Data
       wp_reset_postdata();?>

大家好,我正在尝试做一个 if 和 elseif 来检查:

  • 如果这篇文章属于第 3 类
  • 获取此帖子的信息(类别名称,按此 meta_key 的元值编号按 ASC 顺序排列
  • 否则,如果这篇文章属于第 5 类
  • 按 ASC 顺序获取此帖子的信息...

但是,我不断收到错误消息“致命错误:在...在线...的非对象上调用成员函数 have_posts()。”我想显示所有帖子的所有特色缩略图与 Single Post 属于同一类别。

单篇文章示例:http://ethanlimphotos.com/2012/04/19/orangutan-grabs-legs 特色缩略图滚动应该像这样显示http://ethanlimphotos.com 我希望索引页面上的特色缩略图滚动也可以在单页上工作。 请帮忙,谢谢! :D

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我真的不明白为什么几乎每个人都在想要进行自定义循环时创建一个新对象 :) 这是因为这是您看到示例的方式,还是您有不同的原因(我只是很好奇:) )。

    无论如何,当我需要自定义循环时,我的方式是调用query_posts(),使用普通的have_posts()the_post(),然后使用wp_reset_query() 重置查询变量。因此,当您使用该方法时,您的代码将如下所示:

    <?php if (in_category('3')) {
        $args = array(
        'cat' => 'Japan',
        'orderby' => 'meta_value_num', 
        'meta_key' => 'japan_id',
        'order' => 'ASC',
        );
        query_posts( $args );
    } elseif ( in_category( '5' ) ) {
        $args = array(
            'cat' => 'Borneo',
            'orderby' => 'meta_value_num', 
            'meta_key' => 'borneo_id',
            'order' => 'ASC',
        );
        query_posts( $args );
    }?>
    
    <?php while ( have_posts() ) : the_post(); ?> 
        <?php $status = get_post_meta($post->ID, 'status', true); ?><?php $finishdate = get_post_meta($post->ID, 'finishdate', true); ?>
        <a href="<?php the_permalink(); ?> " rel="favourite" title="<?php the_title(); ?>"><?php the_post_thumbnail('featured-thumbnail'); ?></a>
    <?php endwhile; ?>
    <?php
    // Reset the query data
    wp_reset_query();?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-11
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多