首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Wordpress上有特色的帖子部分不会出现

在Wordpress上有特色的帖子部分不会出现
EN

Stack Overflow用户
提问于 2014-08-07 01:14:23
回答 1查看 110关注 0票数 0

试图在我的网站上创建一个有特色的帖子部分,其中涉及三个文件:

  • 头文件
  • 页脚文件
  • 函数文件
  • 索引文件

我想让选择一个特色的文章只是通过选中一个复选框在编辑屏幕(帖子页),并能够检索这些特色文章的头版。

到目前为止,在我的当前代码中,它只会在posts页面编辑屏幕上显示一个复选框,但是它不会保存,而且在首页上,除了posts循环之外,没有任何东西出现。

这是我的functions.php文件:

代码语言:javascript
复制
/**
 * ----------------------------------------------------------------------------------------
 * 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');


?>

下面是我在index.php文件上的代码:

代码语言:javascript
复制
<?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(); ?>

你知道是什么导致了这个问题吗?或者我遗漏了什么?我只想在我的博客上有一个有特色的帖子部分,至少类似于这个图片:

http://goo.gl/E5tbMv

有什么帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-07 01:38:29

代码语言:javascript
复制
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');

尝试使用该代码,我更正了name属性和update_post_meta函数。对不起,我目前没有Wordpress安装来尝试它。

您似乎也两次使用id featured-post。您应该更改înput字段的id。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25172744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档