php
  • wordpress
  • jetpack
  • 2017-07-31 50 views 1 likes 
    1

    我需要自定義默認設計的相關文章從噴墨包在wordpress到我的自定義設計我無法找到錯誤,在我的代碼錯了。我在functions.php文件中寫了一個鉤子。自定義噴氣包相關文章到我自己的設計在wordpress

    這是我的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();?> 
    

    但沒有什麼顯示和獲得錯誤,不顯示一些時間沒有。任何人都可以請讓我解決出來..

    回答

    0

    我改變了程序的方式,它適用於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> 
    

    相關問題