我想在此代码中添加自定义字段值
<?php $catquery = new WP_Query( 'cat=20;&posts_per_page=5' ); ?>
我需要用自定义字段值
替换数字20此代码用于显示自定义字段
<?php $references = get_post_meta($post->ID, "references",true); ?>
<?php echo $references ?>
有没有办法添加
<?php echo $references ?>
代码如此cat=<?php echo $references ?>
或类似的东西?
答案 0 :(得分:0)
要做你想做的事,你可以这样做:
“最简单的表格”,但如果您的自定义字段中未设置/更正,则可能会中断:
// load the category id
$references = (int)get_post_meta( $post->ID, 'references', TRUE);
// use the category id in the query
$catquery = new WP_Query( 'cat=' . $references . ';&posts_per_page=5' );
好一点:
// use an array for the parameters. Set it up here
$parameters = array(
'posts_per_page' => 5,
);
// load the category id
$references = (int)get_post_meta( $post->ID, 'references', TRUE);
// only if there is a reference ID, add it to the parameters
if ( $references ) {
$parameters['cat'] = $references;
)
// pass the parameters into the WP_Query
$catquery = new WP_Query( $parameters );