Wordpress: the_excerpt not working - wordpress

I recently updated wordpress and now all content is outputting for this module, except "the_excerpt()"
<?php
function blog_feed_content(){
?>
<ul id="blog_list" class="jscroll">
<?php
global $post;
$args = array('category' => 4 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="post clearfix" id="post-<?php the_ID(); ?>">
<div class="post-content clearfix">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-date"><?php the_time('F Y') ?></div>
<div class="post-text">
<?php the_excerpt(); ?>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php
}
function widget_blog_feed($args){
extract($args);
echo $before_widget;
echo $before_title;?>Blog Feed<?php echo $after_title;
blog_feed_content();
echo $after_widget;
}
function init_blog_feed() {
register_sidebar_widget(__('blog_widget'), 'widget_blog_feed');
}
add_action("plugins_loaded", "init_blog_feed");
?>
Why on earth isn't it outputting this one piece of content?
YOU'RE ALL AWESOME. Thanks.

setup_postdata is not exactly the same as using a normal WP_Query with the_post() so not all template tags work as expected with this method of displaying posts.
You should rewrite the code to use a custom WP_Query and traditional Loop rather than using a foreach to iterate through post objects.
Something like:
$myposts = new WP_Query('cat=4');
if( $myposts->have_posts() ) : while( $myposts->have_posts() ) : $myposts->the_post(); ?>
<li class="post clearfix" id="post-<?php the_ID(); ?>">
<div class="post-content clearfix">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-date"><?php the_time('F Y') ?></div>
<div class="post-text">
<?php the_excerpt(); ?>
</div>
</div>
</li>
<?php endwhile;
wp_reset_query;
endif; ?>
That should point you in the right direction. If you are set on sticking with the foreach and get_posts method you could always use some simple string functions (i.e. substr() to truncate a little excerpt from the post_content property of the $post object, like replacing the <?php the_content(); ?> line with:
<?php echo substr($post->the_content, 0, 80); // displays first 80 chars of post ?>

you should define id.
<?php echo get_the_excerpt($post->ID); ?

Related

Wordpress post object getting page title rather than object title

I have an ACF repeater, which repeats post objects. I am changing the postdata to the post object rather than the page so that i can get the title and thumbnail image. This works perfectly for the first one, however the subsequent objects pull the correct thumbnail but the title is pulled from the page title.
Heres the code:
<?php if( have_rows('service_repeater') ): ?>
<?php while ( have_rows('service_repeater') ) : the_row(); ?>
<?php $post_object = get_sub_field('service'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; ?>
<?php setup_postdata( $post ); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<a href="<?php the_permalink(); ?>" class="service">
<div class="background" style="background-image: url('<?php echo $url; ?>');"></div>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_field('read_more_text'); ?></p>
</div>
</a>
<?php unset($post_object, $post); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
Any help would be greatly appreciated!
Ok, so i removed the <?php unset($post_object, $post); ?> and it works now. Im concerned that this will give me trouble later down the line with variables however.

How to display associated WordPress category's posts on Magento product page (Fishpig integration)?

I'm having trouble filtering out just the associated category posts associated with a product in the posts.phtml block (using Fishpig). I tried to use the following solution to get the posts:
Fishpig Wordpress Magento Post issue
like so:
$categoryId = Mage::registry('wordpress_category')->getId();
$recentPostCollection = Mage::getModel('wordpress/post')->getCollection()
->addIsPublishedFilter()
->addCategoryIdFilter($categoryId)
->setOrder('post_date', 'desc')
->setPageSize($numPostsToShow)
;
Then modifying the original Related Posts block:
<?php //$posts = $this->getPosts() ?>
<?php $posts = $recentPostCollection; ?>
<?php if (count($posts) > 0): ?>
<div class="block block-blog block-recent-posts">
<?php if ($title = $this->getTitle()): ?>
<div class="block-title">
<strong><span><?php echo $this->__($title) ?></span></strong>
</div>
<?php endif; ?>
<div class="block-content">
<ul id="<?php echo $this->getListId() ?>">
<?php foreach($posts as $post): ?>
<li class="item">
<?php $image = $post->getFeaturedImage(); ?>
<img src="<?php echo $this->htmlEscape( $image->getAvailableImage() ) ?>" ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('<?php echo $this->getListId() ?>')</script>
</div>
</div>
<?php endif; ?>
And, well, that's not working. When I put a static number into $categoryID, I can at least get data into the $recentPostCollection, but the loops does not work with that data structure... Any help would be amazing! Thanks.
magento fishpig related posts display by category:->
<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php $getCategory = $this->escapeHtml($category->getId()); ?>
<div id="inhouse-tab"><?php echo Mage::getSingleton('core/layout')->createBlock('wordpress/sidebar_widget_posts')->setTemplate('wordpress/sidebar/widget/categoryposts.phtml')->setCategoryId($getCategory)->setPostCount(4)->toHtml(); ?></div>
<?php break; ?>
<?php endforeach; ?>
<?php endif; ?>

Pagination is not working properly in Wordpress

I am currently using wordpress in creating my websites. And i really find a problem regarding pagination. So basically to be able for you to understand what is really my problem, I will post two codes:
This is the first code:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php include (TEMPLATEPATH.'/inc/meta.php'); ?>
<div class="entry">
<?php
the_excerpt();
?>
</div>
<br/><br/>
<h3>Read More...</h3>
</div>
<?php
endwhile;
?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
In this lines of code, I used the regular loop in querying a post. Actually I did not modify the code. I also installed a plugin WP-PageNavi. There is no problem, pagination is working properly.
But when i put this line of code, about the regular loop. Pagination is not working properly. I put this line of code " " because I want only to show post in this said category.
2nd code:
Line of code
<?php query_posts('cat=8'); ?>
End line of code
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<br/><br/>
<h3>Read More...</h3>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
My question is: What should I do in order for the pagination to worked properly? I really need some help with this.
I think the problem is the because of the two The Loop(i.e while and endwhile loop) in a page.
instead of while use foreach loop. below is the example
<?php
$args = array('category' => '8');
$postArr = get_posts($args);
if($postArr){
foreach($postArr as $details){
?>
div <?php post_class() ?> id="post-<?php echo $details->ID; ?>">
<h2><?php echo $details->post_title; ?></h2>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php echo $details->post_excerpt(); ?>
</div>
<br/><br/>
<h3>Read More...</h3>
</div>
<?php
}
}else{
?>
<h2>Not Found</h2>
<?php
}
?>
Note Code not tested...

List wordpress posts by category which match page title

I'm trying to list posts in the category which shares the name of the page. I.e. If you are on the "Services" page it should display posts in the category "Services". I realize it is easy to do with conditionals such as:
<?php if ( (is_page('Groups')) ) { query_posts('category_name=groups');
while (have_posts()) { the_post();?>
<h2 class="title" id="sub">Upcoming Group Programs</h2>
<a href="<?php the_permalink() ?>">
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry"><?php the_content(); ?></div>
</div>
<?php } wp_reset_query(); //Restores Global Post Data }?>
But I would like to do this without having to set multiple specific conditionals, something like:
<?php //global $wp_query; // Uncomment if necessary, shouldn't be
$test = the_title();
$args = array( 'cat_name' => $test // ARGS HERE );
$args = array_merge( $args , $wp_query->query );
query_posts( $args ); while (have_posts()) { the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry"><?php the_content(); ?></div></div>
<?php } ?>
Any thoughts! Obviously I could interchange the page & category "name" with the "slug" or whatever works best. Thanks!
Thanks! I changed a few things around and got it working with your suggestion.
<?php
$catmatch = get_the_title();
//The Query
query_posts('category_name=' . $catmatch ); ?>
Hopefully on the last line there I did the concatenation correctly, it seems to work but if that isn't how it is supposed to be done properly please let me know!
Try changing:
$test = the_title();
to:
$test = get_the_title();
You may also have to remove the line with array_merge();

How to loop through a specific category on single.php in Wordpress?

I've created a custom page and it is set as my homepage, within this custom page I am pulling out the latest post from a specific category, I've also created a form of pagination which when clicked upon will take the user to single.php. My intention for the single.php is to have two custom loops.
Custom loop one
I want single.php to distinguish that it has came from the homepage and loop through all of the posts tagged with the same category as the one on the homepage.
Some of these posts will have to be tagged with more than one category, so the loop will have to know to ignore the other categories and just pay attention to the category in question. Does that make sense?
Custom loop two
If the user hasn't arrived from the homepage, single.php will just act as it normally does i.e, if the user comes from index.php (the blog) they will be taken to this second loop (blog post)
However I don't seem to be able to make the distinction between the two loops, I might be over complicating matters, as I've got a loop which wraps everything together and then I have a loop for my custom pagination.
Here is the code below to show you what I'm talking about
custompage.php (set to home) - This works just fine but I'll post it just incase anyone is able to tidy it up
<?php query_posts('cat=1'); ?>
<?php
$myPosts = new WP_Query();
$myPosts->query('showposts=1');
if (have_posts()) :
while ($myPosts->have_posts()) : $myPosts->the_post();
?>
<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
<div id="post-<?php the_ID(); ?>" class="info">
<h2><?php the_title(); ?></h2>
<ul class="nav">
<?php query_posts('posts_per_page=1&offset=1'); the_post(); ?>
<li class="prev">Previous</li>
<?php wp_reset_query(); ?>
<li class="next"></li>
</ul>
</div>
<!-- end .info -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
single.php - Currently broken
<?php if( in_category('1') ) { ?>
<!-- start -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="info">
<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
<h2><?php the_title(); ?></h2>
<ul class="nav">
<li class="prev"><?php previous_post_link('%link', ' ', 'true', '1') ?></li>
<li class="next"><?php next_post_link('%link', ' ', 'true', '1'); ?></li>
<!--li class="prev"><?php //previous_post_link('%link', '%title;', 'true', '1') ?></li>
<li class="next"><?php //next_post_link('%link', '%title;', 'true', '1'); ?></li-->
</ul>
</div>
<!-- end .info -->
<?php endwhile; else: ?>
<?php endif; ?>
<!-- end -->
<?php }else{ ?>
<div id="content" class="widecolumn" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('<p class="serif">Read the rest of this entry ยป</p>'); ?>
</div>
</div>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
<?php } ?>
The problem I seem to be running into is when a post has been tagged with two categories, wordpress doesn't seem to be able to make the distinction between the two categories and instead of carrying on to the next category it breaks and defaults to the second loop.
how about this?
HOMEPAGE
<?php
$myPosts = new WP_Query();
$myPosts->query('showposts=1&cat=1');
if (have_posts()) :
while ($myPosts->have_posts()) : $myPosts->the_post();
?>
<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
<div id="post-<?php the_ID(); ?>" class="info">
<h2><?php the_title(); ?></h2>
<ul class="nav">
<?php query_posts('posts_per_page=1&offset=1'); the_post(); ?>
<li class="prev">Previous</li>
<?php wp_reset_query(); ?> <-- not sure if this will reset the overall query
<li class="next"></li>
</ul>
</div>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
the queryposts('cat=1') at the top of your query wont run with that category, the category would have to be set inside your custom query
$myPosts->query('showposts=1&cat=1');
the 2nd page (SINGLE.PHP) with 2 loops..
if the user is coming from the homepage to a single page you want to attach the current category to the query, without this wordpress (while on the singles page will default to loop through all posts)
so for the singles page would the below be any good?
<?php if( in_category('1') ) { ?>
<!-- your selected category -->
<!-- start -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="info">
<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
<h2><?php the_title(); ?></h2>
<ul>
<php global $post;
$my_query = get_posts('numberposts=1&cat=1&offset=-1');
foreach($my_query as $post) :
setup_postdata($post);
$link = get_post_meta($post->ID, 'site-url', true); ?>
<li>
<php the_title(); ?>
</li>
<php endforeach; ?>
<php global $post;
$my_other_query = get_posts('numberposts=1&cat=1&offset=1');
foreach($my_other_query as $post) :
setup_postdata($post);
$link = get_post_meta($post->ID, 'site-url', true); ?>
<li>
<php the_title(); ?>
</li>
<php endforeach; ?>
</ul>
</div>
<!-- end .info -->
<?php endwhile; else: ?>
<?php endif; ?>
<!-- end -->
<?php }else{
include('standard-wp-singles-page-stuff.php');
} ?>
then once your singles page loads it will check for the current category in that post, if its in category 1, then it will load your custom loop, then it will loop through 2 posts, in the same category? giving you 2 links to other posts. also using an offset on that query should give you a link forward and a link backwards?
hope that helps..
fixing code layout.. didnt work too well :P

Resources