ngplayground
ngplayground

Reputation: 21617

Wordpress ignores excerpt

Using the below I am able to populate a list and the text from my posts into my Sidebar in Wordpress.

I have placed the <!--more--> tag into my post but it seems to get ignored using the below script.

Could someone assist please

PHP in Wordpress

<?php $the_query = new WP_Query( 'showposts=2' ); ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <li>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
        <p><?php the_content('<span class="readmore strong">'.__('Read more').'</span>'); ?></p>
    </li>
    <?php endwhile;?>

Upvotes: 1

Views: 174

Answers (2)

ztirom
ztirom

Reputation: 4438

If you get only one post back the <!--more--> will be ignored, see the_excerpt() vs. the_content()

Sometimes, it is more meaningful to use only the the_content() function which will decide what to display according to whether the <!--more--> quicktag was used. The <!--more--> quicktag splits a post into two parts; only the content before the tag should be displayed in listing. Remember that <!--more--> is (of course) ignored when showing a single post.

Just try to use <?php the_excerpt(); ?>.

Update:
Like I said in the comments and Donald got helped from the WordPress Support, the solution seems to be to override the global $more like WordPress Reference:

<?php global $more; $more = 0; ?>

In the context of this question:

<?php $the_query = new WP_Query( 'showposts=2' ); ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <?php global $more; $more = 0; ?>
    <li>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
        <p><?php the_content('<span class="readmore strong">'.__('Read more').'</span>'); ?></p>
    </li>
    <?php endwhile;?>

should do it.

Upvotes: 1

ngplayground
ngplayground

Reputation: 21617

Answer from Wordpress Support

functions.php

function force_more() {
    global $more;
    $more = 0;
}

sidebar.php

<?php
  force_more();
  query_posts('category_name=mycategory&showposts=5');
  while (have_posts()) : the_post();
?>

Upvotes: 0

Related Questions