What is the syntax for printing the sequence of category name and its follow up posts in my new template in a WordPress site? I have tried a lot via Google but all are not working properly.
There are many ways to do this, but here is a simple one (you will have to improve it with the category and posts links at least):
<?php $categories= get_categories();
if( !empty($categories) ):
?>
<ul>
<?php foreach ($categories as $category): ?>
<li>
<?=$category->cat_name?>
<?php $posts = get_posts($category->cat_id);
if( !empty($posts) ):
?>
<ul>
<?php foreach( $posts as $post ): ?>
<li><?= $post->post_title; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Related
In a post (single.php) I want to list other posts in the category that post is included in.
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
?>
<ul>
<?php query_posts( 'showposts=5&orderby=date&cat=$cat_id' ); ?>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
Articles in all categories come with this code. However, I only want the articles in the category to which that post belongs to be listed.
I found the solution.
<ul>
<?php
foreach((get_the_category()) as $category) :
$cat= $category->cat_ID
?>
<?php $args = 'cat=' . $cat . '&orderby=date&order=ASC'; ?>
<?php query_posts( $args );?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php
the_title(); ?></a></li>
<?php endwhile; endforeach; ?>
</ul>
Everything about query_post () is here : https://developer.wordpress.org/reference/functions/query_posts/
Client's wordpress is pretty customized and has had tons of stuff gutted and only functions can really be used. Any idea how to customize this:
<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
to only display a certain category?
<?php
$catquery = new WP_Query( 'cat=1&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><?php the_title(); ?></h3></li>
</ul>
<?php endwhile; ?>
I am trying to create a single page that lists the content of each category. I have managed to create the list. I now need to get the name of the category. I have the following code:
<ul>
<li> CATEGORY NAME HERE </li>
<?php query_posts('cat=0'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
How to call the name of the first category (0)?
Current edit: Why won't multiple works?
<div class="first-col">
<ul>
<?php query_posts('cat=0'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li> <?php $category = get_the_category();
echo $category[0]->cat_name;
?> </li>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="first-col">
<ul>
<li> <?php $category = get_the_category();
echo $category[0]->cat_name;?> </li>
<?php query_posts('cat=3'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
You have to get the array of categories and echo the first one from the array.
http://codex.wordpress.org/Function_Reference/get_the_category
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
According to wordpress developers codex:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '' . esc_html( $categories[0]->name ) . '';
}
This will give you the first category and also link it to that category's page.
there is also a shortcodes plug in that will help create lists based on categories, terms, etc. http://wordpress.org/plugins/display-posts-shortcode/
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 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;?>