我有一个快捷码,可以显示特定类别的帖子,或者基于帖子slug的单个帖子。不过,我很难弄清楚如何让它根据它们的slug显示多个帖子。我知道我需要使用explode,但我似乎做得不对。
以下是当前的工作代码:
add_shortcode( \'latest_post\', \'latest_post_query_shortcode\' );
function latest_post_query_shortcode( $atts ) {
ob_start();
$atts = shortcode_atts( array(
\'posts_per_page\' => \'\',
\'category\' => \'\',
\'offset\' => \'\',
\'post\' => \'\',
), $atts );
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\'=> $atts[\'posts_per_page\'],
\'offset\' => $atts[\'offset\'],
);
// Add category if not empty
if ( ! empty ( $atts[\'category\'] ) ) {
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $atts[\'category\'],
),
);
}
// Add post if not empty
if ( ! empty ( $atts[\'post\'] ) ) {
$args[\'name\'] = $atts[\'post\'];
}
$string = \'\';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) { ?>
<section class="recent-posts clear">
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( \'left\' ); ?>>
<? echo \'<a href="\' . get_permalink( $_post->ID ) . \'" title="\' . esc_attr( $_post->post_title ) . \'">\';
echo get_the_post_thumbnail( $_post->ID, \'large\' );
echo \'</a>\';
echo \'<h2><a href="\' . get_permalink( $_post->ID ) . \'" title="\' . esc_attr( $_post->post_title ) . \'">\';
echo get_the_title( $_post->ID);
echo \'</a></h2>\';
?>
</article>
<?php endwhile;
wp_reset_postdata();?>
</section>
<?php
$clean = ob_get_clean();
return $clean;
}
}
我尝试添加:
\'name\' => explode( \', \', $post),
内部
$args = array(
但当我尝试指定两个slug时,没有返回任何结果,例如:[最新发布=“杏仁蛋糕,椰子馅饼”](如果我使用其中任何一个,它都可以工作,但不能同时使用这两个。)
此外,一旦我添加了爆炸,它会在任何地方向我发出警告,否则会使用短代码:
警告:trim()要求参数1为字符串,数组给定。。。
SO网友:Michelle
This works for me:
add_shortcode( \'latest_post\', \'latest_post_query_shortcode\' );
function latest_post_query_shortcode( $atts ) {
ob_start();
$atts = shortcode_atts( array(
\'posts_per_page\' => \'\',
\'category\' => array(), // defined as array here
\'offset\' => \'\',
\'post\' => \'\',
), $atts );
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\'=> $atts[\'posts_per_page\'],
\'offset\' => $atts[\'offset\'],
);
// Add category if not empty
if ( !empty( $atts[\'category\'] ) ) {
$terms = str_replace(\' \', \'\', $atts[\'category\']); // strip whitespace so admins don\'t have to remember whether or not to put a space after each comma
$terms = explode(\',\', $atts[\'category\']); // then get the array
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $terms,
),
);
}
// Add post if not empty
if ( ! empty ( $atts[\'post\'] ) ) {
$args[\'name\'] = $atts[\'post\'];
}
$string = \'\';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) { ?>
<section class="recent-posts clear">
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( \'left\' ); ?>>
<? echo \'<a href="\' . get_permalink( $query->ID ) . \'" title="\' . esc_attr( $query->post_title ) . \'">\';
echo get_the_post_thumbnail( $query->ID, \'large\' );
echo \'</a>\';
echo \'<h2><a href="\' . get_permalink( $query->ID ) . \'" title="\' . esc_attr( $query->post_title ) . \'">\';
echo get_the_title( $query->ID);
echo \'</a></h2>\';
?>
</article>
<?php endwhile;
wp_reset_postdata();?>
</section>
<?php
$clean = ob_get_clean();
return $clean;
}
}