2016-07-24 5 views
0

は、私は(テーマのパネルから定義されたカテゴリを呼び出す)、このようなコードを持って与える方法:

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

すべてが正常に動作しますが場合にISSETを追加する必要がありますカテゴリは定義されませんでした。

私はこのようなISSETを与える:

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

しかし、動作しませんでした。 誰でも助けてくれますか?私はPHPが初めてです。

ありがとうございました。ここで

フルコード:

<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> 

答えて

0

はこのようにしてみてください。

$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'; 
} 

更新

ここで更新され、質問に応じたソリューションを働いている:

<?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;?> 
+0

残念ながら私はこのエラーが発生します。注意:5行目のオブジェクト以外のプロパティを取得しようとしています。5行目は$ cat_idです。 – Mailmulah

+0

私は同じコードをテストしました。あなたのコードを共有できますか? – jakob

+0

更新された答えを確認してください。私はそれをテストし、それは正常に動作します – jakob

関連する問題