Display List of subcategories and links in WordPress - wordpress-theming

I'm trying to display the subcategorise for X category page.
and i'm using this code:
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<h3>Products</h3>';
echo '<ul>'.$this_category.'</ul>';
}
}
?>
but is not showing good and not showing the post link under the selected category .
I want to display this list with button for each one.

Related

In Magento 2.1.5, is there a way to render a widget within a template by its ID?

I would like to render a widget in my template by its ID, just like I have with blocks:
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_name')->toHtml();
Try this. This will help you.
For Display Type = All Product Widget
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("all_products")->setProductsCount("5")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>
For Display Type = New Product Widget
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("new_products")->setProductsCount("5")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>

I want to display individual categories (wordpress) on page, but be able to fully style each category title

I am nearing the end of a project and one of the last things I need to do is take some static html divs and enter category links into them.
The thumbnails have transitions styled into them, so when a user hovers over, the category title will appear over the top of a css mask effect.
http://lukwebdesign.co.uk/projects/chinwe-roy/
The thumbnails are towards the bottom of the home page, so any suggestions as to what to look at would be much appreciated.
TIA.
So there are a few ways to do this(especially depending on your setup) so I'm gonna have to make some assumptions on your site.
I'm assuming you're using default Wordpress posts and categories and not custom post types.
<?php
//Get the terms in the taxonomy "category" and assign it to the variable $terms
$terms = get_terms ('category');
//Use a foreach loop to loop through the terms
//i.e. for each term (e.g. workshop, exhibitions etc) do something
foreach ($terms as $term) {
//html for each category goes here
//e.g. display thumnail/featured image/display category title/link
}
?>
So for example within your foreach loop you could out each category url like so:
<a href="<?php echo site_url() . '/' . $term -> taxonomy . '/' . $term -> slug;?>">
<?php echo $term -> name; ?>
</a>
So for each category you can output a div and style it accordingly.

Manually setting wordpress categories for fontface Icons

I'm editing the twenty eleven theme but I want to change the categories so they display the icons as a #fontface icon instead of a word.
The categories needs to look something like this (using an art category as an example):
<i class="icon-brush"></i>
However this is how it currently looks.
<span class="entry-utility-prep entry-utility-prep-cat-links">Posted in</span>Art
Here is what my code is at the moment:
<?php $show_sep = false; ?>
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );
if ( $categories_list ):
?>
<span class="cat-links">
<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
$show_sep = true; ?>
</span>
<?php endif;?>
<?php endif; // End if categories ?>
The class needs to change depending on what post category is set is there a way to manually set what icon links to which category?
You can view my live template here http://mhutchinson.me.uk/
Thanks for your help!
Megan
You might wanna try something like this:
(change the printed HTML inside the foreach to fit your needs)
<?php
foreach((get_the_category()) as $category) {
echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}
?>
Cheers

how to fetch next row when given row is hidden using pagination?

It rather hard for me to come up with a short form of my question so bear with me:
Using jQuery and AJAX, I have all posts stored in my MySQL database displaying on index.php. Each post has a button the lets the user hide that post's DIV.
Now, I want to make it so that I only see 10 results and paginate the rest. I know there are plenty of pagination plugins out there but none of them displays the next post when the user hides one of the 10 already shown posts.
So if I see Posts 1-10 on page 1 (posts 11-20 on page 2) and user hides post 8, I should see Posts 1,2,3,4,5,6,7,9,10,11 on page 1 with posts 12-21 on page 2 and so on.
Question is: How do I do that? Here's what I have right now (fetch posts from database, display posts on page + hide button toggling):
// Javascript
$(document).ready(function() {
$(document).on("click", ".hide", function(){
postID = $(this).attr('id').replace('hide_', '');
$('#post-' + postID).toggle();
return false;
});
});
// PHP
<?php
$pageposts = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts"), OBJECT);
?>
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div id="buttons_<?php the_ID(); ?>">
<div id="hide_<?php the_ID(); ?>" class="hide" style="position:absolute; right: 2.5em;">
<a id="hidebtn_<?php the_ID(); ?>" href="#"><span></span></a>
</div>
</div>
<div id="post-<?php the_ID(); ?>">
/* display post content */
</div>
<?php endforeach; ?>
<?php endif; ?>
An easy way to figure out what would be the next ID to grab would be to store the MAX id on the page, so if your page displays 1-10, 10 would be stored as the MAX id.
When a user hides a post, the mysql query would pull 1 result where id > the stored max id.

Wordpress widget how to only display sub-categories based on selected Parent Category?

I was wondering if anybody know hows to modify the existing category widget to only display the categories within the selected parent category.
Example:
If my categories are structured like:
Computers
Laptops
Desktops
Software
Electronics
Cameras
Audio/Video
If somebody is viewing posts in the Computers category I would like the categories widget in the side bar to only display Laptops, Desktops & Software..
Is there a way to get this done? Is anybody familiar of a plugin that maybe do that?
Thanks!
how about using something like this?
on a singles page you could add a call from within the single.php page to a new sidebar or an include file...?
ie:
<?php if( is_single() ) { include(TEMPLATEPATH.'/newsidebar.php'); } ?>
newsidebar.php
<ul>
<?php
$catsy = get_the_category();
$myCat = $catsy->cat_ID;
wp_list_categories('orderby=id&child_of='.$myCat);
?>
</ul>
this will show only categories from the currently used category?
ie:
if current category is 5 // Computers
then all that will be shown in the list is
* Laptops
* Desktops
* Software
Thanks for your help. I was able to get it to work by doing this...
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<h3>Products</h3>';
echo '<ul>'.$this_category.'</ul>';
}
}
?>
1) To Show only Subcategories:
<?php
// check if the page is viewed is a category page.
if (is_category())
{
$cur_cat = get_query_var('cat');
if ($cur_cat)
{
$new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&&show_count=1&hide_empty=0');
echo '<ul>' . $new_cats . '</ul>';
}
}
?>
2) To Show All Main Categories:
<?php
wp_list_categories('depth=1&title_li=&exclude=1&show_count=1&hide_empty=0');
?>
3) To show All Categories + Subcategories like Tree menu:
Use FoCal plugin.
4) view this code too:
http://wpworks.wordpress.com/2011/01/13/displaying-categories-and-subcategories-tree-on-wordpress/