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.
Related
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; ?>
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); ?
I have a global post query running, but for some reason my custom meta is not being output. When I try to call it inside a typical WordPress loop it works, but not in my code below. Is there any reason why this would be the case? Been trying to figure it out for an hour now....
<?php global $post; $cntr = 0; $myposts = get_posts('&post_type=go-deeper&posts_per_page=12');
foreach($myposts as $post) : setup_postdata($post);?>
<li class="<?php echo "slide_" . $cntr; ?>"><?php the_post_thumbnail('full'); ?></li>
<?php $cntr++; ?>
<?php endforeach; ?>
I just re-wrote the query and it works now:
<?php if(have_posts()): $cntr = 0;?>
<?php query_posts('&post_type=go-deeper&posts_per_page=12');?>
<?php while(have_posts()):the_post();?>
<?php $deeper_link = get_post_meta( get_the_ID(), 'll_deeper_link', true ); ?>
<li class="<?php echo "slide_" . $cntr; ?>">
<?php the_post_thumbnail('full'); ?>
</li>
<?php $cntr++; ?>
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php endif;?>
Now I've done this a couple other times with no problem, I have a main page using home.php and the blog as another page using "blog-home.php" blog template with all the right code to grab the posts but it's not displaying. The only difference is I've added a custom portfolio post field to the functions, would this be effecting it or could it be something else? I can access a post from the home page under latest post putting the code below but that's it.
<?php query_posts("post_per_page=1"); the_post(); ?>
<p><?php the_excerpt(); ?></p>
<?php wp_reset_query(); ?></div>
*UPDATE: I've tried another code but now it is only displaying the blog page as a post. *
<?php
/*
Template Name: Blog Home
*/
?>
<?php get_header(); ?>
<div id="contentwrapper">
<?php query_posts( array ( 'category_name' => 'Blog', 'posts_per_page' => 5 ) ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="blogentry">
<h4><?php the_title(); ?> </h4>
<?php the_content(); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_footer(); ?>
Maybe if you use
$posts = get_posts(array('numberposts' => 1));
global $post;
$post = $posts[0];
the_excerpt();
instead
query_posts();
is never a good idea change the global query if get_posts dosn't work for you try with WP_Query()
I'm building a Wordpress template from an HTML page.
I currently have
<?php $queried_post = get_post( $_GET['id'], $output ); ?>
Then I use ...
<?php echo $queried_post->post_title; ?>
Which works fine for echoing the post title.
Then I try to echo the author with...
<?php echo $queried_post->post_author; ?>
I get back '1' which is not the author name. How is this done correctly?
Try this code:
<?php the_author($_GET['id']); ?>
Codex entry: http://codex.wordpress.org/Function_Reference/the_author
My single.php file:
<?php get_header(); ?>
<?php if (have_posts()): ?>
<?php while (have_posts()): the_post(); ?>
<div class="post post-single">
<h1 class="post-title">
<?php the_title(); ?>
<?php edit_post_link('Edit', '', ''); ?>
</h1>
<div class="content"><?php the_content(); ?></div>
</div>
<?php endwhile; else: ?>
There are no posts to display.
<?php endif; ?>
<?php get_footer(); ?>