如何捕获页面 ID 并显示所选页面?
How to catch pages ids and showing the selected ones?
我有 7 个关于服务的页面。
在 Redux 框架的文档中解释 class select
and multi select
示例多 Select 代码:
$fields = array(
'id' => 'opt-multi-select',
'type' => 'select',
'multi' => true,
'title' => __('Multi Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
//Must provide key => value pairs for radio options
'options' => array(
'1' => 'Opt 1',
'2' => 'Opt 2',
'3' => 'Opt 3'),
'default' => array('2','3')
);
我如何在 options
中动态插入数据库中注册的所有页面 ID 或帖子 ID?
已更新
Redux::setSection($opt_name, array(
'id' => 'options_services',
'title' => 'Services',
'subsection' => true,
'fields' => array(
array(
'id' => 'opt-multi-select_pages',
'type' => 'select',
'multi' => true,
'title' => 'Multi Select Option',
'data' => 'pages', // select pages
'args' => array( 'posts_per_page' => -1 ),
'default' => array('1','2','3','4','5')
)
)
));
index.php
<?php
foreach ($global_master['options_services'] as $singleValue){
?>
<div class="col-md-6 col-sm-6">
<div class="service-item">
<h4> <strong><?php echo $singleValue['post_title'] ?> </strong></h4>
<img src="<?php echo $singleValue['post_thumbnail_url'] ?>" alt="..." class="img-thumbnail img-responsive">
<a href="<?php echo $singleValue['guid'] ?>" class="btn btn-light">See More</a>
</div>
</div>
<?php
}
?>
以上代码没有return索引中的任何内容
在我尝试使用文档 wordpress 中指示的 returns 'post_title' and 'guid' 的代码中,没有 'post_thumbnail_url.'
的引用
更新 2
在 Nathan Dawson 的帮助下,我解决了最初的疑问,并使用了以下代码进行打印:
<?php
foreach ($global_master['opt-multi-select_pages'] as $singleValue){
$post_thumbnail_id = get_post_thumbnail_id($singleValue);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="col-md-6 col-sm-6">
<div class="service-item service">
<h4> <strong><?php echo get_the_title($singleValue); ?> </strong></h4>
<img src="<?php echo $post_thumbnail_url; ?>" alt="..." class="img-thumbnail img-responsive">
<a href="<?php the_permalink($singleValue); ?>" class="btn btn-light">See more</a>
</div>
</div>
<?php
}
?>
试试这个。
//$page_id_array store your all page ids
$page_id_array = get_all_page_ids();
//$options will store multi select options.
$options = array();
foreach( $page_id_array as $page_id ){
$options[$page_id] = get_the_title($page_id);
}
$fields = array(
'id' => 'opt-multi-select',
'type' => 'select',
'multi' => true,
'title' => __('Multi Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
//Must provide key => value pairs for radio options
'options' => $options,
'default' => array('2','3'),
);
Redux 框架中的字段具有开箱即用的动态列出页面的选项,您只需设置参数即可。删除 'options' 并为 'data' 设置一个值。
示例:
$fields = array(
'id' => 'opt-multi-select',
'type' => 'select',
'multi' => true,
'title' => __('Multi Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
'data' => 'pages', // select pages
'args' => array( 'posts_per_page' => -1 ),
'default' => array('2','3')
);
您可能会注意到我还添加了 'args' 选项。默认情况下,页面 'data' 选项将检索 20 个页面,自定义参数会检索所有页面。
文档:https://docs.reduxframework.com/core/the-basics/using-data-argument/
我有 7 个关于服务的页面。
在 Redux 框架的文档中解释 class select
and multi select
示例多 Select 代码:
$fields = array(
'id' => 'opt-multi-select',
'type' => 'select',
'multi' => true,
'title' => __('Multi Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
//Must provide key => value pairs for radio options
'options' => array(
'1' => 'Opt 1',
'2' => 'Opt 2',
'3' => 'Opt 3'),
'default' => array('2','3')
);
我如何在 options
中动态插入数据库中注册的所有页面 ID 或帖子 ID?
已更新
Redux::setSection($opt_name, array(
'id' => 'options_services',
'title' => 'Services',
'subsection' => true,
'fields' => array(
array(
'id' => 'opt-multi-select_pages',
'type' => 'select',
'multi' => true,
'title' => 'Multi Select Option',
'data' => 'pages', // select pages
'args' => array( 'posts_per_page' => -1 ),
'default' => array('1','2','3','4','5')
)
)
));
index.php
<?php
foreach ($global_master['options_services'] as $singleValue){
?>
<div class="col-md-6 col-sm-6">
<div class="service-item">
<h4> <strong><?php echo $singleValue['post_title'] ?> </strong></h4>
<img src="<?php echo $singleValue['post_thumbnail_url'] ?>" alt="..." class="img-thumbnail img-responsive">
<a href="<?php echo $singleValue['guid'] ?>" class="btn btn-light">See More</a>
</div>
</div>
<?php
}
?>
以上代码没有return索引中的任何内容
在我尝试使用文档 wordpress 中指示的 returns 'post_title' and 'guid' 的代码中,没有 'post_thumbnail_url.'
的引用更新 2
在 Nathan Dawson 的帮助下,我解决了最初的疑问,并使用了以下代码进行打印:
<?php
foreach ($global_master['opt-multi-select_pages'] as $singleValue){
$post_thumbnail_id = get_post_thumbnail_id($singleValue);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="col-md-6 col-sm-6">
<div class="service-item service">
<h4> <strong><?php echo get_the_title($singleValue); ?> </strong></h4>
<img src="<?php echo $post_thumbnail_url; ?>" alt="..." class="img-thumbnail img-responsive">
<a href="<?php the_permalink($singleValue); ?>" class="btn btn-light">See more</a>
</div>
</div>
<?php
}
?>
试试这个。
//$page_id_array store your all page ids
$page_id_array = get_all_page_ids();
//$options will store multi select options.
$options = array();
foreach( $page_id_array as $page_id ){
$options[$page_id] = get_the_title($page_id);
}
$fields = array(
'id' => 'opt-multi-select',
'type' => 'select',
'multi' => true,
'title' => __('Multi Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
//Must provide key => value pairs for radio options
'options' => $options,
'default' => array('2','3'),
);
Redux 框架中的字段具有开箱即用的动态列出页面的选项,您只需设置参数即可。删除 'options' 并为 'data' 设置一个值。
示例:
$fields = array(
'id' => 'opt-multi-select',
'type' => 'select',
'multi' => true,
'title' => __('Multi Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
'data' => 'pages', // select pages
'args' => array( 'posts_per_page' => -1 ),
'default' => array('2','3')
);
您可能会注意到我还添加了 'args' 选项。默认情况下,页面 'data' 选项将检索 20 个页面,自定义参数会检索所有页面。
文档:https://docs.reduxframework.com/core/the-basics/using-data-argument/