0
我的循環顯示精選帖子,然後按倒序時間順序發佈帖子。如何過濾通過元值DESC和發佈日期DESC獲得之前的帖子功能?
但是,當我使用<?php echo get_previous_post(); ?>
時,它會以反向時間順序抓取帖子。
如何添加一個過濾器到類似於this類似的以前的帖子功能,而是過濾一個像這樣的數組:'orderby' => array('meta_value' => 'DESC', 'date' => 'DESC')
?
代碼:
UPDATE:
:盪滌的index.php從@sMyles幫助和@JackJohansson的index.php後循環迴路
$args = array( 'posts_per_page' => - 1, 'meta_key' => 'meta-checkbox', 'orderby' => array('meta_value' => 'DESC', 'date' => 'DESC') ); $posts = new WP_Query($args); if($posts->have_posts()){ while($posts->have_posts()){ $posts->the_post(); // Set to content template by default $template = 'content'; // Change template to featured when `is_featured` meta has a value if(get_post_meta(get_the_ID(), 'meta-checkbox', 'yes')){ $template = 'featured'; } // Load template part get_template_part($template, get_post_format()); } }
UPDATE:至於建議,我說,我用以前的帖子功能
一篇文章功能
<div id="next-post">
<?php $prev_post = get_previous_post();
if(!empty($prev_post)) {
echo '<a class="next-story" href="' . get_permalink($prev_post->ID) . '">Next Story</a>';
echo '<a class="next-story-title" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>';
} ?>
</div>
功能對於精選文章:
function sm_custom_meta() {
add_meta_box('sm_meta', __('Featured Posts', 'sm-textdomain'), 'sm_meta_callback', 'post');
}
function sm_meta_callback($post) {
$featured = get_post_meta($post->ID);
?>
<p>
<div class="sm-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if (isset ($featured['meta-checkbox'])) checked($featured['meta-checkbox'][0], 'yes'); ?> />
<?php _e('Featured this post', 'sm-textdomain')?>
</label>
</div>
</p>
<?php
}
add_action('add_meta_boxes', 'sm_custom_meta');
function sm_meta_save($post_id) {
// Checks save status
$is_autosave = wp_is_post_autosave($post_id);
$is_revision = wp_is_post_revision($post_id);
$is_valid_nonce = (isset($_POST[ 'sm_nonce' ]) && wp_verify_nonce($_POST[ 'sm_nonce' ], basename(__FILE__))) ? 'true' : 'false';
// Exits script depending on save status
if ($is_autosave || $is_revision || !$is_valid_nonce) {
return;
}
// Checks for input and saves
if(isset($_POST[ 'meta-checkbox' ])) {
update_post_meta($post_id, 'meta-checkbox', 'yes');
} else {
update_post_meta($post_id, 'meta-checkbox', '');
}
}
add_action('save_post', 'sm_meta_save');