0

Using the category.php template file and I can get all other information about a post except the excerpt or the content. Here's the code I'm using:

$cat = get_queried_object();
$posts = get_posts(array('posts_per_page' => 15, 'offset' => 0, 'category' => $cat->term_id));

                foreach($posts as $post)
                {
                    $postID = $post->ID;
                    $title = get_the_title($postID);
                    $link = get_permalink($postID);
                    $date = get_the_date('M. j, Y', $postID);
                    $authors = coauthors_posts_links(null, null, null, null, false);
                    $excerpt = get_the_excerpt($postID);
                    $thumb = get_the_post_thumbnail($post, array('class' => 'front-page-tease-sm'));

                    echo '<div class="category-post">';

                    // Left box - date
                    printf('<div class="date">%1$s</div>', esc_attr($date));

                    // Middle box flex - headline, author, and excerpt
                    echo '<div class="category-post-info">';
                    printf('<a class="category-headline" href="%1$s">%2$s</a>', esc_attr($link), esc_html($title));
                    printf('<div class="category-author">By %1$s</div>', $authors);
                    printf('<div class="category-tease">%1$s</div>', $excerpt);
                    echo '</div>';

                    // Right box - thumbnail
                    printf($thumb);

                    echo '</div>';
                }

This code does exactly what I expect it to do except for when it comes to excerpts. It prints nothing for the excerpt, not even HTML tags.

  • Just try this and let me know. $excerpt =apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id)); instead of $excerpt = get_the_excerpt($postID); – Ranuka Nov 16 '16 at 1:09
  • No luck with that either – Mike Nov 16 '16 at 1:24
  • wordpress.stackexchange.com/a/152133/106350 Read this answer also – Ranuka Nov 16 '16 at 3:02
  • I added that function but still nothing. I printed the length of $excerpt and it's coming out as 0. – Mike Nov 16 '16 at 3:24
  • Do you have debugging turned on? And if do a data dump on your $post objects does post_content or post_excerpt have any data? – ngearing Nov 16 '16 at 4:22
0

I have checked the code. There are some issues in your code :

  1. There is not any method 'coauthors_posts_links' but WordPress provides a method the_author_posts_link();.
  2. Also for the 'get_the_post_thumbnail()' function 2nd parameter must be image size like
    $thumb = get_the_post_thumbnail($post, 'thumbnail',array('class' => 'front-page-tease-sm'));

I have updated the code. For 'coauthors_posts_links()' no idea what you want to do here ?


$cat = get_queried_object();
$posts = get_posts(array('posts_per_page' => 15, 'offset' => 0, 'category' => 1));

                foreach($posts as $post)
                {
                    $postID = $post->ID;
                    $title = get_the_title($postID);
                    $link = get_permalink($postID);
                    $date = get_the_date('M. j, Y', $postID);
                    $authors = '';
                    $excerpt = get_the_excerpt($postID);
                    $thumb = get_the_post_thumbnail($post, 'thumbnail',array('class' => 'front-page-tease-sm'));

                    echo '';

                    // Left box - date
                    printf('%1$s', esc_attr($date));

                    // Middle box flex - headline, author, and excerpt
                    echo '';
                    printf('%2$s', esc_attr($link), esc_html($title));
                    printf('By %1$s', $authors);
                    printf('%1$s', $excerpt);
                    echo '';

                    // Right box - thumbnail
                    printf($thumb);

                    echo '';
                }
  • coauthors_posts_links() is a function from a plugin, and is irrelevant to this question. The issue is with the excerpt, and only with the excerpt. The code you "fixed" does not resolve this issue. – Mike Nov 16 '16 at 2:20
  • 1
    Hey Mike, for someone asking for free help, this was a bit rude. – rudtek Nov 16 '16 at 5:06
  • I'm sorry but it is extremely frustrating to ask a question and then have someone completely ignore it and give a totally irrelevant answer that helps nothing. – Mike Nov 16 '16 at 16:53
  • did my code below help? – rudtek Nov 16 '16 at 17:37
  • @Mike I executed your code and the issues I have found mentioned above. 'get_the_excerpt() ' worked fine on my side except 'get_the_post_thumbnail()' which have issue. I mentioned in the answer. You can use the <a href=''wordpress.org/plugins/query-monitor/'>Query Monitior<a/> plugin for debugging. Also, there will be a log will under wp-content folder. tommcfarlin.com/viewing-the-php-debug-log check the post for enabling debugging in wordpress – Syed Fakhar Abbas Nov 16 '16 at 20:14
0

I'm going to say you have a filter running on the excerpt.

Have you tried just echoing it instead of print?

<?php echo 'here is my excerpt: '.$excerpt;?>

Also if there is a filter somewhere and you can't find it, you could try:

//$excerpt = get_the_excerpt(); // This doesn't work

// Let's do it like the get_the_excerpt filter function in the core does
// Get Excerpt directly without filters.
$excerpt = $post->post_excerpt;

// Choose what to do based on whether it's empty or not
if ($excerpt != '') echo $excerpt;
else echo 'some fallback';
  • I've tried echoing with no luck. Your other suggestion didn't work either. – Mike Nov 16 '16 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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