如何创建短代码以显示每个类别中的所有帖子
How to create shortcode to display all posts in each category
我有这个问题:我用他自己的一组类别制作了一个自定义页面。在简码中,我想获取所有类别,在类别中,我想要与该类别相关的所有 post。
function innovatiewerkplaats_sort($atts, $content = null){
global $post;
$terms = get_terms('innovatiewerkplaats_categories'); // Get all terms of a taxonomy
$nieuws = '';
foreach($terms as $term):
$nieuws .= '<div class="one">
<h2>Thema <strong>'.$term->name.' id='.$term->term_id.'</strong></h2>
<div class="wrapper">';
$category_query_args = array(
'post_type' => 'innovatiewerkplaats',
// 'category' => $term->term_id,
'category_name' => $term->name,
// 'cat' => $term->term_id,
);
query_posts($category_query_args);
if( have_posts() ) : while (have_posts()) : the_post();
$post_home= get_the_post_thumbnail( $page->ID, 'post-home');
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
$excerpt = get_the_content();
$excerpta = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $excerpt);
// $ter = wp_trim_words( apply_filters( 'rpwe_excerpt', $excerpta ), $args['length'], '…' );
$nieuws .= '<a href="'.get_permalink().'" title="'.get_the_title().'" class="one_fourth workplace">'.$term->term_id.'<span class="img" style="background:url('.$url.') no-repeat center center; background-size:cover;"></span><span class="titel">'.get_the_title().'</span><p></p><span class="more">Lees meer</span></a>';
endwhile; endif;
$nieuws .='</div></div>';
endforeach;
return $nieuws;
}
注意:不要使用 query_post
. Use WP_Query
or get_posts
。
请看讨论When to use WP_query(), query_posts() and pre_get_posts.
这里是与您的问题相关的示例方法,可根据其类别生成所有 post,然后通过简码显示以供日后使用。
- 在一个函数中,你应该先查询所有的post(使用
wp_query
或get_posts
),
- 通过查询循环内的 post id 获取相关类别。如果术语分类法,请使用
get_the_terms
.
- 使用键(在本例中,我们使用术语 slug)构建查询数据数组以对 posts 进行分组。
- 然后将这些数据用于外循环,并用
简单的循环。
- 通过函数
add_shortcode
构建简码(此代码仅使用简单的简码)。
/** Shortcode [my_shortcode_posts] */
add_shortcode( 'my_shortcode_posts', 'so36133962_get_all_posts_by_category' );
/**
* All posts by category
* build query by get_posts
*
* @return string|null
*/
function so36133962_get_all_posts_by_category( $attr, $content = null )
{
/**
* Build custom query
*
*/
$args = array(
'post_type' => 'your-post-type', // set your custom post type
'post_status' => 'publish',
'posts_per_page' => -1,
/** add more arguments such as taxonomy query i.e:
'tax_query' => array( array(
'taxonomy' => 'genre', // set your taxonomy
'field' => 'slug',
'terms' => array( 'comedy','drama' ) // set your term taxonomy
) )
*/
);
$posts = new WP_Query( $args );
/**
* Prepare Posts
*
*/
$result = array();
// The Loop
if ( $posts->have_posts() )
{
while ( $posts->have_posts() )
{
$posts->the_post();
/**
* Get all item in a term taxonomy
*
*/
$categories = get_the_terms( get_the_ID(), 'your-taxonomy' /* set your term taxonomy */ );
if ( ! $categories )
continue;
foreach ( $categories as $key => $category )
{
$term_name = $category->name;
$term_slug = $category->slug;
$term_id = $category->term_id;
}
/**
* Set thumbnail background cover
* Use Featured Image
*/
$img_cover = '';
if ( has_post_thumbnail() )
{
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) );
if ( ! empty( $image_url[0] ) )
{
$img_cover = '<span class="img" style="background:url( ' . esc_url( $image_url[0] ) . ' ) no-repeat center center; background-size:cover;">';
}
}
/**
* Format html content
*
*/
$format = '<a href="%1$s" title="%2$s" class="post-%3$s">%4$s%2$s</span></br><span class="content-%3$s">%5$s</span></br><span class="more">%6$s</span></a>';
/**
* Formatted string post content
*
*/
$content = sprintf( $format,
get_permalink(),
get_the_title(),
get_the_ID(),
$img_cover,
get_the_excerpt(),
__( 'Read More', 'text-domain' )
);
/**
* Set an array of each post for output loop
*
*/
$result[ $term_slug ][] = array(
'post_id' => get_the_ID(),
'post_content' => $content,
'term_name' => $term_name,
'term_id' => $term_id
);
}
}
wp_reset_postdata(); // post reset
/**
* Check existing output
*
*/
if ( ! $result )
return;
/**
* Output loop
*
*/
$output = '';
foreach ( $result as $slug => $data )
{
$count = count( $data );
for ( $i = 0; $i < $count; $i++ )
{
/**
* Set data as object
*
*/
$post = ( object ) array_map( 'trim', $data[ $i ] );
if ( 0 == $i )
{
/**
* Set category id and name
*
*/
$output .= '<div id="term-category-' . absint( $post->term_id ) . '">';
$output .= '<h3>' . esc_html( $post->term_name ) . '</h3>';
}
/**
* Set post id and content
*
*/
$output .= '<div id="post-' . absint( $post->post_id ) . '"><p>' . $post->post_content . '</p></div>';
}
$output .= '</div>';
}
return $output; // complete
}
您可以根据需要更改元素结构。请注意,使用此代码,每个类别中没有重复的 post,并确保更改代码中注释的 post 类型和分类等值。希望对您有所帮助。
我有这个问题:我用他自己的一组类别制作了一个自定义页面。在简码中,我想获取所有类别,在类别中,我想要与该类别相关的所有 post。
function innovatiewerkplaats_sort($atts, $content = null){
global $post;
$terms = get_terms('innovatiewerkplaats_categories'); // Get all terms of a taxonomy
$nieuws = '';
foreach($terms as $term):
$nieuws .= '<div class="one">
<h2>Thema <strong>'.$term->name.' id='.$term->term_id.'</strong></h2>
<div class="wrapper">';
$category_query_args = array(
'post_type' => 'innovatiewerkplaats',
// 'category' => $term->term_id,
'category_name' => $term->name,
// 'cat' => $term->term_id,
);
query_posts($category_query_args);
if( have_posts() ) : while (have_posts()) : the_post();
$post_home= get_the_post_thumbnail( $page->ID, 'post-home');
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
$excerpt = get_the_content();
$excerpta = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $excerpt);
// $ter = wp_trim_words( apply_filters( 'rpwe_excerpt', $excerpta ), $args['length'], '…' );
$nieuws .= '<a href="'.get_permalink().'" title="'.get_the_title().'" class="one_fourth workplace">'.$term->term_id.'<span class="img" style="background:url('.$url.') no-repeat center center; background-size:cover;"></span><span class="titel">'.get_the_title().'</span><p></p><span class="more">Lees meer</span></a>';
endwhile; endif;
$nieuws .='</div></div>';
endforeach;
return $nieuws;
}
注意:不要使用 query_post
. Use WP_Query
or get_posts
。
请看讨论When to use WP_query(), query_posts() and pre_get_posts.
这里是与您的问题相关的示例方法,可根据其类别生成所有 post,然后通过简码显示以供日后使用。
- 在一个函数中,你应该先查询所有的post(使用
wp_query
或get_posts
), - 通过查询循环内的 post id 获取相关类别。如果术语分类法,请使用
get_the_terms
. - 使用键(在本例中,我们使用术语 slug)构建查询数据数组以对 posts 进行分组。
- 然后将这些数据用于外循环,并用 简单的循环。
- 通过函数
add_shortcode
构建简码(此代码仅使用简单的简码)。
/** Shortcode [my_shortcode_posts] */
add_shortcode( 'my_shortcode_posts', 'so36133962_get_all_posts_by_category' );
/**
* All posts by category
* build query by get_posts
*
* @return string|null
*/
function so36133962_get_all_posts_by_category( $attr, $content = null )
{
/**
* Build custom query
*
*/
$args = array(
'post_type' => 'your-post-type', // set your custom post type
'post_status' => 'publish',
'posts_per_page' => -1,
/** add more arguments such as taxonomy query i.e:
'tax_query' => array( array(
'taxonomy' => 'genre', // set your taxonomy
'field' => 'slug',
'terms' => array( 'comedy','drama' ) // set your term taxonomy
) )
*/
);
$posts = new WP_Query( $args );
/**
* Prepare Posts
*
*/
$result = array();
// The Loop
if ( $posts->have_posts() )
{
while ( $posts->have_posts() )
{
$posts->the_post();
/**
* Get all item in a term taxonomy
*
*/
$categories = get_the_terms( get_the_ID(), 'your-taxonomy' /* set your term taxonomy */ );
if ( ! $categories )
continue;
foreach ( $categories as $key => $category )
{
$term_name = $category->name;
$term_slug = $category->slug;
$term_id = $category->term_id;
}
/**
* Set thumbnail background cover
* Use Featured Image
*/
$img_cover = '';
if ( has_post_thumbnail() )
{
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) );
if ( ! empty( $image_url[0] ) )
{
$img_cover = '<span class="img" style="background:url( ' . esc_url( $image_url[0] ) . ' ) no-repeat center center; background-size:cover;">';
}
}
/**
* Format html content
*
*/
$format = '<a href="%1$s" title="%2$s" class="post-%3$s">%4$s%2$s</span></br><span class="content-%3$s">%5$s</span></br><span class="more">%6$s</span></a>';
/**
* Formatted string post content
*
*/
$content = sprintf( $format,
get_permalink(),
get_the_title(),
get_the_ID(),
$img_cover,
get_the_excerpt(),
__( 'Read More', 'text-domain' )
);
/**
* Set an array of each post for output loop
*
*/
$result[ $term_slug ][] = array(
'post_id' => get_the_ID(),
'post_content' => $content,
'term_name' => $term_name,
'term_id' => $term_id
);
}
}
wp_reset_postdata(); // post reset
/**
* Check existing output
*
*/
if ( ! $result )
return;
/**
* Output loop
*
*/
$output = '';
foreach ( $result as $slug => $data )
{
$count = count( $data );
for ( $i = 0; $i < $count; $i++ )
{
/**
* Set data as object
*
*/
$post = ( object ) array_map( 'trim', $data[ $i ] );
if ( 0 == $i )
{
/**
* Set category id and name
*
*/
$output .= '<div id="term-category-' . absint( $post->term_id ) . '">';
$output .= '<h3>' . esc_html( $post->term_name ) . '</h3>';
}
/**
* Set post id and content
*
*/
$output .= '<div id="post-' . absint( $post->post_id ) . '"><p>' . $post->post_content . '</p></div>';
}
$output .= '</div>';
}
return $output; // complete
}
您可以根据需要更改元素结构。请注意,使用此代码,每个类别中没有重复的 post,并确保更改代码中注释的 post 类型和分类等值。希望对您有所帮助。