We are setting up a custom blog layout override for a Joomla site. I am trying to display the sub categories articles under their category headings.
For example the following structure:
Main Cat
- Sub Cat
-- Article 1
-- Article 2
- Sub Cat
-- Article 3
This layout is how the articles should display on the main blog override. For example going to Main Cat would display the above layout.
The subcategories are included in the default way with the following:
<?php if (!empty($this->children[$this->category->id]) && $this->maxLevel != 0) : ?>
<div class="cat-children">
<?php if ($this->params->get('show_category_heading_title_text', 1) == 1) : ?>
<h3> <?php echo JText::_('JGLOBAL_SUBCATEGORIES'); ?></h3>
<?php endif; ?>
<?php echo $this->loadTemplate('children'); ?> </div>
<?php endif; ?>
In the child override the title is included then we would like to display the articles from that child category.
I have tried multiple different versions of getting a for each to work for the sub articles but cannot get it to work.
For example:
<?php foreach ($this->child->children[$child->id] as $key => $item) : ?>
1
<?php echo $item->title; ?>
<?php endforeach; ?>
Related
I'm trying to display Sub categories on main category page.
For example i have a category named as : live tv
And it is consist of some sub categories like : sports tv, cartoon tv, entertainment.
I want to display these sub categories with images inside content area of main category.
I was able to assign images to categories using plugin.
I have tried this code in category.php
<?php
if (is_category()) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
"&echo=0"); else
$this_category = wp_list_categories('orderby=id&depth=1&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
"&echo=0");
if ($this_category) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>
But that code isn't showing anything on my category page.
you can also get sub category as:
<?php global $wpdb;$prefix=$wpdb->prefix;
$subcateogyr_list=$wpdb->get_results("Select * from ".$prefix."term_taxonomy WHERE parent='parent_category_id'");
foreach($subcateogyr_list as $subcat
echo $subcat_name=$wpdb->get_var("select name from ".$prefix."wp_terms where term_taxonomy_id='$subcat['term_id']'");
}
?>
I have a normal posts page in an Anchor CMS theme. Beside the posts list is a category list, which I am intending to use as sorting filters for the blog posts. The category list has been output as follows, based on the Anchor Docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<div class="filter" data-filter=".category-<?php echo $id; ?>"><?php echo $category; ?></div>
<?php endforeach; ?>
I then have to include a corresponding class in each article or post within that list, which would look like this:
<?php if(has_posts()): ?>
<?php $i = 0; while(posts()): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endwhile; ?>
<?php endif; ?>
Notice that the article class category-[num] corresponds with the data-filter on the div "filter". This is what allows sorting.
However any way I try to do this I am getting either doubled up posts or just not working. I had tried using a foreach statement as seen in the docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endforeach; ?>
but this makes posts double up, I assume because it is within a while loop?
In the database, categories and posts are in two separate tables, however the category ID is included in the posts table. I have looked for a way to echo this e.g. article_category_id but with no success so far.
How can I include the category ID in the posts list?
Okay, found the answer to my own question.
This was a simple solution actually, once I thought about it. The various function references in Anchor CMS such as article_category etc can be defined by the user by accessing anchor > functions > articles.php.
Within this file, and based on existing functions such as
function article_category() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->title;
}
}
I created a new function for the category ID. It looks like this:
function article_category_id() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->id;
}
}
and then echoed this out in the posts loops:
<?php echo article_category_id(); ?>
Simple as that!
The functionality I am trying to add is due to the fact that there are links floating around there that could potentially land on a page that the product is out of stock, discontinued, etc. These types products are automatically set to a "catalog" view and "Not Visible Individually." we have overcome the fact they are no longer just getting a 404 error page, but now I would like to add the option to view other products from that same category?
I have this:
<?php $count =0; ?>
<?php $categories = $_product->getCategoryIds();?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<li> <?php echo $_category->getName() ?> </li>
<?php $count++;
if($count== 10) break; ?>
<?php endforeach; ?>
It pulls back the categories correctly, but there are categories that I need to filter out and I can't figure out how to do that? Any help would be GREATLY appreciated?
One way to do this, is to filter by id's:
<?php
foreach($categories as $k => $_category_id):
if($_category_id == $idToFilter):
continue;
else:
// show the category etc.
endif;
endforeach;
?>
I need to create a "Today's Daily News" page for an internal company 'newspaper'. It is run on a wordpress, so the not-so-savvy can handle it. The page just needs to display a tree-ish view of the categories with ONLY TODAY's posts listed in them. If there are no news items in any of the categories for today, then they should not be displayed. I have found some useful bits of code which I can patch together to achieve this, but really I want to utilize as much of wordpress' built in functions as possible. This is the format I am looking for:
Today's Daily News
-Category 1
---News Item Title 1A
---News Item Title 1B
---News Item Title 1C
---News Item Title 1D
-Category 2
---News Item Title 2A
---News Item Title 2B
You can get your custom post query and ordering using either one of the following functions/techniques:
The query_posts function
Custom queries using filter methods to sort/order posts
After some research, I figured out how to get this to work. I create a page template called "Today's Post" and you can see the source below:
<?php
/*
Template Name: Today's Posts
*/
?>
<?php get_header(); ?>
<ul>
<?php
$today = getdate();
$categories = get_categories("orderby=name&parent=0");
foreach ($categories as $category) {
query_posts('year='.$today["year"].'&monthnum='.$today["mon"].'&day='.$today["mday"].'&post_type=post&post_status=publish&cat='.$category->term_id);
echo " <li>\n"
." ".$category->name."\n"
." <ul>\n";
while (have_posts()) : the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo " </ul>\n"
." </li>\n";
}
?>
</ul>
<?php get_footer(); ?>
To show child categories, remove "&parent=0" and likewise change it to a category ID if you just want to display that categories children.
I want to hide this specific in the blog and other page I have this code :
<?php if (is_page(array ('2','4','6','8','10','12'))) : ?>
<?php else : ?>
<?php include('stats.php');?>
<?php endif; ?>
it won't show on 2,6,8,10,12 which are the pages but it shoes in 4 which is the blog please help.
Assuming 'blog' is a category, and it's ID is 4 and you don't want to include 'stats.php' when viewing that category's archive page:
<?php if (!is_page(array('2','4','6','8','10','12')) && !is_category('4')) {
include('stats.php');
} ?>
Otherwise, refer to the documentation for Wordpress conditional tags.