wordpress - 如何在将来发布时显示自定义帖子类型
问题描述
我很难完成两件事。我为事件创建了一个自定义帖子类型。我希望客户能够在日期之前发布活动。这意味着发布计划在未来发布的帖子。一旦日期过去,我还希望帖子恢复为草稿。有谁知道如何做到这一点?这是我一直在玩弄的代码:
<?php
// 注册自定义帖子类型:事件帖子
add_action('init', 'events_register');
函数 events_register() {
$labels = array(
'name' => _x('Events', 'post type general name'),
'singular_name' => _x('Event', 'post type singular name'),
'add_new' => _x('Add New', 'Event item'),
'add_new_item' => __('Add New Event'),
'edit_item' => __('Edit Event'),
'new_item' => __('New Event'),
'view_item' => __('View Event Item'),
'search_items' => __('Search Events'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => 'dashicons-calendar-alt',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 4,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'events' , $args );
function setup_future_hook() {
// Replace native future_post function with replacement
remove_action('future_events','_future_post_hook');
add_action('future_events','publish_future_post_now');
}
function publish_future_post_now($id) {
// Set new post's post_status to "publish" rather than "future."
wp_publish_post($id);
}
add_action('init', 'setup_future_hook');
}
这是我的循环:
<?php // WP_Query arguments
$args = array (
'post_type' => array( 'events' ),
'order' => 'ASC',
'posts_per_page' => '4',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<div class="col-md-6">
<div class="event-box">
<h4><?php the_title(); ?></h4>
<h5><?php the_field('venue_name'); ?></h5>
<div class="date"><?php the_date( 'F j, Y' ); ?></div>
<div class="time"><?php the_field('event_time'); ?></div>
<div class="row">
<div class="col-6">
<a target="_blank" class="btn btn-primary" href="<?php the_field('buy_tickets_link'); ?>">Buy Tickets</a>
</div>
<div class="col-6">
<a target="_blank" class="btn btn-primary" href="<?php the_field('venue_link'); ?>">Venue Info</a>
</div>
</div>
</div>
</div>
<?php }
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata(); ?>
谢谢!!
解决方案
$args = array (
'post_type' => array( 'events' ),
'order' => 'ASC',
'posts_per_page' => '4',
'post_status' => array('publish', 'future'),
);
尝试加入 'post_status' => array('publish', 'future')
你的论点。让我知道这是否有效。
推荐阅读
- javascript - 使用 if 语句声明 const 的值
- flutter - 在类及其状态的有状态小部件中定义构造函数
- r - 如何在正确位置使用 SelectInput 时保存在 DT 中所做的编辑
- azure - 安全中心/Sentinel Playbook 的自动化 RBAC 要求
- reactjs - 如何将弹出的 React 应用程序迁移到 TypeScript?
- css - 如何在 Fusion Slider 中更改标题背景颜色?
- php - 调用 PHP 变量以在 Python 脚本中使用它
- sql - 返回带有 Null 的表时出现问题
- actions-on-google - 从网站深层链接到 Google 上的操作
- python - 如何在 x 轴上仅显示可用值