2016-11-04 26 views
0

私は助けてください、私は立ち往生しています。私はコメントの下にカテゴリに基づいて関連記事を配置する必要がありますまた、リストの各コンテンツに添付された写真を追加し、さらに読む。私が何を意味するのか理解するために、atachedの画像を参照してくださいワードプレスサイトの位置関係の記事

Single.php

<?php 
if (! defined('ABSPATH')) exit; // Exit if accessed directly 
get_header(); 

$post_type = get_post_type(); 
$sidebarPosition = get_option (THEME_NAME."_sidebar_position"); 
$sidebarPositionCustom = get_post_meta ($post->ID, THEME_NAME."_sidebar_position", true); 

$related = ci_get_related_posts(get_the_ID(), 3); 

if($related->have_posts()): 
    ?> 
    <div class="related-posts"> 
    <h3>Related posts</h3> 
    <ul> 
     <?php while($related->have_posts()): $related->the_post(); ?> 
     <li> 
      <h4><?php the_title(); ?></h4> 
      <?php the_excerpt(); ?> 
     </li> 
     <?php endwhile; ?> 
    </ul> 
    </div> 
    <?php 
endif; 
wp_reset_postdata(); 

if($post_type == "gallery") { 
    get_template_part(THEME_INCLUDES.'gallery-single','style-1'); 
} else { 
    get_template_part(THEME_INCLUDES.'news','single'); 
    get_footer(); 
} 


?> 

のfunctions.php

function ci_get_related_posts($post_id, $related_count, $args = array()) { 
    $args = wp_parse_args((array) $args, array(
    'orderby' => 'rand', 
    'return' => 'query', // Valid values are: 'query' (WP_Query object), 'array' (the arguments array) 
)); 

    $related_args = array(
    'post_type'  => get_post_type($post_id), 
    'posts_per_page' => $related_count, 
    'post_status' => 'publish', 
    'post__not_in' => array($post_id), 
    'orderby'  => $args['orderby'], 
    'tax_query'  => array() 
); 

    $post  = get_post($post_id); 
    $taxonomies = get_object_taxonomies($post, 'names'); 

    foreach($taxonomies as $taxonomy) { 
    $terms = get_the_terms($post_id, $taxonomy); 
    if (empty($terms)) continue; 
    $term_list = wp_list_pluck($terms, 'slug'); 
    $related_args['tax_query'][] = array(
     'taxonomy' => $taxonomy, 
     'field' => 'slug', 
     'terms' => $term_list 
    ); 
    } 

    if(count($related_args['tax_query']) > 1) { 
    $related_args['tax_query']['relation'] = 'OR'; 
    } 

    if($args['return'] == 'query') { 
    return new WP_Query($related_args); 
    } else { 
    return $related_args; 
    } 
} 

答えて

0

私はあなたが関連する適用しないpost_type:ここでは

Post position to resolve

は私の現在のコードですしかし、あなたはあなたが挿入したコードをsingle.phpにテンプレートpに挿入することができます必要なアート(例:single-content-news.php)。あなたのコードを追加できる場所を正確に確認することができます。このテンプレートを探す

また、配置しようとすることができます

if($post_type == "gallery") { 
    get_template_part(THEME_INCLUDES.'gallery-single','style-1'); 
    // does this template emebd the footer ? if not, if you want add get_footer(); in this statement 
} else { 
    get_template_part(THEME_INCLUDES.'news','single'); 
    get_footer(); 
} 

$related = ci_get_related_posts(get_the_ID(), 3);

前に
関連する問題