2016-11-04 26 views
0

请帮助我,我卡住了。我需要在评论下根据类别定位相关帖子,还可以将附加的图片添加到列表中的每个内容以及阅读更多内容。请参阅图片了解我的意思。在wordpress站点上的位置相关文章

Post position to resolve

这里是我当前的代码:

的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要应用相关帖子,但是你可以将你放在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);