Php 小部件插件中的自定义字段未显示
我创建了一个自定义帖子类型,然后执行了以下操作: 在主页中,我有一个普通的循环,我显示该帖子类型的内容,如自定义字段、缩略图等 然后,通过functions.php我创建了一个小部件,它允许我根据类别显示2种自定义帖子类型。 一切正常,但当我查看页面时,自定义字段不显示 我在主页的第一个循环中使用了以下代码:Php 小部件插件中的自定义字段未显示,php,wordpress,custom-fields,Php,Wordpress,Custom Fields,我创建了一个自定义帖子类型,然后执行了以下操作: 在主页中,我有一个普通的循环,我显示该帖子类型的内容,如自定义字段、缩略图等 然后,通过functions.php我创建了一个小部件,它允许我根据类别显示2种自定义帖子类型。 一切正常,但当我查看页面时,自定义字段不显示 我在主页的第一个循环中使用了以下代码: <?php echo get_post_meta($post->ID, 'game_offer', true); ?> 由于某种原因,我在小部件函数中的循环中不起作
<?php echo get_post_meta($post->ID, 'game_offer', true); ?>
由于某种原因,我在小部件函数中的循环中不起作用
在这里,您可以看到自定义字段存在于后端的图像
如果需要,我在函数中的代码如下所示:
class My_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'my_widget',
'description' => 'Custom Post Type widget',
);
parent::__construct( 'my_widget', 'Cutom Post Type Widget', $widget_ops );
}
public function widget( $args, $instance ) {
echo '<section id="custom_post_type_widget">';
?>
<div class="top_bar">
<h3 class="border-radius">Top Casino Offers</h3>
</div>
<?php
$mypost = array(
'post_type' => 'game_reviews',
'posts_per_page' => 2,
'type' => 'top-casino-offers',
);
$loop = new WP_Query( $mypost );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="custom_post_widget_container flex outer">
<div class="left">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<div class="game_imag">';
echo the_post_thumbnail();
echo '</div>';
}
?>
</div>
<div class="right">
<div>
<div class="flex">
<div>
<p>
<span class="title"><?php the_title(); ?></span> | <span class="rating">
<?php
$nb_stars = intval( get_post_meta( get_the_ID(), 'game_rating', true ) );
for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
if ( $star_counter <= $nb_stars ) {
echo '<img src="'.get_template_directory_uri().'/images/icon.png"/>';
} else {
echo '<img src="'.get_template_directory_uri().'/images/grey.png"/>';
}
}
?>
</span>
</p>
<ul class="custom-field-list">
<li><?php echo get_post_meta($post->ID, 'game_offer', true); ?></li>
</ul>
<?php echo get_post_meta($post->ID, 'game_offer', true); ?>
</div>
<div class="right">
<ul class="play-reviews flex">
<li><a href="#" class="play_now">Play Now</a></li>
<li><a href="#" class="reviews">Reviews</a></li>
</ul>
</div>
</div>
<p><?php echo get_the_excerpt(); ?><a class="read-more" href="<?php the_permalink(); ?>">Read More</a></p>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query();
echo '</section>';
}
}
add_action( 'widgets_init', function(){
register_widget( 'My_Widget' );
});
class My_Widget扩展了WP_Widget{
公共函数构造(){
$widget_ops=数组(
'classname'=>'my_widget',
'description'=>'自定义帖子类型小部件',
);
父项::u构造('my_widget','Cutom Post Type widget',$widget_ops);
}
公共函数小部件($args,$instance){
回声';
?>
顶级赌场优惠
|
使用查询帖子而不是WP\u查询
<?php
class My_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'my_widget',
'description' => 'Custom Post Type widget',
);
parent::__construct( 'my_widget', 'Cutom Post Type Widget', $widget_ops );
}
public function widget( $args, $instance ) {
echo '<section id="custom_post_type_widget">';
?>
<div class="top_bar">
<h3 class="border-radius">Top Casino Offers</h3>
</div>
<?php
global $post;
$game_reviews_args=array(
'post_type' => 'game_reviews',
'post_status' => 'publish',
'showposts' => 2,
'tax_query' => array(
array(
'taxonomy' => 'type', //taxonomy name
'terms' => 'top-casino-offers', //category name slug
'field' => 'slug'
)
),
'orderby' => 'date',
'order' => 'DESC'
);
query_posts($game_reviews_args);
if (have_posts()) :
while (have_posts()) : the_post();
?>
<div class="custom_post_widget_container flex outer">
<div class="left">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<div class="game_imag">';
echo the_post_thumbnail();
echo '</div>';
}
?>
</div>
<div class="right">
<div>
<div class="flex">
<div>
<p>
<span class="title"><?php the_title(); ?></span> | <span class="rating">
<?php
$nb_stars = intval( get_post_meta( $post->ID, 'game_rating', true ) );
for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
if ( $star_counter <= $nb_stars ) {
echo '<img src="'.get_template_directory_uri().'/images/icon.png"/>';
} else {
echo '<img src="'.get_template_directory_uri().'/images/grey.png"/>';
}
}
?>
</span>
</p>
<ul class="custom-field-list">
<li><?php echo get_post_meta($post->ID, 'game_offer', true); ?></li>
</ul>
<?php echo get_post_meta($post->ID, 'game_offer', true); ?>
</div>
<div class="right">
<ul class="play-reviews flex">
<li><a href="#" class="play_now">Play Now</a></li>
<li><a href="#" class="reviews">Reviews</a></li>
</ul>
</div>
</div>
<p><?php echo get_the_excerpt(); ?><a class="read-more" href="<?php the_permalink(); ?>">Read More</a></p>
</div>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_query();
echo '</section>';
}
}
add_action( 'widgets_init', function(){
register_widget( 'My_Widget' );
});
?>
顶级赌场优惠
|
因此,毕竟我认为我所需要的只是WP\u查询实例之前的全局$post变量:
global $post;
顶级赌场提供什么?它是一个类别名称。你的分类名称是什么?分类法被称为类型。所以我在注册中使用了这个名称:register_taxonomy('type',array('game_reviews'),$args);game_reviews是我的自定义帖子类型的名称。我觉得这很奇怪,因为我在主页模板文件中使用了相同的代码,并且在那里工作。只有在小部件中,我看到了除此特定自定义字段之外的所有内容。也不起作用。现在我实际上快速创建了另一个自定义帖子类型,只是为了测试目的和该字段仍然无法显示它是否起作用。希望您不介意解释原因。我有点困惑使用query_posts而不是WP_query查看query_posts。但我认为WP_query比query_posts加query_posts更好,可能会导致问题。请阅读这篇文章: