Answer the question
In order to leave comments, you need to log in
How to assign a value to a variable in a loop?
Hello!
There is a need to separate the tape by dates! I have a loop for outputting tape entries in my template:
<?php
global $DateControl; /*ПРОБУЮ ГЛОБАЛЬНУЮ ПЕРЕМЕННУЮ*/
class td_module_8 extends td_module {
function render() {
ob_start();
$title_length = $this->get_shortcode_att('m8_tl');
date_default_timezone_set('Europe/Kiev');
$DateNow = date("d.m.Y");
//$DateControl = "1"; ЕСЛИ ТУТ ДЕЛАТЬ ПРИСВОЕНИЕ - ПРИСВАИВАЕТ
$DateNews = $this->get_date();
?>
<div class="<?php echo $this->get_module_classes();?>">
<div class="item-details">
<?php //dsy даты
if($this->get_date() != $DateControl ){ //ЭТО РАБОЧАЯ ЛОГИКА, ЕСЛИ ДАТА ТЕКУЩЕГО ПОСТА ОТЛИЧАЕТСЯ ОТ КОНТРОЛЬНОЙ ДАТЫ (DateControl - ПРИСВАИВАЕМ В КОНЦЕ ЦИКЛА, И ВОТ ПРИСВОЕНИЕ НИЖЕ, ОНО НЕ РАБОТАЕТ), ТО СТАВИТ ДАТУ НОВОГО ДНЯ
echo $this->get_date();
}
?>
<?php //DSY ВАЖНОСТЬ
$i = get_field( "important-news", $this->post->ID );
//$icon = '<img src= ' + get_field( "img-icon", $this->post->ID ) + '>';
$icon = get_field( "img-icon", $this->post->ID ) ;
switch ($i) {
case null:
echo $this->get_title($title_length, $icon);
break;
case "important":
echo $this->get_titleImportant($title_length, $icon);
break;
case "importanthard":
echo $this->get_title_very_Important($title_length, $icon);
break;
}
?>
<?php /* echo $this->get_image('td_324x235'); */ ?>
<div class="td-module-meta-info">
<?php if (td_util::get_option('tds_category_module_8') == 'yes') { echo $this->get_category(); }?>
<?php echo $this->get_author();?>
<?php echo $this->get_date();?>
<?php /*echo get_the_time('', $this->post->ID ); */?>
<?php echo $this->get_comments();?>
</div>
</div>
<?php echo $this->get_quotes_on_blocks();?>
<?php
$DateControl = $this->get_date(); // ВОТ ТУТ ЗАГВОЗДКА, ПРИСВОЕНИЕ НИГДЕ НЕ РАБОТАЕТ, ТОЛЬКО В ВЕРХНЕЙ ЧАСТИ КОДА, А МНЕ ПРИСВАИВАТЬ ДАТУ НАДО В КОНЦЕ КАЖДОЙ ИТЕРАЦИИ ЦИКЛА
?>
</div>
<?php return ob_get_clean();
}
}
$DateNews = $this->get_date();
static $DateControl ;
if( $DateControl != $DateNews ){
echo 'DateControl != DateNews </br></br>';
echo $DateControl;
}
echo 'Обе переменные вне</br>';
echo $DateControl;
echo $this->get_date();
//echo $DateNews;
<time class=»entry-date updated td-module-date» datetime=»2018-07-23T23:22:53+03:00″>23.07.2018</time>
Answer the question
In order to leave comments, you need to log in
The global keyword
must be inside the function - it's not for declaring variables as global, but for importing them into the current scope.
But since you are using a class, it makes much more sense to use a static property of that class instead of a global variable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question