简体   繁体   English

WordPress的帖子没有拉内容

[英]Wordpress posts not pulling content

This is my code (below) I am attempting to pull posts only from category id 4 and It is working however it is not pulling the post content. 这是我的代码(如下),我仅尝试从类别ID 4拉出帖子,并且它正在工作,但是没有拉出帖子内容。 It is displaying everything else. 它正在显示其他所有内容。

Can anyone help me with this? 谁能帮我这个?

<?php  get_header(); ?> 
<div id="content" class="fixed">
<?php
    echo '<div class="row fixed">';
        echo '<div class="col580 no-print">';

        global $post;
        $myposts = get_posts('category=4');
foreach($myposts as $post) :
?>
  <div class="post-item">
  <?php

    $src = null;
    $count = 0;

    $readmorelabel = get_option(EWF_SETUP_THNAME."_blog_read_more", __('&#8212; Read More', EWF_SETUP_THEME_DOMAIN));                   

        $count++;

        //## Get post classes
        //##
        $post_class = get_post_class();
        $post_class_fin = null;

        foreach($post_class as $key=> $ctclass){
            $post_class_fin.= ' '.$ctclass;
        }


        //## Get post categories
        //##
         get_the_category( $post->ID );

         $post_categories = null;
         foreach((get_the_category( $post->ID )) as $category) { 
            if ($post_categories == null){
                $post_categories.= '<a href="'.get_category_link( $category->term_id ).'" >'.$category->cat_name.'</a>';
            }else{
                $post_categories.= ', <a href="'.get_category_link( $category->term_id ).'" >'.$category->cat_name.'</a>';
            }
         }


        //## Get post featured image
        //##
        $image_id = get_post_thumbnail_id($post->ID);  
        $image_url = wp_get_attachment_image_src($image_id,'blog-featured-image');  


        $src .= '<div class="blog-post '.$post_class_fin.'">';

            $src .= '<div class="blog-post-date">'.get_the_time('d').' <span>'.get_the_time('M Y').'</span></div>' ;
            $src .= '<h3 class="blog-post-title"><a href="' . get_permalink() . '">'.get_the_title($post->ID).'</a></h3>' ;


            $src .= '<ul class="blog-post-info fixed">
                        <li class="categories">'.$post_categories.'</li>

                        <li class="comments"><a href="'.get_permalink().'#comments">'.get_comments_number().' '.__('Comments', EWF_SETUP_THEME_DOMAIN).'</a></li>
                     </ul>';

            if ($image_id){
                $src .= '<div><a href="'.get_permalink().'"><img class="blog-post-thumb" src="'.$image_url[0].'" width="480" height="200" alt="" /></a></div>';
            }

            global $more;
            $more = false;          
            $src .= '<p>'.do_shortcode(get_the_content('&nbsp;')).'</p>';   

            $more = true;

            $src .= '<div class="fixed"><p class="blog-post-readmore"><a href="'.get_permalink().'">'.$readmorelabel.'</a></p></div>';

            if ($wp_query_blog->post_count != $count ){
                $src .= '<div class="hr"></div>'; 
            } 

        $src .= '</div>';  




    if ($wp_query->found_posts > $wp_query->query_vars['posts_per_page']){
        $src .= ewf_sc_blog_navigation_steps($wp_query->query_vars['posts_per_page'], $wp_query);
        }

    echo $src;

?>



  </div>

<?php comments_template(); ?>
<?php endforeach; wp_reset_postdata(); 



        echo '</div>';

        echo '<div class="col280 last">';
            if ( !function_exists('dynamic_sidebar')  || !dynamic_sidebar('sidebar-page') );
        echo '</div>';


    echo '</div>'; 

?>

</div>

<?php   get_footer();  ?>

You are using get_posts() so you can easily get the post content by using post_content. 您正在使用get_posts(),因此可以使用post_content轻松获取帖子内容。

I see you use are using this 我看你用的是用这个

global $post;
$myposts = get_posts('category=4');
foreach($myposts as $post) :

Please avoid using global variables as local variables, by doing so you might change its value and you might face some issue later, rather do something as below 请避免将全局变量用作局部变量,这样做可能会更改其值,以后可能会遇到问题,请执行以下操作

global $post;
$myposts = get_posts('category=4');
foreach($myposts as $mypost) :

and after this you can easily get the details of each post inside the for each loop for example 然后,您可以轻松地在for每个循环中获取每个帖子的详细信息,例如

global $post;
$myposts = get_posts('category=4');
foreach($myposts as $mypost) :
  echo $mypost->post_title; // This gives you the post title
  echo $mypost->post_content; // This gives you the post content
endforeach;

You can try and take a reference from the above and try with your code. 您可以尝试从上面获取参考,然后尝试使用您的代码。

Base line: if you are using get_posts() you can get the content of the post from $variable->post_content; 基线:如果您使用的是get_posts(),则可以从$ variable-> post_content获取帖子的内容;

Hope it helps!!! 希望能帮助到你!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM