Shortcode per a mostrar entrades blog on vulguis (plantilles oceanwp)

S’ha de fer un code snipped i posar:

if ( ! function_exists( 'zlp_get_oceanwp_blog_wrap_classes' ) ) {
	function zlp_get_oceanwp_blog_wrap_classes( $classes = NULL ) {
		
		// RETURN CUSTOM CLASS IF SET
		if ( $classes ) {
			return $classes;
		}
		
		// ADMIN DEFAULTS
		$style   = oceanwp_blog_entry_style();
		$classes = array( 'entries', 'clr' );
			
		// ISOTOPE CLASSES
		if ( $style == 'grid-entry' ) {
			$classes[] = 'oceanwp-row';
			if ( 'masonry' == oceanwp_blog_grid_style() ) {
				$classes[] = 'blog-masonry-grid';
			} else {
				if ( 'infinite_scroll' == oceanwp_blog_pagination_style() ) {
					$classes[] = 'blog-masonry-grid';
				} else {
					$classes[] = 'blog-grid';
				}
			}
		}
		
		// EQUAL HEIGHTS
		if ( oceanwp_blog_entry_equal_heights() ) {
			$classes[] = 'blog-equal-heights';
		}
		
		// INFINITE SCROLL CLASS
		if ( 'infinite_scroll' == oceanwp_blog_pagination_style() ) {
			$classes[] = 'infinite-scroll-wrap';
		}
		
		// ADD FILTER FOR CHILD THEMING
		$classes = apply_filters( 'ocean_blog_wrap_classes', $classes );
		// TURN CLASSES INTO SPACE SEPARATED STRING
		if ( is_array( $classes ) ) {
			$classes = implode( ' ', $classes );
		}
		// RETURN CLASSES
		return esc_attr( $classes );
		
	}
} 

Ara creem el shortcode


add_shortcode('zlp_posts', 'zlp_posts_shortcode');
function zlp_posts_shortcode($atts, $content = null) {
	
	global $post;
	
	extract(shortcode_atts(array(
		'num'     => '3',
		'order'   => 'DESC',
		'orderby' => 'post_date',
	), $atts));
	
	$args = array(
		'posts_per_page' => $num,
		'order'          => $order,
		'orderby'        => $orderby,
	);
	
	$posts = get_posts($args);
	$output = '';
	
	if($posts){
		$output .= '<div id="blog-entries" class="'.zlp_get_oceanwp_blog_wrap_classes().'">';
	
		foreach($posts as $post) {
			setup_postdata($post);
			ob_start();
			
			get_template_part( 'partials/entry/layout' );
			
			$output .= ob_get_clean();
		}
	
		$output .= '</div>';
		
		wp_reset_postdata();
	}
	
	return $output;
	
} 



Ara només cal afegir aquest shortcode on vulguem : [zlp_posts]
i si vols controlar la quantitat a mostar: [zlp_posts num=4]

Article original: https://www.zealopers.com/wordpress-tutorials/oceanwp-create-custom-shortcode-to-show-blog-posts/

  • Autor de l'entrada:
  • Categoria de l'entrada:Wordpress