php 具有精选帖子和新布局的博客
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 具有精选帖子和新布局的博客相关的知识,希望对你有一定的参考价值。
<?php 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' );
/**
* Saves the custom meta input
*/
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' );
?>
<!------------------------------------------------------->
<!--Homepage Blog-->
<!------------------------------------------------------->
<div class="container">
<div class="row gutter-100">
<h1 class="h1-lg"><?php the_field('blog_title' , 267); ?></h1>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="wide">
<?php
$args = array(
'posts_per_page' => 1,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
);
$featured = new WP_Query($args);
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
<!-- Blog Featured Post -->
<div class="col-sm-6">
<a href="<?php the_permalink( ); ?>" title="<?php the_title(); ?>">
<div class="featured center-content gutter-bottom-mb" style="background-image: url(<?php the_field('news_image'); ?>)">
<div class="overlay"></div>
<div class="featured-title">
<h1><?php the_title(); ?></h1>
</div>
</div>
</a>
</div>
<?php
endwhile; else:
endif;
?>
<div class="col-sm-6 regular-posts">
<div class="row gutter-bottom-200">
<?php
$image = get_field('news_image');
$args = array(
'numberposts' => 3,
'orderby' => 'post_date',
'include' => '',
'exclude' => '',
);
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
echo '<div class="col-sm-6"><a href="' . get_permalink($recent["ID"]) . '"><div class="non-featured center-content gutter-bottom-30" style="background-image: url('.$image.');"><div class="overlay"></div><div class="featured-title"><h1>' . $recent["post_title"].'</h1></div></div>
</a> </div> ';
}
wp_reset_query();
?>
<div class="col-sm-6">
<a href="<?php the_field('blog_read_more_link' , 267); ?>">
<button class="non-featured btn-square">
<h1><?php the_field ('blog_read_more_text', 267); ?></h1>
</button>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
以上是关于php 具有精选帖子和新布局的博客的主要内容,如果未能解决你的问题,请参考以下文章
php [Feat Image WP]在帖子#wordpress #php中启用精选图片