2

I am trying to modify my loop to save post data by category. I found some code on this site that saved the post's titles based on their categories and tried to modify this to save the post's content. However, while get_the_title and get_the_category work, get_the_content returns null.

Here is the code:

if ( false === ( $q = get_transient( 'category_list' ) ) ) {

    $args = array( 
        'posts_per_page' => -1
    );

    $query = new WP_Query($args); 

    $q = array();

    $body = array();

    while ( $query->have_posts() ) { 

        $query->the_post(); 

        $a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';

        $post_id = get_the_ID();

        $post_id = $post->ID;

        $body[$post_id] = array();

        $body[$post_id]['title'] = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>'; //works

        $body[$post_id]['content'] = get_the_content('Read more');

        $categories = get_the_category();

        foreach ( $categories as $key=>$category ) {

            $b = '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a>';

        }

        $q[$b][] = $post_id; // Create an array with the category names and post titles

    }


    /* Restore original Post Data */
    wp_reset_postdata();

    set_transient( 'category_list', $q, 12 * HOUR_IN_SECONDS );
  }

Edit: Here is how I am using the $body array:

foreach($q[$b] as $post) {
  echo('<div class="teaser"><div class="teaser-title"><a href = "">' . $post . '</a></div><div class="teaser-text">'. $body[$post] . '</div><div class="teaser-footer"><p>pemsource.org</p></div></div>');
}

Edit2: I added the full code. When I do a var dump of body I get NULL and when I do a var dump of $q I get

array(3) { ["Conundrums"]=> array(1) { [0]=> string(64) "new post" } ["Tips and Tricks"]=> array(1) { [0]=> string(80) "Tips and tricks" } ["Uncategorized"]=> array(1) { [0]=> string(78) "Tips and Tricks" } }

seemingly regardless of how I edit the loop. I am very confused. Any help is much appreciated

10
  • Does that post have content? And your saving the $content to the array $body, but where are you using $body?
    – ngearing
    Commented Apr 25, 2016 at 23:44
  • I will update the question
    – Jared
    Commented Apr 25, 2016 at 23:56
  • try global $post; $content = $post->post_content;
    – majick
    Commented Apr 26, 2016 at 0:39
  • where you put your loop?
    – Jevuska
    Commented Apr 26, 2016 at 1:39
  • 1
    whoops I mean: global $post; $content = apply_filters('the_content',$post->post_content);
    – majick
    Commented Apr 26, 2016 at 2:18

2 Answers 2

2

echo $post->post_content; will echo your post content. Keep in mind though, it's raw out of the database (same as get_the_content()). If you want to apply the same filters that the_content() receives, follow the instructions in the codex:

<?php
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
?>
2
  • how about the text link 'Read more'?
    – Jevuska
    Commented Apr 26, 2016 at 10:28
  • For Read More link <a href="<?php echo get_permalink(); ?>"> Read more &raquo; <?php echo get_the_title(); ?></a>
    – Owais Alam
    Commented Apr 26, 2016 at 11:54
-1

maybe try <?php instead of <? in your lines of code, not sure but I understand that this can cause problems.

or use <?php echo $post->post_content; ?> and try without 'Read More' means only <?php the_content(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.