[英]Featured Posts Section on Wordpress won't show up
Trying to create a Featured Posts Section on my site and there are three files involved: 尝试在我的网站上创建“特色帖子”部分,其中涉及三个文件:
I want to make selecting a featured post is just by checking a checkbox in edit screen (posts page) AND be able to retrieve these featured articles on the front page. 我想通过选择编辑屏幕(帖子页面)中的复选框来选择特色帖子,并能够在首页上检索这些特色文章。
So far with my current code it is showing up a checkbox only on the posts page edit screen however it won't save and on the front page nothing is appearing except the posts loop. 到目前为止,使用我当前的代码,它仅在帖子页面编辑屏幕上显示一个复选框,但是不会保存,并且在首页上除了帖子循环外什么都没有出现。
here's my functions.php file: 这是我的functions.php文件:
/**
* ----------------------------------------------------------------------------------------
* 1.0 - Define constants.
* ----------------------------------------------------------------------------------------
*/
define( 'THEMEROOT', get_stylesheet_directory_uri() );
define( 'IMAGES', THEMEROOT. '/images' );
define( 'SCRIPTS', THEMEROOT. '/js' );
/** */
/***********************************************************************************************/
/* 2.0 - Add Theme Support for Post Thumbnails */
/***********************************************************************************************/
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
set_post_thumbnail_size(300, 200);
}
/***********************************************************************************************/
/* 3.0 - Add Theme Support for Post Thumbnails */
/***********************************************************************************************/
function register_post_assets(){
add_meta_box('featured-post', __('Featured Post'), 'add_featured_meta_box', 'post', 'advanced', 'high');
}
add_action('admin_init', 'register_post_assets', 1);
function add_featured_meta_box($post){
$featured = get_post_meta($post->ID, '_featured-post', true);
echo "<label for='_featured-post'>".__('Feature this post?', 'foobar')."</label>";
echo "<input type='checkbox' name='_featured-post' id='featured-post' value='1' ".checked(1, $featured)." />";
}
function save_featured_meta($post_id){
// Do validation here for post_type, nonces, autosave, etc...
if (isset($_REQUEST['featured-post']))
update_post_meta(esc_attr($post_id, '_featured-post', esc_attr($_REQUEST['featured-post'])));
// I like using _ before my custom fields, so they are only editable within my form rather than the normal custom fields UI
}
add_action('save_post', 'save_featured_meta');
?>
Here's my code on the index.php file: 这是我在index.php文件上的代码:
<?php get_header(); ?>
<?php while(have_posts()) : the_post(); ?>
<h1><a href="<?php the_permalink(); ?>" class="gray">
<?php the_title(); ?>
</a></h1>
<p class="details">By <a href="<?php the_author_posts() ?>"><?php the_author(); ?> </a> / On <?php echo get_the_date('F j, Y'); ?> / In <?php the_category(', '); ?></p>
<?php if (has_post_thumbnail()) : ?>
<figure> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('', array('class' => 'opacity-hover box-layer img-responsive')); ?></a> </figure>
<p class="excerpt"> <?php the_excerpt(); ?> </p>
<div class="btn-margin"> <a href="<?php the_permalink(); ?>" class="btn btn-primary">CONTINUE READING >>> </a> </div>
</li>
<?php endif; ?>
<?php endwhile; ?>
<h1>Featured Posts</h1>
<?php
$args = array(
'posts_per_page' => 5,
'meta_key' => '_featured-post',
'meta_value' => 1
);
$featured = new WP_Query($args);
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post();
the_title();
the_content();
endwhile; else:
endif;
?>
<?php get_footer(); ?>
Any idea what's causing the problem or if I am missing something? 您知道导致问题的原因是什么,或者如果我缺少什么? I just want to have a featured posts section on my blog atleast similar to this image: 我只想在我的博客上至少有一个特色帖子部分类似于此图片:
http://goo.gl/E5tbMv http://goo.gl/E5tbMv
Any help? 有什么帮助吗?
function add_featured_meta_box($post){
$featured = get_post_meta($post->ID, '_featured-post', true);
echo "<label for='_featured-post'>".__('Feature this post?', 'foobar')."</label>";
echo "<input type='checkbox' name='featured-post' id='featured-post' value='1' ".checked(1, $featured)." />";
}
function save_featured_meta($post_id){
// Do validation here for post_type, nonces, autosave, etc...
if (isset($_REQUEST['featured-post']))
update_post_meta(esc_attr($post_id), '_featured-post', esc_attr($_REQUEST['featured-post']));
// I like using _ before my custom fields, so they are only editable within my form rather than the normal custom fields UI
}
add_action('save_post', 'save_featured_meta');
Try with that code, I corrected the name
attribute and the update_post_meta
function. 尝试使用该代码,我更正了name
属性和update_post_meta
函数。 Sorry but I currently don't have a Wordpress install to try it. 抱歉,但我目前没有安装Wordpress来尝试。
You seem to use the id featured-post
twice, too. 您似乎也两次使用了id featured-post
。 You should change the id of the înput
field. 您应该更改înput
字段的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.