gpt4 book ai didi

php - Wordpress 精选文章代码错误

转载 作者:行者123 更新时间:2023-11-28 09:48:50 25 4
gpt4 key购买 nike

我正在尝试在我的网站上创建一个精选帖子部分,其中涉及三个文件:

header.php
footer.php
functions.php
index.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');


?>

这是我在索引文件中的代码:

<?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(); ?>

知道是什么导致了这个问题,或者我是否遗漏了什么?

最佳答案

检查以上代码后,

index.php 运行良好,但导致问题的原因是 functions.php on save_post 操作页面未进入函数 save_featured_meta($post_id){}

检查这个添加 admin_init 而不是 save_post 并在上面的函数中回显它正在显示但在 save_post 上它没有进入函数。

尝试使用

而不是 save_post

edit_post 或

pre_post_update 或

post_updated

关于php - Wordpress 精选文章代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25160702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com