用php buffer输出WP shortcode
Published
2023-04-25
浏览次数 : 102
function ml_list_movies($atts,$content = null)
{
ob_start();
$atts = shortcode_atts( array(
'title' => 'Latest movies',
'count' => 12,
'genre' => 'all',
'pagination' => 'off',
),$atts );
// condition === () ? :
$pagination = $atts['pagination'] == 'on' ? false : true;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
//check genre
if ($atts['genre'] == 'all') {
$terms = '';
} else {
$terms = array(array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => $atts['genre'],
));
}
//query
$args = array(
'post_type' => 'movie_listing',
'orderby' => 'menu_order',
'order' => 'ASC',
'no_found_rows' => $pagination,
'posts_per_page' => $atts['count'],
'paged' => $paged,
'tax_query' => $terms
);
$movies = new WP_Query($args);
if ($movies->have_posts()) {
//get genre slug
$genre = str_replace('-', '', $atts['genre']);
?>
<div class="movie-list">
<?php while ($movies->have_posts()) {
$movies->the_post(); $image = wp_get_attachment_image_url( get_post_thumbnail_id( ),'single-post-thumbnail' ); ?>
<div class="movie-col">
<img src="<?php echo $image;?>" alt="" class="feat-img">
<h5 class="movie-title"><?=the_title( );?></h5>
<a href="<?php the_permalink( ); ?>">View Details</a>
</div>
<?php
}
echo '<div style="clear: both;"></div>';
wp_reset_postdata(); ?>
</div>
<?php
} else {
return '<p>No movies found!</p>';
}
return ob_get_clean();
}
add_shortcode( 'movies', 'ml_list_movies' );
- 标签1
- 标签1
- 标签1