简体   繁体   中英

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

So, I have created a custom widget for my wordpress dashboard in my function.php file, with this:

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

So, until here everything is ok! This code above will make the user create a post with field of a day and a month. So I would like to know how can a put this data into something like this on my sidebar.php page (sidebar of my website):

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

My sidebar.php code is this:

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

But I'm getting this error on my webpage, doesn't show anything on my sidebar but this:

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

Can someone please help me with this loop?


UPDATE:

The error was fixed but the loop is not working. It only show my latest post from the especific post-type. I would like to show the 4 latest but not order by title. I would like to show the 4 latest based on the date ($day and $month variables). Because it is a list of the next events.

Sorry if I'm not making myself enough clear, but thanks in advance for the patience in help me.

Here is the recent code:

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

UPDATE - Problem solved


So, I finally found a way to the loop work. The result is:

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

Thank you SO MUCH the people here who try to solve this with me.

you didnt close the two opened '{'

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

You are missing two closing } . One from the if statement, the other from the while loop :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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