Wordpress에 처음 입성했고 단일 게시물 페이지를 만들려고합니다. 추천 이미지를 가져 왔지만 추천 이미지가없는 경우 대체 이미지로 교체하려고합니다.Wordpress는 fallback을 배경 이미지로 사용하여 이미지를 표현했습니다.
현재이 (루프 내부 obvs)처럼 보이는 : 그것은 그때 에코 $ 엄지 비트에 배치하는 URL을 가져옵니다 위대한입니다
<?php $thumb = get_the_post_thumbnail_url(); ?>
<div class="news-hero" style="background-image: url('<?php echo $thumb;?>')">
<div class="page-title"><h1><?php the_title(); ?></h1></div>
</div>
.
$ thumb을 섬네일 URL 또는 다른 URL로 정의해야한다고 생각하지만 어떻게해야할까요?
나는 그것을 목록 페이지의 이미지 태그로 관리했지만 가능한 경우이를 배경 이미지로 (URL을 가져 오는 것) 할 수있는 것을 선호합니다.
이것은 현재 목록에서 사용하고 있지만 img 태그에서만 작동합니다. 예쁜 그림을 디자인
<div class="news-collation-img">
<?php if (has_post_thumbnail()) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/img/news/fallback-image.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
어떤 도움의 사람이 줄 수있는 것이
감사를 P는 일을 만드는 것보다 훨씬 쉽다. 위에서 언급 한 페이지에 일반 텍스트로 템플릿 디렉토리를 생성, 어떤 이유로
<?php
//define fallback image path
$thumb = bloginfo('template_directory').'/img/news/fallback-image.jpg';
if (has_post_thumbnail()) {
//override fallback image if post has any thumbnail
$thumb = get_the_post_thumbnail_url();
}
?>
<div class="news-hero" style="background-image: url('<?php echo $thumb;?>')">
<div class="page-title"><h1><?php the_title(); ?></h1></div>
</div>
정렬 의미에서 지금 테마의 디렉토리까지, URL의 첫 번째 부분을 침 있어요하지만 그냥이를 태우고 페이지 상단의 복사본으로 $ thumb 에코 대신에 bloginfo ('template_directory')를 포함 시키면 작동하도록 할 수는 있지만 실제로 실제 이미지를 가져 오지는 않습니다. – NickL