PHP WordPress - exclude by category description - php

This is my footer. There is parade of categories with most articles in. I need to exclude here categories with description that starts with "XXX".
So If some categories have description that starts with "XXX", it may donĀ“t show here.
Is it possible please? Im newbie in PHP so I dont know if can I declare category discreption here.
<?php global $teo_options;?>
<footer role="contentinfo">
<?php if(isset($teo_options['enable_popular_companies']) && $teo_options['enable_popular_companies'] == 1) { ?>
<div class="stripe-regular">
<div class="row">
<div class="column">
<h2><?php _e('Name', 'Couponize');?></h2>
</div>
</div>
<div class="row collapse">
<div class="column">
<div class="popular-companies flexslider">
<ul class="rr slides">
<?php
$args['hide_empty'] = 1;
$args['orderby'] = 'count';
$args['order'] = 'desc';
if(isset($teo_options['blog_category']) && $teo_options['blog_category'] != '')
$args['exclude'] = implode(",", $teo_options['blog_category']);
$categories = get_categories($args);
foreach($categories as $category) {
$image = get_option('taxonomy_' . $category->cat_ID);
$image = $image['custom_term_meta'];
?>
<li>
<a href="<?php echo get_category_link( $category->term_id );?>" class="wrapper-5 small">
<img src="<?php echo aq_resize($image, 130, 130, true); ?>" alt="<?php echo $category->name;?> coupons">
</a>
</li>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
<?php } ?>

Everything is possible but you're just complicating your problem.
Why would you identify a category by a piece of text in the Description?
Also, searching in the description as it's text could end to be a slow and unnecessary query, if you have a lot of categories.
To solve it, I recommend you take a look at the documentation about Including & Excluding Categories.
What I would do is to make sub-categories and either hide them manually or do a trick between the child and parent categories.

Related

Displaying categories?

Such a problem in WordPress, I want to display on the page information about categories, as well as their image ... I added the image in the category using the ACF plugin, all data is displayed: name, description, but the picture is not, here is my code, what's wrong tell me?
<?php
$categories = get_categories(array('hide_empty' => 0, 'taxonomy' => 'authors-category'));
foreach ($categories as $cat) {
$id = $cat->cat_ID;
$term = get_queried_object();
$cat_img = get_field('category_image', $cat);
?>
<!--Author-->
<div class="author-block">
<div class="photo-author" style="background: url(<?php echo $cat_img['url']; ?>) #f3b458 no-repeat center center;background-size: cover;"></div>
<div class="name-author"><?php echo $cat->name ?></div>
<div class="count-post-author">
41 post </br>
See All Posts By The Author
</div>
<div class="content-author">
<?php echo $cat->description ?>
</div>
</div>
<!--End Block-->
<?php } ?>
This is what happened, only in place of the figure should be pictures:
Solved the problem by switching the settings in the plugin
Array to URL
enter image description here

Wordpress: How to get the link of a particular category

Okay, on my blog I have four categories that the user can click to. Management, Industry News, Productivity etc.
Here: http://imgur.com/a/wHqqc
Requirement: I need to find a way using php to link to each category page.
<div class="categories-section">
<div class="category">
<?php
$categories = get_categories();
foreach ($categories as $cat) {
if($cat->cat_name = 'MANAGEMENT') {
$category_link = get_category_link($cat->cat_ID);
}
}
?>
<a href="#"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-1.jpg">
<h3> INDUSTRY NEWS</h3></a>
</div>
<div class="category">
<a href="<?php echo $category_link; ?>"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-2.jpg">
<h3> MANAGEMENT</h3></a>
</div>
<div class="category">
<a href="http://localhost/wordpress/category/PRODUCTIVITY/"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-1.jpg">
<h3> PRODUCTIVITY</h3></a>
</div>
<div class="category">
<a href="http://localhost/wordpress/category/PERSONAL-DEVELOPEMENT/"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-2.jpg">
<h3> PERSONAL DEVELOPEMENT</h3></a>
</div>
</div>
Problem: The page css is breaking and it's not working, currently the only way I can to link to category is to hard code it.
Ideas?
You are missing an equal sign (=) in the if-condition in your foreach.
if ($cat->cat_name == 'MANAGEMENT') {
$category_link = get_category_link($cat->cat_ID);
break;
}
You should also break after the result has been found so you don't loop over the other categories.
Update:
I'm not sure if there is a better function in Wordpress to do this, but you could save all links in an associative array to get all links at once.
$wp_categories = get_categories();
$categories = [];
foreach ($wp_categories as $cat)
$categories[$cat->cat_name] = get_category_link($cat->cat_ID);
Now you can do the following:
// Management link:
echo $categories['MANAGEMENT'];

How to get product category URL Key?

How can I get the product category's URL Key?
I can get the category name by $category->getName() but it does not work if I use this $category->getURLKey(),
$categories = $_product->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categories as $category) {
$productCategoryName = $category->getName();
var_dump($category->getURLKey());
}
Return null
Any ideas?
I'm not entirely sure what your goal is, but you're not going to be able to get category information from $_product. Feel free to ask questions based upon my code below. But my code below will grab the active categories children and their info. However, this should be a good enough base for what you're looking to do regardless.
<!-- Use this section if you're trying to grab children categories and their info -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="subcategories">
<p>Select a category to view products:</p>
<ul class="clearfix">
<!-- Display Each Subcategory Image and Name Feel Free to remove the img src section to remove the image -->
<?php foreach ($categories as $category): ?>
<li class="grid12-3">
<a href="<?php echo $category->getUrl() ?>" class="clearfix">
<?php if($thumbFile = $category->getThumbnail()): ?>
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<?php endif;?>
<span><?php echo $category->getName() ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- End -->
Any questions feel free to ask! Sorry, I just stole an example from a project so the HTML may not line up for a direct copy.
foreach($categories as $category) {
// for category URL key
var_dump($category->getUrlPath());
// for category URL
var_dump($category->getUrl());
}

Filter by 2 different category value

I want a field to show if it has a category of either 2 values from 2 different sub fields.
Im not quite sure how to do this though with 2 different subfields.
Here is an exmaple im trying to demo to show how I would like it to work:
<?php while(has_sub_field('team_profile')):
$category = get_sub_field('category');
$category_2 = get_sub_field('category_2');
if($category=='copyclearance')
if($category_2=='anothercat'){ ?>
<li class="col-lg-2 teamProfile">
<img src="<?php the_sub_field('profile_image'); ?>" class="img-responsive"/>
<h2><?php the_sub_field('profile_name'); ?></h2>
<p class="jobTitle"><?php the_sub_field('job_title'); ?></p>
</li>
<?php
}
endwhile; ?>
This is my working version with just one category filter (but I need 2):
<?php while(has_sub_field('team_profile')):
$category = get_sub_field('category');
if ($category=='copyclearance') { ?>
<li class="col-lg-2 teamProfile">
<img src="<?php the_sub_field('profile_image'); ?>" class="img-responsive"/>
<h2><?php the_sub_field('profile_name'); ?></h2>
<p class="jobTitle"><?php the_sub_field('job_title'); ?></p>
</li>
<?php
}
endwhile; ?>
So this would work, by looking to see if anything has been posted in either $category or in $category_2 then displaying all of them results. NOT using the 2 to filter specifically i.e only showing posts that are in both $category and $category_2
Instead of nesting your if statements, use the or operator:
if($category=='copyclearance' || $category=='anothercat') {
// ...
}
If you want to do more than two, I recommend using an array:
$valid_cats = array('copyclearance', 'cat2', 'cat3', 'etc');
if(in_array($category, $valid_cats)) {
// ...
}

Child page content

I have a parent page that acts as menu for my portfolio.
It pulls in thumbnail images from the child pages which i have been able to accomplish with magic fields and some code. It dumps the images into a grid layout. The thumbnails are pulled into one container div like so:
div id="folio-content">
<div class="thumb-container">
<div class="thumb"><img src="/images/pic.jpg"/>
</div>JCPenny</div>
... </div>`
when the div gets filled up with 2 thumbnails I want to create a new container div and fill it with 2 images again and so on after 2 images.
So, if you had 4 images it would look like this.
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
this is the code I am using in my page.php file.
<?php get_header(); ?>
<div id="folio-content">
<?php
$projectpage = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
$count = 0;
foreach($projectpage as $page)
{
$content = $page->post_content;
if(!$content)
continue;
if ($count == 10) --- this is geting 10 images now, but I want to get them all.
break;
$count++;
$content = apply_filters('the_content', $content);
?>
<div class="thumb-container">
<div class="thumb"><a href="<?php echo get_permalink($page->ID); ?>"<?php echo get_image ("thumbnail",1,1,1,$page->ID);?></a>
</div><?php echo $page->post_title ?>
</div>
<?php
}
?>
</div><!--/close set!-->
</div>
also, how can I get ALL child pages? I have it set to 10 now with this if ($count == 10)
any help? thanks a ton again!!!!
I'm not familiar with "get_pages" but since Wordpress treats posts and pages in an identical manner you could use this.
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
The -1 removes the limit and gets ALL the specified pages.
I have cobbled together some code, that sort of sounds right but does not work at all! Which I am not surprised. But it is a starting point - please take a look at this code, maybe it is step in the right direction?
<?php
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
if (have_posts()) :
$i=0; // counter
while(get_posts()) : the_post();
if($i%2==0) { // if counter is multiple of 3, put an opening div ?>
<!-- <?php echo ($i+1).'-'; echo ($i+2); ?> -->
<div>
<?php } ?>
<div class="single_item">
<h2><?php the_title(); ?></h2>
</div>
<?php $i++;
if($i%2==0) { // if counter is multiple of 3, put an closing div ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php
if($i%2!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>
<?php endif; ?>

Categories