2013-04-22 92 views
1

我有一个WordPress的主题,我买了很多不同的小工具来与它。 我有一个下面的代码,它所做的是显示最近的帖子。 我需要对它进行更改,实际上是两个,而不是向我展示最近的POST,我需要它向我展示最近的投资组合项目以及仅限某个类别。编辑我最近的帖子Widget

<?php 
// =============================== My Recent Posts (News widget) ====================================== 
class MY_PostWidget extends WP_Widget { 
    /** constructor */ 
    function MY_PostWidget() { 
     parent::WP_Widget(false, $name = 'My - Recent Posts'); 
    } 

    /** @see WP_Widget::widget */ 
    function widget($args, $instance) {  
     extract($args); 
     $title = apply_filters('widget_title', $instance['title']); 
       $category = apply_filters('widget_category', $instance['category']); 
       $linktext = apply_filters('widget_linktext', $instance['linktext']); 
       $linkurl = apply_filters('widget_linkurl', $instance['linkurl']); 
       $count = apply_filters('widget_count', $instance['count']); 
     ?> 
       <?php echo $before_widget; ?> 
        <?php if ($title) 
         echo $before_title . $title . $after_title; ?> 

         <?php $temp = $wp_query; 
            $wp_query= null; 
            $wp_query = new WP_Query(); ?> 

           <ul class="latestpost"> 

           <?php $querycat = $category; ?> 

           <?php $wp_query->query("showposts=". $count ."&category_name=". $querycat); ?> 

           <?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?> 
           <li> 
        <?php if(has_post_thumbnail()) { 
          echo '<figure class="featured-thumbnail"><a href="'; the_permalink(); echo '">'; 
          echo the_post_thumbnail('small-post-thumbnail'); 
          echo '</a></figure>'; 
          } 
          ?> 
        <time datetime="<?php the_time('Y-m-d\TH:i'); ?>"><a href="<?php the_permalink(); ?>"><?php the_time('[m-d-Y]'); ?></a></time> 
        <?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,13);?> 
           </li> 
           <?php endwhile; ?> 
           </ul> 
           <?php endif; ?> 

           <?php $wp_query = null; $wp_query = $temp;?> 


           <!-- Link under post cycle --> 
           <?php if($linkurl !=""){?> 
            <a href="<?php echo $linkurl; ?>" class="button"><?php echo $linktext; ?></a> 
           <?php } ?> 


       <?php echo $after_widget; ?> 

     <?php 
    } 

    /** @see WP_Widget::update */ 
    function update($new_instance, $old_instance) {    
     return $new_instance; 
    } 

    /** @see WP_Widget::form */ 
    function form($instance) {    
     $title = esc_attr($instance['title']); 
      $category = esc_attr($instance['category']); 
      $linktext = esc_attr($instance['linktext']); 
      $linkurl = esc_attr($instance['linkurl']); 
      $count = esc_attr($instance['count']); 
     ?> 
     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p> 

     <p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Category Slug:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo $category; ?>" /></label></p> 

     <p><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Posts per page:'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" /></label></p> 

      <p><label for="<?php echo $this->get_field_id('linktext'); ?>"><?php _e('Link Text:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linktext'); ?>" name="<?php echo $this->get_field_name('linktext'); ?>" type="text" value="<?php echo $linktext; ?>" /></label></p> 

      <p><label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('Link Url:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" type="text" value="<?php echo $linkurl; ?>" /></label></p> 
     <?php 
    } 

} // class Widget 
?> 

这是现在的样子在控件窗格:

Widget

回答

1

在这里这条线

<?php $wp_query->query("showposts=". $count ."&category_name=". $querycat); ?> 

<?php $wp_query->query("showposts=". $count ."&category_name=". $querycat."&post_type=portfolio"); ?> 
+0

谢谢Bhumi,我试过了,出于某种原因,它不起作用。在主题中有一个名为“集合”的部分,当点击该部分时,它将进入投资组合部分(因为它链接到/ portfolio-view/...)。所以我想我必须像你说的那样放置那部分代码,但它不起作用。你认为还有其他原因吗? – 2013-04-23 09:39:57

0

我公顷取代有一个类似你的小部件。我能够从上面的答案中获得帮助。希望这会帮助你或其他人。

<?php 
// =============================== My Recent Portfolio (News widget) ====================================== 
class MY_PortfolioWidget extends WP_Widget { 
    /** constructor */ 
    function MY_PortfolioWidget() { 
     parent::WP_Widget(false, $name = 'My - Recent Portfolio'); 
    } 

    /** @see WP_Widget::widget */ 
    function widget($args, $instance) {  
     extract($args); 
     $title = apply_filters('widget_title', $instance['title']); 
       $category = apply_filters('widget_category', $instance['category']); 
       $post_format = apply_filters('widget_post_format', $instance['post_format']); 
       $linktext = apply_filters('widget_linktext', $instance['linktext']); 
       $linkurl = apply_filters('widget_linkurl', $instance['linkurl']); 
       $count = apply_filters('widget_count', $instance['count']); 
       $excerpt_count = apply_filters('widget_excerpt_count', $instance['excerpt_count']); 

     ?> 
       <?php echo $before_widget; ?> 
        <?php if ($title) 
         echo $before_title . $title . $after_title; ?> 



         <?php if($post_format == 'post-format-standard') { 

          $args = array(
             'showposts' => $count, 
             'portfolio_category' => $category, 
             'post_type' => portfolio, 
             'tax_query' => array(
             'relation' => 'AND', 
              array(
               'taxonomy' => 'post_format', 
               'field' => 'slug', 
               'terms' => array('post-format-aside', 'post-format-gallery', 'post-format-link', 'post-format-image', 'post-format-quote', 'post-format-audio', 'post-format-video'), 
               'operator' => 'NOT IN' 
              ) 
             ) 
             ); 

         } else { 

          $args = array(
           'showposts' => $count, 
           'category_name' => $category, 
           'post_type' => portfolio, 
           'tax_query' => array(
           'relation' => 'AND', 
            array(
             'taxonomy' => 'post_format', 
             'field' => 'slug', 
             'terms' => array($post_format) 
            ) 
           ) 
           ); 

         } ?> 




         <div class="latestpost-container"> 
          <?php $wp_query = new WP_Query($args); ?> 
           <ul class="latestpost"> 



           <?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?> 

           <li class="clearfix"> 
            <?php if(has_post_thumbnail()) { ?> 
             <?php 
             $thumb = get_post_thumbnail_id(); 
             $img_url = wp_get_attachment_url($thumb,'full'); //get img URL 
             $image = aq_resize($img_url, 200, 146, true); //resize & crop img 
             ?> 
             <figure class="featured-thumbnail"> 
              <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /></a> 
             </figure> 
            <?php } ?> 
             <div class="post-meta"> 
              <time datetime="<?php the_time('Y-m-d\TH:i'); ?>"><?php the_time('l, j F Y'); ?></time> 
              <?php _e('By', 'theme1796'); ?> <?php the_author_posts_link() ?> 
             </div> 
             <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to', 'theme1796');?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> 
            <?php if($excerpt_count!="") { ?> 
             <div class="excerpt"> 
              <?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$excerpt_count);?> 
             </div> 
             <div class="post-footer"> 
              <?php comments_popup_link('No Comments', '<span>1</span> Comment', '<span>%</span> Comments', 'comments-link', 'Comments are closed'); ?> 
              <a href="<?php the_permalink() ?>" class="link"><?php _e('Read more', 'theme1796'); ?></a> 
             </div> 
            <?php } ?> 
           </li> 
           <?php endwhile; ?> 
           </ul> 
           <?php endif; ?> 

           <?php $wp_query = null; $wp_query = $temp;?> 

           <!-- Link under post cycle --> 
           <?php if($linkurl !=""){?> 
            <a href="<?php echo $linkurl; ?>" class="link"><?php echo $linktext; ?></a> 
           <?php } ?> 

         </div>      




       <?php echo $after_widget; ?> 

     <?php 
    } 

    /** @see WP_Widget::update */ 
    function update($new_instance, $old_instance) {    
     return $new_instance; 
    } 

    /** @see WP_Widget::form */ 
    function form($instance) {    
     $title = esc_attr($instance['title']); 
      $category = esc_attr($instance['category']); 
      $post_format = esc_attr($instance['post_format']); 
      $linktext = esc_attr($instance['linktext']); 
      $linkurl = esc_attr($instance['linkurl']); 
      $count = esc_attr($instance['count']); 
      $excerpt_count = esc_attr($instance['excerpt_count']); 
     ?> 
     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'theme1796'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p> 

     <p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Category Slug:', 'theme1796'); ?> <input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo $category; ?>" /></label></p> 

      <p><label for="<?php echo $this->get_field_id('post_format'); ?>"><?php _e('Post format:', 'theme1796'); ?><br /> 

     <select id="<?php echo $this->get_field_id('post_format'); ?>" name="<?php echo $this->get_field_name('post_format'); ?>" style="width:150px;" > 
      <option value="post-format-standard" <?php echo ($post_format === 'post-format-standard' ? ' selected="selected"' : ''); ?>>Standard</option> 
     <option value="post-format-aside" <?php echo ($post_format === 'post-format-aside' ? ' selected="selected"' : ''); ?>>Aside</option> 
      <option value="post-format-quote" <?php echo ($post_format === 'post-format-quote' ? ' selected="selected"' : ''); ?> >Quote</option> 
      <option value="post-format-link" <?php echo ($post_format === 'post-format-link' ? ' selected="selected"' : ''); ?> >Link</option> 
      <option value="post-format-image" <?php echo ($post_format === 'post-format-image' ? ' selected="selected"' : ''); ?> >Image</option> 
     <option value="post-format-gallery" <?php echo ($post_format === 'post-format-gallery' ? ' selected="selected"' : ''); ?> >Gallery</option> 
      <option value="post-format-audio" <?php echo ($post_format === 'post-format-audio' ? ' selected="selected"' : ''); ?> >Audio</option> 
      <option value="post-format-video" <?php echo ($post_format === 'post-format-video' ? ' selected="selected"' : ''); ?> >Video</option> 
     </select> 
     </label></p> 

     <p><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Posts per page:'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" /></label></p> 

      <p><label for="<?php echo $this->get_field_id('excerpt_count'); ?>"><?php _e('Excerpt length (words):'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('excerpt_count'); ?>" name="<?php echo $this->get_field_name('excerpt_count'); ?>" type="text" value="<?php echo $excerpt_count; ?>" /></label></p> 

      <p><label for="<?php echo $this->get_field_id('linktext'); ?>"><?php _e('Link Text:', 'theme1796'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linktext'); ?>" name="<?php echo $this->get_field_name('linktext'); ?>" type="text" value="<?php echo $linktext; ?>" /></label></p> 

      <p><label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('Link Url:', 'theme1796'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" type="text" value="<?php echo $linkurl; ?>" /></label></p> 
     <?php 
    } 

} // class Widget 
?> 
相关问题