我一直在研究一个按类别查询页面和显示自定义字段的快捷码。我什么都做得很好,除了;如果我的“custom\\u field”变量是“numeric”,它将返回正确的“posts\\u per\\u page”数,如果我的“custom\\u field”变量是“text”,它将不返回任何内容,除非以10的倍数输入“posts\\u per\\u page”。例如,如果我想显示“5”篇文章,我必须输入“posts\\u per\\u page=“50”。
我完全困惑了。
另一方面,我一直在读到,我不应该使用“extract(shortcode\\u atts)”,但如果不这样做,我就无法传递变量。这应该如何编写?
一如既往,我非常感谢你的指导。
//开始短代码
add_shortcode("custom_query_test_100", "custom_query_test_100");
function custom_query_test_100($atts, $content = null) {
// Attributes
extract( shortcode_atts(
array(
\'pagination\' => \'true\',
\'query\' => \'\',
\'category\' => \'\',
\'custom_field\' => \'\',
\'number_posts\' => \'\',
\'order_by\' => \'\',
\'order\' => \'\',
), $atts ));
// WP_Query arguments
$args = array(
\'paged\' => ( get_query_var(\'paged\') ? get_query_var(\'paged\') : 1 ),
\'post_type\' => array( \'page\', \' post\' ),
\'post_status\' => array( \'publish\' ),
\'cat\' => $category,
\'meta_key\' => $custom_field,
\'posts_per_page\' => \'50\',
\'order\' => $order,
\'orderby\' => $order_by,
);
// The Query
global $wp_query,$paged,$post;
$query = new WP_Query( $args );
ob_start();
if ($query->have_posts()):
while ($query->have_posts()) : $query->the_post();
if ( strlen(get_post_meta($post->ID, $custom_field, true)) > 0 ) :
?>
<div style="margin:10px 0 30px;"></div>
<a href="<?php the_permalink() ?>">
<h2 style="margin:0 0 5px 0; font-size:24px;"><?php echo the_title(); ?></h2>
<div class="one-third-width-responsive">
<?php echo the_post_thumbnail( \'large\' ); ?>
</div>
</a>
<div class="two-third-width-responsive">
<div style="font-size:15px;"><?php echo get_post_meta($post->ID, $custom_field, true); ?></div>
</div>
<?php endif; ?>
<div class="clearfix"></div>
<?php
endwhile;
endif;
?>
<div style="margin-top:35px;"></div>
<?php if ($query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $query;
?>
<div style="margin:20px 0; background-color:#e3f0f7; width:100%; padding:30px;">
<div style="float:left; line-height:0;"><?php echo get_previous_posts_link(\'« Previous\'); ?></div>
<div style="float:right; line-height:0;"><?php echo get_next_posts_link(\'Next »\', $query->max_num_pages ); ?></div>
</div>
<?php
$wp_query = $orig_query; // fix for pagination to work
endif;
wp_reset_postdata();
$output = ob_get_clean();
//print $output; // debug
return $output;
}
//结束