繁体   English   中英

php如何通过一些创建的字段(wordpress)以帖子和顺序进行php循环?

[英]How to do php loop with posts and order by some created fields (wordpress)?

因此,我在function.php文件中为wordpress仪表板创建了一个自定义小部件,其内容如下:

 //----- Agenda -----

    function ccr_agenda_sec() {

    $labels = array(
        'name'                => __( 'Agenda', 'codexcoder' ),
        'singular_name'       => __( 'Agenda', 'codexcoder' ),
        'add_new'             => _x( 'Add new appointment', 'codexcoder', 'codexcoder' ),
        'add_new_item'        => __( 'Add new appointment', 'codexcoder' ),
        'edit_item'           => __( 'Edit appointment on agenda', 'codexcoder' ),
        'new_item'            => __( 'New appointment on agenda', 'codexcoder' ),
        'view_item'           => __( 'See Agenda', 'codexcoder' ),
        'search_items'        => __( 'Search on Agenda', 'codexcoder' ),
        'not_found'           => __( 'Not found on Agenda', 'codexcoder' ),
        'not_found_in_trash'  => __( 'Not found in trash', 'codexcoder' ),
        'parent_item_colon'   => __( 'Parent-item Agenda:', 'codexcoder' ),
        'menu_name'           => __( 'Agenda', 'codexcoder' ),
        );

    $args = array(
        'labels'              => $labels,
        'hierarchical'        => false,
        'description'         => 'description',
        'taxonomies'          => array(),
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => null,
        'menu_icon'           => null,
        'show_in_nav_menus'   => true,
        'publicly_queryable'  => true,
        'exclude_from_search' => false,
        'has_archive'         => true,
        'query_var'           => true,
        'can_export'          => true,
        'rewrite'             => true,
        'capability_type'     => 'post',
        'supports'            => array(
            'title', 'editor', 'thumbnail'
            )
        );

    register_post_type( 'agenda-sec', $args );
    flush_rewrite_rules( false );
    }

    add_action( 'init', 'ccr_agenda_sec' );

    // agenda-sec meta box register
    add_action('add_meta_boxes', 'ccr_agenda_sec_metabox' );

    function ccr_agenda_secretario_metabox(){        
        add_meta_box('ccr_agenda_sec_metabox_id', __('Date of appointment'), 'show_ccr_agenda_sec_metabox', 'agenda-sec', 'side', 'high');
    }

    function show_ccr_agenda_sec_metabox($post){
    $day= get_post_meta(get_the_id(), 'agenda_day', true ); 
    $month= get_post_meta(get_the_id(), 'agenda_month', true ); 

    ?>

    <p>
        <input type="text" class="widefat" name="agenda_day" value="<?php if($day){echo $day;} ?>" placeholder="x.: 06">
    </p>
    <p>
        <select name="month" id="month" onchange="" size="1" name="agenda_month" value="<?php if($month){echo $month;} ?>">
            <option value="01">JAN</option>
            <option value="02">FEV</option>
            <option value="03">MAR</option>
            <option value="04">ABR</option>
            <option value="05">MAI</option>
            <option value="06">JUN</option>
            <option value="07">JUL</option>
            <option value="08">AGO</option>
            <option value="09">SET</option>
            <option value="10">OUT</option>
            <option value="11">NOV</option>
            <option value="12">DEZ</option>
        </select>
    </p>

    <?php 
}

// save agenda_secretario value
add_action('save_post', 'ccr_agenda_sec_save' );

function ccr_agenda_sec_save($post_id){
    if(isset($_POST['agenda_day'])) {
        update_post_meta($post_id,'agenda_day', $_POST['agenda_day']);
    }
    //save mes value
    if(isset($_POST['agenda_month'])) {
        update_post_meta($post_id,'agenda_month', $_POST['agenda_month']);
    }
}

//----END of Agenda -----

所以,直到这里一切都OK! 上面的代码将使用户创建一个日期为一天零一个月的帖子。 因此,我想知道如何在我的sidebar.php页面(我的网站的侧边栏)中将这些数据放入类似的内容中:

该图显示了我想如何显示此帖子,获取$ day和$ month而不是the_time()函数(单击此处查看)

我的sidebar.php代码是这样的:

<?php
/* 
 *
 * @package WordPress
 * @subpackage Skin
 * @since Skin 1.0
 */
?>
<div class="sidebar-wrapper col-md-3">
    <?php if ( is_active_sidebar( 'sidebar-1' )  ) : ?>
        <aside id="secondary" class="sidebar widget-area" role="complementary">     

                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Documents</h4>
                    <ul class="nav nav-pills nav-stacked left-menu" id="stacked-menu">
                        <li>
                          <a href="#" data-target="#item1"><span class="fa fa-balance-scale"></span>Item 1</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item2"><span class="fa fa-cubes"></span>Item 2</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item3"><span class="fa fa-line-chart"></span>Item 3</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item4"><span class="fa fa-gavel"></span>Item 4</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item5"><span class="fa fa-users"></span>Item 5</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item2"><span class="fa fa-graduation-cap"></span>Item 6</a>
                        </li>
                    </ul>
                </div>

                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Agenda</h4>

                    <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                        <?php query_posts(array('paged' => get_query_var('paged'),'post_type'=>'agenda-sec', 'orderby'=>'title', 'order' => 'ASC', 'showposts'=>4)); 
                            if(have_posts()) { while (have_posts()) { the_post();   
                        ?>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php echo get_post_meta(get_the_id(), 'agenda_day', true ); ?></h4>
                                <h5 id="month"><?php echo get_post_meta(get_the_id(), 'agenda_month', true ); ?></h5>
                            </span>
                            <span><?php the_title(); ?></span>
                        </li>


                    </ul>
                    <p style="float: right;"><a href="#">See more</a></p><br>


                </div><br><br>


                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Events</h4>
                    <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>
                    </ul>
                    <p style="float: right;" ><a href="#">See more</a></p><br>   
                </div><br><br>

        </aside><!-- .sidebar .widget-area -->
    <?php endif; ?>
</div><!-- .sidebar-wrapper -->

但是我的网页上出现此错误,但是我的侧边栏上没有显示任何内容,但这是:

Parse error: syntax error, unexpected 'endif' (T_ENDIF) in C:\xampp\htdocs\intranet\wp-content\themes\skin-master-intranet\sidebar.php on line 220

有人可以帮我解决这个问题吗?


更新:

错误已修复,但循环无法正常工作。 它仅显示especific帖子类型中我的最新帖子。 我想显示最新的4个,但不按标题显示。 我想根据日期($ day和$ month变量)显示最新的4个。 因为它是下一个事件的列表。

抱歉,如果我没有使自己足够清楚,请先感谢您的耐心帮助。

这是最近的代码:

<div class="sidebar-wrapper col-md-3">
    <?php if ( is_active_sidebar( 'sidebar-1' )  ) : ?>
        <aside id="secondary" class="sidebar widget-area" role="complementary">     

                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Agenda</h4>

                    <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                        <?php query_posts(array('paged' => get_query_var('paged'),'post_type'=>'agenda-sec', 'orderby'=>'title', 'order' => 'ASC', 'showposts'=>4)); 
                            if(have_posts()) { while (have_posts()) { the_post();   
                        ?>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php echo get_post_meta(get_the_id(), 'agenda_day', true ); ?></h4>
                                <h5 id="month"><?php echo get_post_meta(get_the_id(), 'agenda_month', true ); ?></h5>
                            </span>
                            <span><?php the_title(); ?></span>
                        </li>


                    </ul>
                    <p style="float: right;"><a href="#">See more</a></p><br>


                </div><br><br>


                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Events</h4>
                    <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                        <!-- Here I'll have another events list, but with another post-type -->
                    </ul>
                    <p style="float: right;" ><a href="#">See more</a></p><br>   
                </div><br><br>

        </aside><!-- .sidebar .widget-area -->
    <?php endif; ?>
</div><!-- .sidebar-wrapper -->

更新-问题已解决


因此,我终于找到了循环工作的方法。 结果是:

<div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Agenda</h4>

                    <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">

                        <?php 
                            $args = array(
                                'post_type'     => 'agenda-sec',
                                'post_status'   => 'publish',
                                "posts_per_page"   => 4,
                            );

                            $getPosts = new WP_Query($args);

                            while($getPosts->have_posts()) : $getPosts->the_post(); ?>
                                <li>
                                    <span class="event">
                                        <h4 id="day"><?php echo get_post_meta(get_the_id(), 'agenda_day', true ); ?></h4>
                                        <h5 id="month"><?php echo get_post_meta(get_the_id(), 'agenda_month', true ); ?></h5>
                                    </span>
                                    <span><?php the_title(); ?></span>
                                </li>
                           <?php endwhile;
                        ?>                  
                    </ul>
                    <p style="float: right;"><a href="#">See more</a></p><br>

非常感谢这里的人试图与我解决这个问题。

您没有关闭两个打开的“ {”

    <?php
    /* 
     *
     * @package WordPress
     * @subpackage Skin
     * @since Skin 1.0
     */
    ?>
    <div class="sidebar-wrapper col-md-3">
        <?php if ( is_active_sidebar( 'sidebar-1' )  ) : ?>
            <aside id="secondary" class="sidebar widget-area" role="complementary">     

                    <div class="col-md-12 col-sm-6">
                        <h4 class="sidebar-menu-title">Documents</h4>
                        <ul class="nav nav-pills nav-stacked left-menu" id="stacked-menu">
                            <li>
                              <a href="#" data-target="#item1"><span class="fa fa-balance-scale"></span>Item 1</a>
                            </li>
                            <li>
                              <a href="#" data-target="#item2"><span class="fa fa-cubes"></span>Item 2</a>
                            </li>
                            <li>
                              <a href="#" data-target="#item3"><span class="fa fa-line-chart"></span>Item 3</a>
                            </li>
                            <li>
                              <a href="#" data-target="#item4"><span class="fa fa-gavel"></span>Item 4</a>
                            </li>
                            <li>
                              <a href="#" data-target="#item5"><span class="fa fa-users"></span>Item 5</a>
                            </li>
                            <li>
                              <a href="#" data-target="#item2"><span class="fa fa-graduation-cap"></span>Item 6</a>
                            </li>
                        </ul>
                    </div>

                    <div class="col-md-12 col-sm-6">
                        <h4 class="sidebar-menu-title">Agenda</h4>

                        <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                            <?php query_posts(array('paged' => get_query_var('paged'),'post_type'=>'agenda-sec', 'orderby'=>'title', 'order' => 'ASC', 'showposts'=>4)); 
                                if(have_posts()) { while (have_posts()) { the_post();   
                            ?>

                            <li>
                                <span class="event">
                                    <h4 id="day"><?php echo get_post_meta(get_the_id(), 'agenda_day', true ); ?></h4>
                                    <h5 id="month"><?php echo get_post_meta(get_the_id(), 'agenda_month', true ); ?></h5>
                                </span>
                                <span><?php the_title(); ?></span>
                            </li>


                        </ul>
                        <p style="float: right;"><a href="#">See more</a></p><br>


                    </div><br><br>


                    <div class="col-md-12 col-sm-6">
                        <h4 class="sidebar-menu-title">Events</h4>
                        <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                            <li>
                                <span class="event">
                                    <h4 id="day"><?php the_time(d) ?></h4>
                                    <h5 id="month"><?php the_time(M) ?></h5>
                                </span>
                            </li>

                            <li>
                                <span class="event">
                                    <h4 id="day"><?php the_time(d) ?></h4>
                                    <h5 id="month"><?php the_time(M) ?></h5>
                                </span>
                            </li>

                            <li>
                                <span class="event">
                                    <h4 id="day"><?php the_time(d) ?></h4>
                                    <h5 id="month"><?php the_time(M) ?></h5>
                                </span>
                            </li>

                            <li>
                                <span class="event">
                                    <h4 id="day"><?php the_time(d) ?></h4>
                                    <h5 id="month"><?php the_time(M) ?></h5>
                                </span>
                            </li>
                        </ul>
                        <p style="float: right;" ><a href="#">See more</a></p><br>   
                    </div><br><br>

            </aside><!-- .sidebar .widget-area -->
        <?php 
                }
            }
        endif; ?>
    </div><!-- .sidebar-wrapper -->

您缺少两个结尾} 一个来自if语句,另一个来自while循环:

<?php
/* 
 *
 * @package WordPress
 * @subpackage Skin
 * @since Skin 1.0
 */
?>
<div class="sidebar-wrapper col-md-3">
    <?php if ( is_active_sidebar( 'sidebar-1' )  ) : ?>
        <aside id="secondary" class="sidebar widget-area" role="complementary">     

                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Documents</h4>
                    <ul class="nav nav-pills nav-stacked left-menu" id="stacked-menu">
                        <li>
                          <a href="#" data-target="#item1"><span class="fa fa-balance-scale"></span>Item 1</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item2"><span class="fa fa-cubes"></span>Item 2</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item3"><span class="fa fa-line-chart"></span>Item 3</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item4"><span class="fa fa-gavel"></span>Item 4</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item5"><span class="fa fa-users"></span>Item 5</a>
                        </li>
                        <li>
                          <a href="#" data-target="#item2"><span class="fa fa-graduation-cap"></span>Item 6</a>
                        </li>
                    </ul>
                </div>

                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Agenda</h4>

                    <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                        <?php query_posts(array('paged' => get_query_var('paged'),'post_type'=>'agenda-sec', 'orderby'=>'title', 'order' => 'ASC', 'showposts'=>4)); 
                            if(have_posts()) { while (have_posts()) {

                                 the_post();   
                        ?>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php echo get_post_meta(get_the_id(), 'agenda_day', true ); ?></h4>
                                <h5 id="month"><?php echo get_post_meta(get_the_id(), 'agenda_month', true ); ?></h5>
                            </span>
                            <span><?php the_title(); ?></span>
                        </li>


                    </ul>
                    <p style="float: right;"><a href="#">See more</a></p><br>


                </div><br><br>


                <div class="col-md-12 col-sm-6">
                    <h4 class="sidebar-menu-title">Events</h4>
                    <ul style="border-bottom: 2px solid #EAEBEE;" id="event-list">
                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>

                        <li>
                            <span class="event">
                                <h4 id="day"><?php the_time(d) ?></h4>
                                <h5 id="month"><?php the_time(M) ?></h5>
                            </span>
                        </li>
                    </ul>
                    <p style="float: right;" ><a href="#">See more</a></p><br>   
                </div><br><br>

        </aside><!-- .sidebar .widget-area -->
    <?php 
    }
    }
    endif; 

    ?>
</div><!-- .sidebar-wrapper -->

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM