Answer the question
In order to leave comments, you need to log in
Carbon Fields. What to fix in the code for the posts widget from the rubric with the output of data from multilayer associations?
Hello. There are posts in the 'chat' format - these are addresses. (the site doesn't need to use record formats for its intended purpose, so I've attached to them so as not to produce entities) Addresses have a few simple CF fields. There are entries in the "aside" format - these are events. In events, there is a complex field, in which there is an association field. The address is selected through the latter. And from this I need to make a poster - a widget in which active events from the category specified through the assotiation field will be displayed.
// Создание кастомных полей
add_action( 'carbon_fields_register_fields', 'venue_attach_post_meta' );
function venue_attach_post_meta() {
Container::make( 'post_meta', 'venue_post_options', 'Опции локации' )
->where('post_format', '=', 'chat')
->add_fields( array(
Field::make( 'text', 'venue_adress', 'Адрес' ),
) );}
// Вывод
if ( has_post_format('chat') ) {
echo carbon_get_the_post_meta('venue_adress');
}
// Создание кастомных полей
add_action( 'carbon_fields_register_fields', 'event_attach_post_meta' );
function event_attach_post_meta() {
Container::make( 'post_meta', 'event_post_options', 'Опции спектакля' )
->where('post_format', '=', 'aside')
->add_fields( array(
Field::make( 'complex', 'event_options', 'Установки времени, даты и адреса' )
->add_fields( array(
Field::make('text', 'event_date', 'Дата')
->set_width( 33 ),
Field::make('text', 'event_time', 'Время')
->set_width( 33 ),
Field::make('association', 'event_adress')
->set_types( array(
array(
'type' => 'post',
'post_format' => 'chat',
),
))
->set_max(1)
) )
) );}
// Вывод (огромная благодарность Антону Вакуленко за помощь с кодом)
<?php if ( has_post_format('aside') ){
$places = carbon_get_post_meta( $post->ID, 'event_options' );
if ( $places ) {
foreach ( $places as $place ) {
echo $place['event_date'];
echo $place['event_time'];
$address = $place['event_adress'];
foreach ( $address as $item ) {
echo get_permalink($item['id']);
echo get_the_title($place['id']);
echo carbon_get_post_meta($item['id'], 'venue_adress');
}
}
}
}
function load_widgets() {
class OutPostsWidget extends Widget {
protected $form_options = array( 'width' => 500 );
function __construct() {
$this->setup( 'outposts_widget', 'Вывод постов из рубрик', 'Blah-blah', array(
Field::make( 'text', 'outposts_title', 'Заголовок' )->set_default_value( '') ,
Field::make( 'association', 'outposts_select' )
->set_types( array(
array( 'type' => 'term', 'taxonomy' => 'category', ),
) )
) );
}
// Called when rendering the widget in the front-end
function front_end( $args, $instance ) {
echo $args['before_title'] . $instance['outposts_title'] . $args['after_title'];
$cats = $instance['outposts_select'];
if ( $cats ) {
foreach ( $cats as $cat ) {
// До этого момента код работает правильно
foreach ( $cat as $posts ) {
// Отсюда начинается моя ошибка, которую я не знаю как исправить
var_dump( $posts );
echo '<p> Test Text ' . get_the_title($posts['id']) . '</p>'; // Это выводится несколько раз, по разу для каждого слоя иерархии, естественно, с пустотой, вместо title, запись выводится только одна – самая последняя.
}
}
}
}
}
register_widget( 'OutPostsWidget' );
}
Answer the question
In order to leave comments, you need to log in
I gave up on this one. Too difficult for me. But I came up with another solution - displaying the code in a widget with php support. But he has problems too. The crux of the problem is here , if anyone is interested.
if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
rewrite ^(.*) /index.php last;
}
it?
Sketched out the basic config .
try_files is faster and preferred over rewrite.
What immediately came to mind. It is not entirely clear what you need, but there is something to build on.
or
location / {
root /var/www/;
index index.php;
if (-f $request_filename) {
break;
}
if (-e $request_filename)
{
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php last;
break;
}
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
if (-f $request_filename) {
break;
}
if (-e $request_filename)
{
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php last;
break;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question