I have created a function that allows me to display the next/previous category, by order of their position. This is working fine, the problem I am having is getting ACF fields to display specific to each category.
I currently have a colour picker ACF field for every category - which is different for each one, this is set to display on each link. At the moment, the ACF field is only bringing in the hex value from the current category that is being viewed, as opposed to the next/previous ones.
function next_prev_cat() {
$this_taxonomy = get_queried_object();
if (is_category()) {
$taxonomies = get_categories();
}
if (is_tag()) {
$taxonomies = get_tags();
}
foreach ($taxonomies as $position => $tax):
if ($this_taxonomy->term_id == $tax->term_id):
$next_tax = $position + 1;
$prev_tax = $position - 1;
break;
endif;
endforeach;
$showPrev;
$showNext;
if ($prev_tax < 0) {
$prev_tax=count($taxonomies) - 1;
$showPrev='';
} else {
$prev_tax;
$showPrev = $taxonomies[$prev_tax]->name;
}
if ($prev_tax == count($taxonomies)-2) {
$next_tax = 0;
$showNext = '';
} else {
$showNext = $taxonomies[$next_tax]->name;
}
$prevLink = get_term_link( $taxonomies[$prev_tax] );
$nextLink = get_term_link( $taxonomies[$next_tax] );
?>
<div class="area-links">
<a style="border:1px solid <?php the_field('brand_colour', $tax); ?>" href="<?php echo $prevLink;?>"><?php echo $showPrev; ?></a>
<a style="border:1px solid <?php the_field('brand_colour', $tax); ?>"href="<?php echo $nextLink;?>"><?php echo $showNext ?></a>
</div>
<?php
}
Here is updated version of code
function next_prev_cat() {
$this_taxonomy = get_queried_object();
$id_for_color=false;
if (is_category()) {
$taxonomies = get_categories();
}
if (is_tag()) {
$taxonomies = get_tags();
}
$colors=array();
foreach ($taxonomies as $position => $tax):
$id_for_color=$tax->term_id;
$colors[$id_for_color]=get_field('brand_colour', $id_for_color);
$cats=array_push($cats,$id_for_color );
if ($this_taxonomy->term_id == $tax->term_id):
$next_tax = $position + 1;
$prev_tax = $position - 1;
break;
endif;
endforeach;
$showPrev;
$showNext;
if ($prev_tax < 0) {
$prev_tax=count($taxonomies) - 1;
$showPrev='';
} else {
$prev_tax;
$prev_color=$colors[$taxonomies[$prev_tax]->term_id];
$showPrev = $taxonomies[$prev_tax]->name;
}
if ($prev_tax == count($taxonomies)-2) {
$next_tax = 0;
$showNext = '';
} else {
$next_color=$colors[$taxonomies[$next_tax]->term_id];
$showNext = $taxonomies[$next_tax]->name;
}
$prevLink = get_term_link( $taxonomies[$prev_tax] );
$nextLink = get_term_link( $taxonomies[$next_tax] );
?>
<div class="area-links">
<a style="border:1px solid <?php echo $prev_color; ?>" href="<?php echo $prevLink;?>"><?php echo $showPrev; ?></a>
<a style="border:1px solid <?php echo $next_color; ?>"href="<?php echo $nextLink;?>"><?php echo $showNext ?></a>
</div>
<?php}
Related
I am trying to show google ads in my website. I would like insert one at the top of the loop. 1st one and now more. It echo ad front every post, but I would like just show it once before the first one.
This is the code what I am using.
<div class="search-results">
<div class="row">
<?php
$counter_max = 4;
$column_size = 'col-sm-6 col-md-3';
if( $ads_search_layout == 'style-left' ){
$counter_max = 3;
$column_size = 'col-sm-6 col-md-4';
}
if( $view == 'list' ){
$counter_max = 2;
$column_size = 'col-md-6';
if( $ads_search_layout == 'style-left' ){
$counter_max = 1;
$column_size = 'col-md-12';
}
}
$counter = 0;
if( $ads->have_posts() ) {
while( $ads->have_posts() ){
$ads->the_post();
if( $counter == $counter_max ){
$counter = 0;
echo '</div><div class="row">';
}
?>
<div class="<?php echo esc_attr( $column_size ); ?>">
<?php
if(($counter>1) && ($counter == 0)){
echo( classifieds_load_path( 'includes/adsence.php' ) );
}
if( $view == 'grid' ){
$image_size = 'classifieds-ad-box-bug';
include( classifieds_load_path( 'includes/ad-box.php' ) );
}
else{
$excerpt_max = 154;
include( classifieds_load_path( 'includes/ad-box-alt.php' ) );
}
$counter++;
?>
</div>
<?php
}
}
?>
</div>
</div>
<?php wp_reset_postdata(); ?>
i've created a colophon for my website in which i post all the logos of the different sponsors i have. I add all the sponsors via custom post type. i also added a specific custom taxonomy to distinguish between the different typologies of sponsorships.
I use this code in the footer.php to display them:
<?php $terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { ?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $term->slug,
);
$pesca_post = new WP_Query ($arg);
$quanti_post = $pesca_post->post_count;
if(have_posts()){
while ($pesca_post->have_posts()) : $pesca_post->the_post();
$featured = get_the_post_thumbnail_url(get_the_ID(),'large');
if ($quanti_post == 5){
$classe_bootstrap = 15;
}elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
echo '<div class="col-md-' . $classe_bootstrap . '">';
if (isset($featured)){
$img = $featured;
}else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($post->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
<?php echo '</div>';
endwhile;
}?>
</div>
<?php }
}?>
my problem is that this code is completely working just on some pages, on other it shows the contents avoiding the ones belonging to the first term, no matter which it will be.
I have noticed that it works in pagaes where i use other queries.
What am i doing wrong?
i changed it in this way and now it's working!
$terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { //per ogni termine presente
$nome = $term->slug;?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $nome,
);
$elementi = get_posts($arg);
$quanti_post = count( $elementi );
if ($quanti_post == 5){
$classe_bootstrap = 15;
}
elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}
elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
foreach($elementi as $elemento){
$featured = get_the_post_thumbnail_url($elemento->ID,'large');
if (isset($featured)){
$img = $featured;
}
else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<div class="col-md-<?php echo $classe_bootstrap; ?>">
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($elemento->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
</div>
<?php }?>
</div>
<?php }
}?>
hello I am new to programing. I am creating a simple RSS feed plugin for wordpress to upload some products to WooCommerce and need to upload the category to wordpress . In plugin the category is visible but they are not showing like the url link. I need the category to show like checkbox. Can anybody have any idea ?
File number 1
* Init feed with information from DB
private function load()
{
if ($this->id) {
$post = get_post( $this->id );
if (!$post) {
$this->id = null;
return;
}
$this->title = $post->post_title;
$this->id = $post->ID;
$meta = get_post_meta($post->ID);
foreach ($meta as $key=>$item) {
$newKey = substr($key, 1);
$this->properties[$newKey] = $item[0];
if ($newKey == 'post_category') {
$this->properties[$newKey] = unserialize($item[0]);
}
}
}
}
..................
$fields = array( 'post_category');
..................
// Create post
$post = array('post_category' => $this->post_category);
And the file number 2 have this
<div class="postbox">
<div class="handlediv" title="<?php esc_html_e('Click to toggle', 'rss-autopilot'); ?>"><br></div>
<h3 class="hndle ui-sortable-handle"><span><?php esc_html_e('Categories', 'rss-autopilot'); ?></span></h3>
<div class="inside">
<ul class="rssap-categories-list">
<?php wp_category_checklist( 0, 0, $feed->post_category, false, null, true ); ?>
</ul>
<div class="clear"></div>
</div>
</div>
Here is your code.
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul class="product-cats">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<input name="product_category[]" type="checkbox" value="'. $term->term_id.'"';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
I have an foreach to add terms to a post on Wordpress Which is working great. Code below:
<h3>
<?php foreach($terms as $term) {?>
<?php echo $term->name;?>
<?php } ?>
</h3>
However I need to add a counter so that if there is more than one term in the <h3> it adds a / between them. For instance:
<h3>Term Name</h3>
<h3>Term Name / Term Name / Term Name</h3>
This is the code I have so far however its not working.
<?php
$i = 1;
foreach($terms as $term) {
if($i == 1){
echo ' / '.$term->name;
} else {
echo $term->name;
}
$i++;
} ?>
You don't need to use a counter. Just put each $term->name in to an array, and implode it:
echo implode(' / ', array_map(function($term) { return $term->name; }, $terms));
Here's a demo
Please try below code.
<?php $terms = array_values($terms);
if( sizeof($terms) > 1){ ?>
<h3><?php echo implode(' / ', array_map(function($term) { return $term->name; }, $terms)); ?></h3>
<?php }else{ $term = $terms[0]; ?>
<h3>echo $term->name;</h3>
<?php } ?>
<?php
$i = 1;
foreach($terms as $term) {
if($i > 1) {
echo ' / '.$term->name;
} else {
echo $term->name;
$i++;
}
}
?>
Here is the working solution :)
<?php
$terms = array("Term One","Term Two","Term Three","Term Four");
$i = 1;
$result="";
foreach($terms as $term)
{
if($i==1)
{
$result=$result.$term.' ';
$i++;
}
else
{
$result=$result."/".' ';
$result=$result.$term.' ';
}
}
echo $result;
?>
I created list of category and sub category to display in my front page. When try to display all my category and subcategory are listing. I want only my main category to list. If anyone can help in this issue. Thank you
<span class="ad-category">
<?php
$category = get_the_category();
if ($category[0]->category_parent == 0) {
$tag = $category[0]->cat_ID;
$tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
if (isset($tag_extra_fields[$tag])) {
$category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
$category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
}
} else {
$tag = $category[0]->category_parent;
$tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
if (isset($tag_extra_fields[$tag])) {
$category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
$category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
}
}
if(!empty($category_icon_code)) {
?>
<div class="category-icon-box" style="background-color: <?php echo $category_icon_color; ?>;"><?php $category_icon = stripslashes($category_icon_code); echo $category_icon; ?></div>
<?php
}
$category_icon_code = "";
?>
</span>
Try This
<span class="ad-category">
<?php
$category = get_the_category();
if ($category[0]->category_parent == 1) {
$tag = $category[0]->category_parent;
$tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
if (isset($tag_extra_fields[$tag])) {
$category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
$category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
}
}
if(!empty($category_icon_code)) {
?>
<div class="category-icon-box" style="background-color: <?php echo $category_icon_color; ?>;"><?php $category_icon = stripslashes($category_icon_code); echo $category_icon; ?></div>
<?php
}
$category_icon_code = "";
?>
</span>