$wp_query 代码显示不正确
$wp_query code not displaying correctly
我最近从头开始制作了一个wordpress电影网站,我现在发现很难解决无法正常工作的代码。
我遇到的问题是我有一个自定义 post 类型 "movies" 和一些附加到上述 post 类型的自定义元数据,我想做的是列出我的自定义 post按元数据 "released" 的顺序键入 "movies"。这是我的 functions.php 文件的一部分。
add_filter( 'rwmb_meta_boxes', 'your_prefix_meta_boxes' );
function your_prefix_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
'title' => __( 'Movie Meta Box', 'textdomain' ),
'post_types' => 'movies',
'fields' => array(
array(
'id' => 'name',
'name' => __( 'Title', 'textdomain' ),
'type' => 'text',
),
array(
'id' => 'released',
'name' => __( 'Release Date', 'textdomain' ),
'type' => 'date',
),
array(
'id' => 'rating',
'name' => __( 'Rating', 'textdomain' ),
'type' => 'radio',
'options' => array(
'0' => __( 'Unrated', 'textdomain' ),
'1' => __( '1', 'textdomain' ),
'2' => __( '2', 'textdomain' ),
'3' => __( '3', 'textdomain' ),
'4' => __( '4', 'textdomain' ),
'5' => __( '5', 'textdomain' ),
'6' => __( '6', 'textdomain' ),
'7' => __( '7', 'textdomain' ),
'8' => __( '8', 'textdomain' ),
'9' => __( '9', 'textdomain' ),
'10' => __( '10', 'textdomain' ),
),
),
array(
'id' => 'duration',
'name' => __( 'Duration', 'textdomain' ),
'type' => 'text',
),
array(
'id' => 'genre',
'name' => __( 'Genre', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'director',
'name' => __( 'Director/s', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'writer',
'name' => __( 'Writer/s', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'stars',
'name' => __( 'Star/s', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'storyline',
'name' => __( 'Storyline', 'textdomain' ),
'type' => 'textarea',
),
),
);
return $meta_boxes;
}
这是档案-movies.php
<?php get_header(); ?>
<?php bd_pagination(); ?>
<div class="row" role="main"><!-- ROW -->
<?php
$args = array(
"posts_per_page" => 10,
"post_type" => "movies",
"post_status" => "publish",
"meta_key" => "released",
"orderby" => "meta_value_num",
"order" => "DESC"
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="col-lg-9">
<div class="panel panel-default panel-body" style="padding-bottom:0;"><!-- PANEL -->
<p class="fa fa-calendar"></p> <?php the_time( 'd-m-Y' ); ?> | <p class="fa fa-clock-o"></p> <?php the_time( 'H:i a' ); ?> | <p class="fa fa-user"></p> <a href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>"><?php the_author(); ?></a></i> | <p class="fa fa-envelope-o"></p> posted in movies
<article class="post <?php if ( has_post_thumbnail() ) { ?>has-thumbnail <?php } ?>">
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'image-poster' ); ?></a>
</div>
<article class="post-thumbnail-text">
<h4 class="align_Center" style="color:#000; padding-bottom:1em;">
<?php the_title(); ?>
</h4>
<?php
$nam = get_post_meta( get_the_ID(), "name", true );
$rel = get_post_meta( get_the_ID(), "released", true );
$rat = get_post_meta( get_the_ID(), "rating", true );
$dur = get_post_meta( get_the_ID(), "duration", true );
$gen = get_post_meta( get_the_ID(), "genre", true );
$dir = get_post_meta( get_the_ID(), "director", true );
$wri = get_post_meta( get_the_ID(), "writer", true );
$sta = get_post_meta( get_the_ID(), "stars", true );
$sto = get_post_meta( get_the_ID(), "storyline", true );
?>
<div class="align_Left">
<p><b>Title:</b> <?php echo $nam; ?></p>
<p><b>Released:</b> <?php echo $rel; ?></p>
<p><b>Rating:</b> <?php echo $rat; ?>/10</p>
<p><b>Duration:</b> <?php echo $dur; ?> min</p>
<p><b>Genre:</b> <?php echo $gen; ?></p>
<p><b>Director/s:</b> <?php echo $dir; ?></p>
<p><b>Writer/s:</b> <?php echo $wri; ?></p>
<p><b>Stars:</b> <?php echo $sta; ?></p>
</div>
</article>
</article>
<br>
<div class="panel panel-default panel-body" style="padding:20px 20px 10px 20px">
<p><b>Storyline:</b> <?php echo $sto; ?></p>
</div>
</div><!-- /PANEL -->
</div>
<?php endwhile; ?>
<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div><!-- /ROW -->
<?php bd_pagination(); ?>
<br>
</div><!-- ./CONTAINER -->
<?php get_template_part( 'Secondfooter' ); ?>
<?php get_footer(); ?>
希望有人能帮助我..已经@它几个星期了。
提前致谢
使用meta_type
定义正确的类型:
'meta_key' => 'released',
'meta_type' => 'DATE',
'orderby' => 'meta_value_date',
'order' => 'DESC'
(此示例假设使用 DATE 格式)
meta_value
- Note that a meta_key=keyname
must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use meta_value_num
instead for numeric values. You may also specify meta_type
if you want to cast the meta value as a specific type. Possible values are NUMERIC
, BINARY
, CHAR
, DATE
, DATETIME
, DECIMAL
, SIGNED
, TIME
, UNSIGNED
, same as in $meta_query
. When using meta_type
you can also use meta_value_* accordingly. For example, when using DATETIME as meta_type
you can use meta_value_datetime
to define order structure.
在此处阅读更多相关信息:Orderby_Parameters
我最近从头开始制作了一个wordpress电影网站,我现在发现很难解决无法正常工作的代码。 我遇到的问题是我有一个自定义 post 类型 "movies" 和一些附加到上述 post 类型的自定义元数据,我想做的是列出我的自定义 post按元数据 "released" 的顺序键入 "movies"。这是我的 functions.php 文件的一部分。
add_filter( 'rwmb_meta_boxes', 'your_prefix_meta_boxes' );
function your_prefix_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
'title' => __( 'Movie Meta Box', 'textdomain' ),
'post_types' => 'movies',
'fields' => array(
array(
'id' => 'name',
'name' => __( 'Title', 'textdomain' ),
'type' => 'text',
),
array(
'id' => 'released',
'name' => __( 'Release Date', 'textdomain' ),
'type' => 'date',
),
array(
'id' => 'rating',
'name' => __( 'Rating', 'textdomain' ),
'type' => 'radio',
'options' => array(
'0' => __( 'Unrated', 'textdomain' ),
'1' => __( '1', 'textdomain' ),
'2' => __( '2', 'textdomain' ),
'3' => __( '3', 'textdomain' ),
'4' => __( '4', 'textdomain' ),
'5' => __( '5', 'textdomain' ),
'6' => __( '6', 'textdomain' ),
'7' => __( '7', 'textdomain' ),
'8' => __( '8', 'textdomain' ),
'9' => __( '9', 'textdomain' ),
'10' => __( '10', 'textdomain' ),
),
),
array(
'id' => 'duration',
'name' => __( 'Duration', 'textdomain' ),
'type' => 'text',
),
array(
'id' => 'genre',
'name' => __( 'Genre', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'director',
'name' => __( 'Director/s', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'writer',
'name' => __( 'Writer/s', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'stars',
'name' => __( 'Star/s', 'textdomain' ),
'type' => 'textarea',
),
array(
'id' => 'storyline',
'name' => __( 'Storyline', 'textdomain' ),
'type' => 'textarea',
),
),
);
return $meta_boxes;
}
这是档案-movies.php
<?php get_header(); ?>
<?php bd_pagination(); ?>
<div class="row" role="main"><!-- ROW -->
<?php
$args = array(
"posts_per_page" => 10,
"post_type" => "movies",
"post_status" => "publish",
"meta_key" => "released",
"orderby" => "meta_value_num",
"order" => "DESC"
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="col-lg-9">
<div class="panel panel-default panel-body" style="padding-bottom:0;"><!-- PANEL -->
<p class="fa fa-calendar"></p> <?php the_time( 'd-m-Y' ); ?> | <p class="fa fa-clock-o"></p> <?php the_time( 'H:i a' ); ?> | <p class="fa fa-user"></p> <a href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>"><?php the_author(); ?></a></i> | <p class="fa fa-envelope-o"></p> posted in movies
<article class="post <?php if ( has_post_thumbnail() ) { ?>has-thumbnail <?php } ?>">
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'image-poster' ); ?></a>
</div>
<article class="post-thumbnail-text">
<h4 class="align_Center" style="color:#000; padding-bottom:1em;">
<?php the_title(); ?>
</h4>
<?php
$nam = get_post_meta( get_the_ID(), "name", true );
$rel = get_post_meta( get_the_ID(), "released", true );
$rat = get_post_meta( get_the_ID(), "rating", true );
$dur = get_post_meta( get_the_ID(), "duration", true );
$gen = get_post_meta( get_the_ID(), "genre", true );
$dir = get_post_meta( get_the_ID(), "director", true );
$wri = get_post_meta( get_the_ID(), "writer", true );
$sta = get_post_meta( get_the_ID(), "stars", true );
$sto = get_post_meta( get_the_ID(), "storyline", true );
?>
<div class="align_Left">
<p><b>Title:</b> <?php echo $nam; ?></p>
<p><b>Released:</b> <?php echo $rel; ?></p>
<p><b>Rating:</b> <?php echo $rat; ?>/10</p>
<p><b>Duration:</b> <?php echo $dur; ?> min</p>
<p><b>Genre:</b> <?php echo $gen; ?></p>
<p><b>Director/s:</b> <?php echo $dir; ?></p>
<p><b>Writer/s:</b> <?php echo $wri; ?></p>
<p><b>Stars:</b> <?php echo $sta; ?></p>
</div>
</article>
</article>
<br>
<div class="panel panel-default panel-body" style="padding:20px 20px 10px 20px">
<p><b>Storyline:</b> <?php echo $sto; ?></p>
</div>
</div><!-- /PANEL -->
</div>
<?php endwhile; ?>
<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div><!-- /ROW -->
<?php bd_pagination(); ?>
<br>
</div><!-- ./CONTAINER -->
<?php get_template_part( 'Secondfooter' ); ?>
<?php get_footer(); ?>
希望有人能帮助我..已经@它几个星期了。
提前致谢
使用meta_type
定义正确的类型:
'meta_key' => 'released',
'meta_type' => 'DATE',
'orderby' => 'meta_value_date',
'order' => 'DESC'
(此示例假设使用 DATE 格式)
meta_value
- Note that ameta_key=keyname
must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Usemeta_value_num
instead for numeric values. You may also specifymeta_type
if you want to cast the meta value as a specific type. Possible values areNUMERIC
,BINARY
,CHAR
,DATE
,DATETIME
,DECIMAL
,SIGNED
,TIME
,UNSIGNED
, same as in$meta_query
. When usingmeta_type
you can also use meta_value_* accordingly. For example, when using DATETIME asmeta_type
you can usemeta_value_datetime
to define order structure.
在此处阅读更多相关信息:Orderby_Parameters