Как в цикле вставить условие?

Ссылка скопирована
1 ответ

Как правильно вставить условие в цикл
есть вывод постов, хочу незарегистрированным подставить вместо thumbnail другую картинку

по условию, только не могу понять как его правильно воткнуть в цикл
вот условие

<?php if ( !is_user_logged_in() : ?>   <div class="img"><img src="<?php echo get_template_directory_uri(); ?>/img/th.png"/></div>  <?php else : ?>     <div class="img"><?php the_post_thumbnail(); ?></div> <?php endif; ?>

<?php if ( !is_user_logged_in() : ?> <div class="img"><img src="<?php echo get_template_directory_uri(); ?>/img/th.png"/></div> <?php else : ?> <div class="img"><?php the_post_thumbnail(); ?></div> <?php endif; ?>

вот цикл

<div class="postlist"> 	<div class="container"> 		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 		<div class="post"> 		<div class="img"><?php the_post_thumbnail(); ?></div> 		<div class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> 		<div class="excerpt"><?php the_excerpt(); ?></div> 		<div class="read"> 			<a href="<?php the_permalink(); ?>"> 			<?php             if (ICL_LANGUAGE_CODE == 'en') {                echo "readmore...";             } elseif (ICL_LANGUAGE_CODE == 'uk') {                echo "детальіше..."; 						}          ?> 			</a> 		</div> 	</div>  	<?php endwhile; else: ?> 	<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p> 	<?php endif; ?> 	</div> </div>

<div class="postlist"> <div class="container"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="post"> <div class="img"><?php the_post_thumbnail(); ?></div> <div class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <div class="excerpt"><?php the_excerpt(); ?></div> <div class="read"> <a href="<?php the_permalink(); ?>"> <?php if (ICL_LANGUAGE_CODE == 'en') { echo "readmore..."; } elseif (ICL_LANGUAGE_CODE == 'uk') { echo "детальіше..."; } ?> </a> </div> </div> <?php endwhile; else: ?> <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?> </div> </div>

как не пытаюсь подставить, лезут ошибки

Спасибо

Дополнительно:

У вас скобки не хватает, включите отображение ошибок в браузере

<?php if ( ! is_user_logged_in() ) : ?>

Нужно решить такую задачу?

Опишите проблему, и специалист поможет с настройкой, исправлением ошибки или доработкой сайта. Подберём понятный план работ без лишней переписки.

Заказать помощь
Лучший ответ
1
Андрей PHP Ответ

Ошибка у вас в самом условии: пропущена закрывающая скобка после is_user_logged_in(). Должно быть if ( ! is_user_logged_in() ) :, а не if ( !is_user_logged_in() :.

Вставлять условие нужно прямо в том месте цикла, где сейчас выводится миниатюра:

&lt;div class="post"&gt;
    &lt;div class="img"&gt;
        &lt;?php if ( ! is_user_logged_in() ) : ?&gt;
            &lt;img src="&lt;?php echo esc_url(get_template_directory_uri() . '/img/th.png'); ?&gt;" alt=""&gt;
        &lt;?php else : ?&gt;
            &lt;?php the_post_thumbnail(); ?&gt;
        &lt;?php endif; ?&gt;
    &lt;/div&gt;
 
    &lt;div class="title"&gt;
        &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
    &lt;/div&gt;
 
    &lt;div class="excerpt"&gt;&lt;?php the_excerpt(); ?&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div class="post"&gt; &lt;div class="img"&gt; &lt;?php if ( ! is_user_logged_in() ) : ?&gt; &lt;img src="&lt;?php echo esc_url(get_template_directory_uri() . '/img/th.png'); ?&gt;" alt=""&gt; &lt;?php else : ?&gt; &lt;?php the_post_thumbnail(); ?&gt; &lt;?php endif; ?&gt; &lt;/div&gt; &lt;div class="title"&gt; &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt; &lt;/div&gt; &lt;div class="excerpt"&gt;&lt;?php the_excerpt(); ?&gt;&lt;/div&gt; &lt;/div&gt;

Если это дочерняя тема, для картинки лучше использовать get_stylesheet_directory_uri(), чтобы путь брался из child theme:

echo esc_url(get_stylesheet_directory_uri() . '/img/th.png');

echo esc_url(get_stylesheet_directory_uri() . '/img/th.png');

Логика простая: цикл while ( have_posts() ) : the_post(); оставляете как есть, а внутри карточки заменяете только блок thumbnail. Не нужно открывать новый цикл или закрывать endif после endwhile.

Другие ответы (0)

Пока нет других ответов. Будьте первым, кто поможет автору.

Ответить на вопрос

комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Вам также может быть интересно