所以,我在 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 -----
所以,直到这里一切都好!上面的代码将使用户创建一个包含日期和月份字段的帖子。所以我想知道如何在我的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
有人可以帮我解决这个循环吗?
更新:
错误已修复,但循环无效。它只显示我特定的后期类型的最新帖子。我想展示最新的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>
非常感谢这里的人们试图与我解决这个问题。
答案 0 :(得分:2)
你没有关闭两个打开的'{'
<?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 -->
答案 1 :(得分:1)
您缺少两个结束}
。一个来自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 -->