创建简码以在边栏中显示相关帖子(同一类别的帖子)

时间:2020-06-25 22:43:58

标签: php wordpress

我想在文章侧边栏中(在wordpress中)显示该类别所属的5篇近期文章的列表。我正在使用下面的代码(使用简码)显示5个帖子(在这种情况下,来自类别62)。有没有办法在functions.php中编写此代码,以便对其进行优化,而不必为每个新类别重写所有内容?

 /** 
  * function to add recent posts widget 
  */ 
function wpcat_postsbycategory_musculacao() {
 // the query
 $the_query = new WP_Query( array( 'cat' => '62', 'posts_per_page' => 5 ) ); 
 // The Loop
 if ( $the_query->have_posts() ) {
     $string .= '<ul class="postsbytag widget_recent_entries">';
     while ( $the_query->have_posts() ) {
         $the_query->the_post();
             if ( has_post_thumbnail() ) {
             $string .= '<li>';
             $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 80, 80) ) . get_the_title() .'</a></li>';
             } else { 
             // if no featured image is found
             $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
             }
             }
     } else {
     // no posts found
 }
 $string .= '</ul>';
 return $string;
 /* Restore original Post Data */
 wp_reset_postdata();
 }
 // Add a shortcode
 add_shortcode('categoryposts-musculacao', 'wpcat_postsbycategory_musculacao');
 
 /** 
  * function to change search widget placeholder 
  */
 function db_search_form_placeholder( $html ) {
        $html = str_replace( 'placeholder="Pesquisar ', 'placeholder="Buscar ', $html );
        return $html;
}
add_filter( 'get_search_form', 'db_search_form_placeholder' );

1 个答案:

答案 0 :(得分:0)

这里的复杂之处在于,在WP中,帖子可以具有多个类别。您需要决定如何处理-从所有类别获取信息,或者如果您只想显示一个类别的帖子,那么如何选择哪个类别?

根据您的处理方式,我在下面给出了一些答案。

1。获取当前帖子类别中 any 中的帖子

由于帖子可以具有多个类别,因此您可以获取所有ID,并使用它来查询这些类别的 any 中的帖子:

// 1. get the categories for the current post
global $post;
$post_categories = get_the_category( $post->ID );

// 2. Create an array of the ids of all the categories for this post
$categoryids = array(); 
foreach ($post_categories as $category)
    $categoryids[] = $category->term_id;

// 3. use the array of ids in your WP_Query to get posts in any of these categories
$the_query = new WP_Query( array( 'cat' => implode(",",$categoryids), 'posts_per_page' => 5 ) ); 

1a。获取任何类别中的帖子,但不是其子女

注释cat将包含这些类别ID的子级。如果只想包含那些确切类别而不是子类别,请使用category__in而不是cat

$the_query = new WP_Query( array('category__in' => $categoryids, 'posts_per_page' => 5) ); 

2。获取具有所有当前帖子类别的帖子

如果您希望发布的帖子与当前的帖子具有相同的类别,则可以通过与上述相同的方式来完成,除了我们使用category__and而不是cat(请注意,这是包括那些类别的孩子):

$the_query = new WP_Query( array('category__in' => $categoryids, 'posts_per_page' => 5) ); 

3。如果您知道自己的帖子只有一个类别

如果您知道每个帖子只有一个类别,那么您可以使用类别数组中的第一个元素:

// 1. Just use the id of the first category
$categoryid = $post_categories[0]->term_id;
$the_query = new WP_Query( array( 'cat' => $categoryid, 'posts_per_page' => 5 ) ); 

4。将类别传递到简码

如果要指定要显示的类别,可以将类别ID传递到短代码中,让您选择所需的任何类别。 (仅供参考,您也可以将其与slug(而不是id)一起使用,这可能会更加用户友好)

我假设您的简码目前是这样使用的:

[categoryposts-musculacao]

我们可以更改功能,以便您可以通过以下类别:

[categoryposts-musculacao category=62]

Shortcode函数可以接受$attributes参数,该参数包含我们要添加到短代码中的信息或“属性”,例如category。我们使用它来获取作为名为$category的变量传递的类别,然后只使用它而不是其余函数中的硬编码值。

// 1. include the $attributes argument
function wpcat_postsbycategory_musculacao(  $attributes ) {
    // 2. get the value passed in as category - this will save it into a variable called `$category`
    extract( shortcode_atts( array(
        'category' => ''
    ), $attributes ) );

    // 3. if there is no category don't do anything (or sohw a message or whatever you want
    if (!$category) return "";

    // 4. Now just use your variable instead of the hardcoded value in the rest of the code
     $the_query = new WP_Query( array( 'cat' => $category, 'posts_per_page' => 5 ) ); 
 
    // do the rest of your stuff!
}

参考:Wordpress Codex Shortcode API

4a。 slug将类别传递到短代码中

如果您希望您的短代码中的category属性适用于某条代码而不是ID,则只需更改WP_Query即可使用category_name而不是cat

$the_query = new WP_Query( array( 'category_name' => $category, 'posts_per_page' => 5 ) );