【问题标题】:Avoid pulling custom post type thumbnail to header image Wordpress避免将自定义帖子类型缩略图拉到标题图像 Wordpress
【发布时间】:2018-12-14 14:34:41
【问题描述】:

我尝试用谷歌搜索解决方案并尝试使用 PHP 和 JS,但没有运气,所以我需要 Wordpress 专家的帮助。

其中一个网站为客户工作的问题,我遇到的问题是自定义帖子类型。

其中一个模板将抓取我最后一个海关帖子类型的缩略图作为页眉图像。
如果我在模板中删除我的自定义帖子类型循环,则特征图像将恢复为默认的 WP 页面特征图像。 (

如何避免标题部分从我的自定义帖子类型缩略图中拉出?

Worpdress 版本:4.8

————-这是标题图像如何显示的代码——-

footer.php

</div>
    <?php 
    if(is_category()):
        $cat_id=get_cat_id(single_cat_title('',false));
        $img=get_option("taxonomy_".$cat_id);
        $url=$img['img'];
        else:
        global $post;
        $post_id = $post->ID; 
        if(has_post_thumbnail($post_id)):
            $url=wp_get_attachment_url(get_post_thumbnail_id($post_id));
        else:
            $url=get_template_directory_uri().'/img/banner.jpg';
        endif;
    endif;
        ?>

<script>
    $(function() {
      $.vegas({
        src:'<?php echo $url;?>' , fade:500, 
      });
      $.vegas('overlay', {
        src:'<?php bloginfo('template_directory');?>/img/06.png'
      });
      $('.carousel').carousel()
    });
    </script>

这是模板自定义帖子类型循环

如果我删除此循环,则会显示页眉的默认页面特征图像。

模板.php

  <div id=”test”>
  <?php
  $args=array(‘post_type’=>’services’,’meta_key’=>’level’,’orderby’=>’meta_value_num’,’order’=>’ASC’);
  $query=new WP_Query($args);
  while($query->have_posts()): $query->the_post();
  ?>
   <a href=”<?php the_permalink();?>”>
    <div class=”col-md-4 col our-services-page”>
     <?php $font=get_post_meta(get_the_ID(),’_font_image’,false);?>
     <span class=”icn <?php echo $font;?>”></span>
      <div id=”overly”>
         <?php the_post_thumbnail(‘medium’); ?>
         <?php echo get_field(‘excerpt’, get_the_ID());?>
     </div>
    <h3><?php the_title();?></h3>
   </div>
  </a>
  <?php
  endwhile;
  ?>
  </div>

这是我尝试过但没有用的方法

  1. 我使用 jquery 来定位该页面 id 和 img 元素来替换图像,我将它添加到模板的末尾只是尝试先测试,不工作,但工作 chrome 控制台。

    李>
  2. 我更改了footer.php的if else语句,但是所有的header图片都不会显示。

    if(is_category()):
    $cat_id=get_cat_id(single_cat_title('',false));
    $img=get_option("taxonomy_".$cat_id);
    $url=$img['img'];
    else:
    global $post;
    $post_id = $post->ID; 
    if(has_post_thumbnail($post_id)):        
    $url=wp_get_attachment_url(get_post_thumbnail_id($post_id));
    
    /*code I add target page id*/
    elseif(is_page(886)):
    $url=get_template_directory_uri().'/img/banner.jpg';
    /* end of code I add*/
    
    else:
    $url=get_template_directory_uri().'/img/banner.jpg';
    endif;
    endif;
    ?>
    

感谢您的帮助。

【问题讨论】:

  • 你到底想要什么,拉那个缩略图?
  • template.php 循环中,在endwhile; 之后,添加wp_reset_postdata(); 看看是否有帮助。
  • @SallyCJ 非常感谢! 有效!谢谢!

标签: wordpress thumbnails custom-post-type


【解决方案1】:

感谢@Sally CJ 的帮助,我添加了

<?php wp_reset_query(); ?>

在 endwhile 循环之后,我的问题就消失了。 可以查看documentation here.

【讨论】:

  • 没问题,很高兴它对您有所帮助! =)
猜你喜欢
  • 2019-03-30
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多