如何捕获页面ID并显示所选的?

时间:2017-01-25 19:31:27

标签: wordpress wordpress-theming redux-framework

我有7页关于服务。

在Redux Framework的文档中解释了类select and multi 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中动态插入在DB中注册的所有页面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
    }
?>

以上代码不会在索引中返回任何内容

在我尝试的代码中使用文档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
    }
?>

2 个答案:

答案 0 :(得分:1)

Redux Framework中的字段可以选择动态列出开箱即用的页面,您只需设置参数即可。删除'选项'并为'数据'设置一个值。

示例:

$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,自定义args检索所有页面。

文档:https://docs.reduxframework.com/core/the-basics/using-data-argument/

答案 1 :(得分:0)

试试这个。

//$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'),
);