如何 php 循环发布帖子并按某些创建的字段(wordpress)排序?

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 -----

所以,到这里为止一切正常!上面的代码将使用户创建一个 post ,其中包含一天和一个月的字段。所以我想知道如何将这些数据放入我的 sidebar.php 页面(我网站的侧边栏)中:

我的 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

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


更新:

错误已修复,但循环不工作。它只显示来自特定 post-type 的最新 post。我想显示 4 个最新但不按标题排序。我想根据日期显示 4 个最新的($day 和 $month 变量)。因为它是下一个事件的列表。

抱歉,如果我说得不够清楚,但在此先感谢您的耐心帮助。

这是最近的代码:

<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 -->