在 wordpress 中将 Jet pack 相关文章定制为我自己的设计

customise Jet pack related articles to my own design in wordpress

我需要自定义相关文章的默认设计,从 wordpress 中的 Jet pack 到我的自定义设计我无法在我的代码中找到错误的地方。我在 functions.php 文件中写了一个钩子。

这是我的钩子functions.php

function jetpackme_custom_related() {
$posts = '<div class="single-article-popularGi">';
$posts .= '<h1>related</h1><div class="row">';

if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) {
    $related = Jetpack_RelatedPosts::init_raw()
        ->get_for_post_id(
            get_the_ID(),
            array( 'size' => 2 )
        );

    if ( $related ) {
        foreach ( $related as $result ) {
            // Get the related post IDs
           $title = get_the_title( $result[ 'id' ] );
           $link = get_permalink( $result[ 'id' ] );
           $image = featuredOrFirstImage($result[ 'id' ], 'blog-post-image');
           $category_name = get_the_category( $result[ 'id' ] );


           $posts .= '<div class="col-sm-3 col-3"><span class="thumbnail-image"><a href="'.$link.'">'.$image.'</a></span></div>';
           $posts .= '<div class="post-details col-sm-3 col-3 align-self-center"><div class="entry-cat post-category">'.$category_name.'</div>';
           $posts .= '<div class="mostPopularTitle"><h2 class="entry-title"><a class="post-title" href="%s" rel="bookmark">'.$title.'</a></h2>';

           $posts .= '<div class="readArticle"><a class="readMorePost" href="'.$link.'">MORE GI></a>';
        }
    }
}

$posts .= '</div>';
$posts .= '</div>';

// Return a list of post titles separated by commas
if( $related ){ 
   return $posts;
}else{
  return false;
}
}
add_action('admin_init', 'jetpackme_custom_related');

我在 single.php 中这样调用过它

<?php echo $related = jetpackme_custom_related();?>

但是什么都没有显示,有时会出现什么都不显示的错误。任何人都可以让我解决它..

我改变了程序的方式,它适用于 me.My 代码是

$related_posts = array();
$query = array();
$query['showposts'] = 2;// Number of posts to show
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) :
    $related = Jetpack_RelatedPosts::init_raw()
     ->set_query_name( 'theme-custom' ) // optional, name can be anything
     ->get_for_post_id( get_the_ID(), array( 'size' => $query['showposts'] )
  );
  if ( $related ) :
     foreach ( $related as $result ) :
        $related_posts[] = $result[ 'id' ];
     endforeach;
  endif;
endif;

if ( $related_posts ) {
  $query['post__in'] = $related_posts;
  $query['orderby'] = 'post__in';
  $title = __( 'Related', 'prefix' );
} else {
  $query['post__not_in'] = array( $post->ID );
  $title = __( 'Related', 'prefix' );
}

我把相关文章称为..

<div class="single-article-popularGi">
  <h1><?php esc_attr_e( $title ); ?></h1>
  <div class="row">  
     <?php $related = new WP_Query( $query ); ?>
        <?php while ( $related->have_posts() ) : $related->the_post(); ?>
           <div class="col-sm-3 col-3">
              <div class="thumbnail-image">
                 <a href="<?php the_permalink();?>"><span class="thumb-wrap"><?php the_post_thumbnail('medium', array("class" => "img-fluid")); ?></span></a>
              </div>
           </div>
           <div class="post-details col-sm-3 col-3 align-self-center">
              <?php if ( has_category() ) : ?>
                 <div class="entry-cat post-category">
                    <?php the_category(' '); ?>
                 </div><!-- end .entry-cats -->
              <?php endif; // has_category() ?>
              <div class="mostPopularTitle">
                 <?php the_title( sprintf( '<h2 class="entry-title"><a class="post-title" href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
              </div>
              <i class="postIntro"><?php echo $intro = wp_trim_words( get_field('intro' ), 20,'...' ); ?></i>
              <div class="readArticle">
                 <a class="readMorePost" href="<?php echo get_permalink(); ?>">MORE GI></a>
              </div>
           </div>
        <?php endwhile; 
     wp_reset_query(); ?>
  </div>