相关帖子,wordpress查询

时间:2015-08-11 12:17:32

标签: php wordpress wp-query

我一直用于我的wp网站的主题有相关的帖子短代码,但它并没有隐藏当前查看的帖子。我有一些想法在查询之前将变量添加到帖子中,以便查询看到当前查看的帖子将该变量设置为true并跳过它。这是代码:

<?php 
global $post;
$categories = get_the_category();
$ceker=false;

foreach ($categories as $category[0]) {
if ($ceker == false){
    $ceker=true;    
    ?>
content etc...

我在自己的主题中使用它并且它有效。我该如何做一些类似于这个主题使用的短代码的东西:

#BLOG LIST...
if(!function_exists('dt_blog_posts')) {

    function dt_blog_posts( $atts, $content = null ) {
        extract(shortcode_atts(array(
            'show_feature_image' => 'true',                              
            'excerpt_length' => 35,
            'show_meta' => 'true',
            'limit' => -1,
            'categories' => '',
            'posts_column' => 'one-column',     // one-column, one-half-column, one-third-column
        ), $atts));

        global $post;
        $meta_set = get_post_meta($post->ID, '_tpl_default_settings', true);
        $page_layout = !empty($meta_set['layout']) ? $meta_set['layout'] : 'content-full-width';
        $post_layout = $posts_column;

        $article_class = "";
        $feature_image = "";
        $column = ""; $out = "";

        //POST LAYOUT CHECK...
        if($post_layout == "one-column") {
            $article_class = "column dt-sc-one-column blog-fullwidth";
            $feature_image = "blog-full";
        }
        elseif($post_layout == "one-half-column") {
            $article_class = "column dt-sc-one-half";
            $feature_image = "blog-twocolumn";
            $column = 2;
        }
        elseif($post_layout == "one-third-column") {
            $article_class = "column dt-sc-one-third";
            $feature_image = "blog-threecolumn";
            $column = 3;
        }

        //PAGE LAYOUT CHECK...
        if($page_layout != "content-full-width") {
            $article_class = $article_class." with-sidebar";
            $feature_image = $feature_image."-sidebar";
        }

        //POST VALUES....
        if($categories == "") $categories = 0;
        //PERFORMING QUERY...

        if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
        elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
        else { $paged = 1; }

        $args = array('post_type' => 'post', 'paged' => $paged, 'posts_per_page' => $limit, 'cat' => $categories, 'ignore_sticky_posts' => 1);
        $wp_query = new WP_Query($args);
        $pholder = dt_theme_option('general', 'disable-placeholder-images');
        if($wp_query->have_posts()): $i = 1;
         while($wp_query->have_posts()): $wp_query->the_post();

            $temp_class = $format = "";

            if($i == 1) $temp_class = $article_class." first"; else $temp_class = $article_class;
            if($i == $column) $i = 1; else $i = $i + 1;

                $out .= '<div class="'.$temp_class.'"><!-- Post Starts -->';

                $out .= '<article id="post-'.get_the_ID().'" class="'.implode(" ", get_post_class("blog-post", $post->ID)).'">';

                    if($show_meta != "false"):

                        $out .= '<div class="post-details"><!-- Post Details Starts -->';

                                $out .= '<div class="date"><span>'.get_the_date('d').'</span>'.get_the_date('M').'<br />'.get_the_date('Y').'</div>';

                                $out .= '<div class="post-comments">';
                                    $commtext = "";
                                    if((wp_count_comments($post->ID)->approved) == 0)   $commtext = '0';
                                    else $commtext = wp_count_comments($post->ID)->approved;
                                    $out .= '<a href="'.get_permalink().'/#comments">'.$commtext.' <i class="fa fa-comment"></i></a>';
                                $out .= '</div>';

                                $format = get_post_format();                                
                                $out .= '<span class="post-icon-format"> </span>';
                        $out .= '</div><!-- Post Details ends -->';

                    endif;              

                    $out .= '<div class="post-content"><!-- Post Content Starts -->';

                        $out .= '<div class="entry-thumb">';
                            if(is_sticky()):
                                $out .= '<div class="featured-post">'.__('Featured','iamd_text_domain').'</div>';
                            endif;
                            //POST FORMAT STARTS

                            if( $format === "image" || empty($format) ):
                              if( has_post_thumbnail() && $show_feature_image != 'false'):
                                  $out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
                                      $attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
                                  $out .= '</a>';
                              elseif($pholder != "true" && $show_feature_image != 'false'):
                                  $out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
                                      $out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
                                  $out .= '</a>';
                              endif;
                            elseif( $format === "gallery" ):
                              $post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
                              $post_meta = is_array($post_meta) ? $post_meta : array();
                              if( array_key_exists("items", $post_meta) ):
                                  $out .= "<ul class='entry-gallery-post-slider'>";
                                  foreach ( $post_meta['items'] as $item ) { $out .= "<li><img src='{$item}' alt='gal-img' /></li>";    }
                                  $out .= "</ul>";
                              endif;
                            elseif( $format === "video" ):
                                  $post_meta =  get_post_meta($post->ID ,'_dt_post_settings', true);
                                  $post_meta = is_array($post_meta) ? $post_meta : array();
                                  if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
                                      if( array_key_exists('oembed-url', $post_meta) ):
                                          $out .= "<div class='dt-video-wrap'>".wp_oembed_get($post_meta['oembed-url']).'</div>';
                                      elseif( array_key_exists('self-hosted-url', $post_meta) ):
                                          $out .= "<div class='dt-video-wrap'>".wp_video_shortcode( array('src' => $post_meta['self-hosted-url']) ).'</div>';
                                      endif;
                                  endif;
                            elseif( $format === "audio" ):
                                  $post_meta =  get_post_meta($post->ID ,'_dt_post_settings', true);
                                  $post_meta = is_array($post_meta) ? $post_meta : array();
                                  if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
                                      if( array_key_exists('oembed-url', $post_meta) ):
                                          $out .= wp_oembed_get($post_meta['oembed-url']);
                                      elseif( array_key_exists('self-hosted-url', $post_meta) ):
                                          $out .= wp_audio_shortcode( array('src' => $post_meta['self-hosted-url']) );
                                      endif;
                                  endif;
                            else:
                              if( has_post_thumbnail() && $show_feature_image != 'false'):
                                  $out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
                                      $attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
                                  $out .= '</a>';
                              elseif($pholder != "true" && $show_feature_image != 'false'):
                                  $out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
                                      $out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
                                  $out .= '</a>';
                              endif;
                           endif;
                           //POST FORMAT ENDS
                        $out .= '</div>';

                        $out .= '<div class="entry-detail">';
                            $out .= '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
                            if($excerpt_length != "" || $excerpt_length != 0) $out .= dt_theme_excerpt($excerpt_length);
                        $out .= '</div>';

                        if($show_meta != "true"):
                            $out .= '<div class="post-meta">';
        $out .= '<div class="opsirnije">';
                            $out .= '<a href="'.get_permalink().'">'.'Opširnije &#187;'.'</a>';
                                $out .= '<ul>';
                                    $out .= '<li><span class="fa fa-user"></span><a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a></li>';
                                    $categories = get_the_category();
                                    $thiscats = "";
                                    if($categories) {
                                    foreach($categories as $category) {
                                        $thiscats .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s",'iamd_text_domain' ), $category->name ) ) . '">'.$category->cat_name.'</a>, '; }
                                    $thiscats = substr($thiscats,0, (strlen($thiscats)-2));
                                    $out .= '<li><span class="fa fa-thumb-tack"></span>'.$thiscats.'</li>';
                                    }
                                    $out .= get_the_tag_list('<li><span class="fa fa-pencil"></span>', ', ', '</li>');
                                $out .= '</ul>';
                            $out .= '</div>';
                        endif;
                    $out .= '</div><!-- Post Content Ends -->';
                $out .= '</article>';
            $out .= '</div><!-- Post Ends -->';
         endwhile;

         if($wp_query->max_num_pages > 1):
            $out .= '<div class="pagination-wrapper">';
                if(function_exists("dt_theme_pagination")) $out .= dt_theme_pagination("", $wp_query->max_num_pages, $wp_query);
            $out .= '</div>';
         endif;
         wp_reset_query($wp_query);
        else:
            $out .= '<h2>'.__('Nothing Found.', 'iamd_text_domain').'</h2>';
            $out .= '<p>'.__('Apologies, but no results were found for the requested archive.', 'iamd_text_domain').'</p>';
        endif;

        return $out;
    }
    add_shortcode('dt_blog_posts', 'dt_blog_posts');
    add_shortcode('dt_sc_blogposts', 'dt_blog_posts');
}

1 个答案:

答案 0 :(得分:0)

答案很简单。刚刚添加

 'post__not_in' => array( get_the_ID() ))

在数组中。

所有......