douli8428 2016-07-24 08:51
浏览 24

如何在get_cat_id上给出isset($ up_options-> category1)

I have a code like this (call a category defined from theme panel):

 <?php query_posts ('ignore_sticky_posts=1&showposts=1&cat='.get_cat_id
 ($up_options->category1)
 ); 

Everything work fine but need to add isset in case the category did not define.

I give isset like this:

 <?php query_posts ('ignore_sticky_posts=1&showposts=1&cat='.get_cat_id(
 (isset($up_options->category1) && $up_options->category1))
 );

But did not work. Can anyone help me? I'm pretty new to PHP.

Thank for any kind of helps.

Here the full code:

 <div class="wrapper">
    <?php query_posts ('ignore_sticky_posts=1&showposts=1&cat='.get_cat_id($up_options->category1));    
     if (have_posts()) :
         while (have_posts()) : the_post();?>
    <div class="inside">
        <div class="title">
            <h5> <a href="<?php the_permalink() ?>" title="<?php  the_title(); ?>"><?php echo substr(the_title('', '', false), 0, 75); ?>...</a> </h5>
        </div>
    </div>
 <?php endwhile; endif; wp_reset_query();?>
 </div>
  • 写回答

1条回答 默认 最新

  • doushang7209 2016-07-24 12:20
    关注

    Try like this:

    $cat_id = get_cat_ID($up_options->category1);
    
    if($cat_id != null){
        query_posts('ignore_sticky_posts=1&showposts=1&cat='.$cat_id);
    } else {
        echo 'Category not defined';
    }
    

    Update

    Here is working solution according to updated question:

    <?php 
        global $up_options;
        $cat_id = get_cat_ID($up_options->category1);
    
        if($cat_id != null) :
            query_posts ('ignore_sticky_posts=1&showposts=1&cat='.$cat_id);    
             if (have_posts()) :
                 while (have_posts()) : the_post();?>
            <div class="inside">
                <div class="title">
                    <h5>
                        <a href="<?php the_permalink() ?>" title="<?php  the_title(); ?>"><?php echo substr(the_title('', '', false), 0, 75); ?>...</a>
                    </h5>
                </div>
            </div>
         <?php endwhile; endif; wp_reset_query(); endif;?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大