Word "Array" is Appearing on Blog Posts Page - php

I'm getting the word "Array" coming up on my blog POSTS page when I use the below code. I have a feeling that it relates to this line:
I am using a similar template for my PORTFOLIO and NEWS pages where the wording is slightly different on this line ("showportcat" and "shownewscat" instead of "get_the_category") and it should be displaying the categories but instead, the word 'array' is in it's place.
I've tried "showpostcat" but that didnt' work so I wonder if I need to be re-wording it? Or maybe the problem is on another part of the code which I've included below?
<div class="greyblock">
<h4 class="nomar"><?php echo the_title(); ?></h4>
<div class="sep"></div>
<?php echo get_the_category(get_the_ID()); ?>
<div class="clear"></div>
<ul class="blogpost_list columns2">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => get_option("posts_per_page"),
);
if (isset($_GET['slug'])) {
$args['tax_query']=array(
array(
'taxonomy' => 'postcat',
'field' => 'slug',
'terms' => $_GET['slug']
)
);
}
$wp_query->query($args);
?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();
#We have:
#get_permalink() - Full url to post;
#get_the_title() - Post title;
#get_the_content() - Post text;
#get_post_time('U', true) - unix timestamp
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo "
<li class='pc'>
<img alt='".get_the_title()."' src='".TIMTHUMBURL."?w=400&h=400&src=".$featured_image[0]."'>
<h4>".get_the_title()."</h4>";
$terms = get_the_terms($post->ID, 'postcat');
if ( $terms && ! is_wp_error( $terms ) ) {
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
}
echo "
<p>".get_the_excerpt()."</p>
<a href='".get_permalink()."' class='read'>Read more</a>
<br class='clear' />
</li>
";
endwhile; ?>
</ul>
</div>
<?php get_pagination() ?>
<?php $wp_query = null; $wp_query = $temp; ?>

How about:
$categories = get_the_category(get_the_ID());
foreach ($categories as $category) {
echo $category;
}

The problem is still there. I'm guessing it relates to the way I am wording one of the below two lines as it is 'showportcat' for portfolios and 'shownewscat' for news but 'showpostcat' doesn't work for posts so I just can't figure it out:
<?php echo get_the_category(get_the_ID()); ?>
**OR**
'taxonomy' => 'postcat',

Related

How to fix: custom categories navigation shows duplicate items

I have a category menu that filters the posts shown in the page. My problem is that instead of having an item per category in the menu, I see duplicates of the same Item. It seems like I have as much duplicates as the number of the posts under that category.
Link to the page where the navigation is: http://www.confesercenti.pistoia.it/connessioni/
You can see it under "Filtra le aziende per settore economico". For example, you will notice "COMUNICAZIONE" is repeated five times.
This is the code:
<div id="filters" class="settore_dropdown_wrapper button-group">
<p>Filtra le aziende per settore economico</p>
<ul id="port-list" class="port-filter">
<li><a class="active" data-filter="*">Tutti</a></li>
<?php
$main_convenzione = new WP_Query(array(
'post_type' => 'azienda',
'meta_key' => 'convenzioni_attive',
'meta_value' => 1
));
if ($main_convenzione->have_posts()) : while($main_convenzione->have_posts()) : $main_convenzione->the_post();
// while($main->have_posts()) : $main->the_post();
global $post;
$post_id = $post->ID;
// $terms_list = get_the_terms( get_the_ID(), 'settore' );
$terms_list = get_the_terms( $post_id, 'settore');
// $args = array(
// 'post_type' => 'azienda', // filter by the post type progetto
// 'taxonomy' => 'settore',
// 'hide_empty' => true
// );
// $terms = get_terms($args); // Get all terms of a taxonomy
foreach ( $terms_list as $term_single ) {
$term_name = $term_single->name;
$term_slug = $term_single->slug; ?>
<li><a data-filter=".<?php echo strtolower(preg_replace('/[^a-zA-Z]+/', '-', $term_name)); ?>">
<?php echo esc_attr($term_name); ?></a></li>
<?php }
endwhile; endif; wp_reset_postdata(); ?>
</ul><!--port-list-->
</div><!--filters-->
As stated above it seems like it shows as much duplicates as the number of the posts under that category. My goal is to have one item per category instead of having a al those duplicates
You need to save the filters you need instead of showing them directly. You can use in_array to check if a value is in your array. Something like would work:
<div id="filters" class="settore_dropdown_wrapper button-group">
<p>Filtra le aziende per settore economico</p>
<ul id="port-list" class="port-filter">
<li><a class="active" data-filter="*">Tutti</a></li>
<?php
$main_convenzione = new WP_Query(array(
'post_type' => 'azienda',
'meta_key' => 'convenzioni_attive',
'meta_value' => 1
));
if ($main_convenzione->have_posts()) : while($main_convenzione->have_posts()) : $main_convenzione->the_post();
global $post;
$terms_list = get_the_terms( $post->ID, 'settore');
$terms = [];
foreach ( $terms_list as $term_single ) {
if(!in_array($term_single->name, $terms)) {
$terms[] = $term_single->name;
};
};
endif;
?>
<?php foreach($terms as $term): ?>
<li><a data-filter=".<?php echo strtolower(preg_replace('/[^a-zA-Z]+/', '-', $term)); ?>"><?php echo esc_attr($term); ?></a></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul><!--port-list-->
</div><!--filters-->

Get the current post category name inside while loop

I created a custom post type "stm_media_gallery"
And three category inside this custom post type.
I want to display category name associated with each post.
<?php $gallery_query = new WP_Query( array('post_type' =>
'stm_media_gallery', 'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post(); ?>
--Display post name and its category name
<?php endif; ?>
<?php endwhile; ?>
You just need to put following code inside loop :
<div>
<?php
foreach((get_the_category()) as $category){
echo $category->name."<br>";
echo category_description($category);
}
?>
</div>
Update in existing code
<?php $gallery_query = new WP_Query(
array('post_type' => 'stm_media_gallery',
'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post();
$gallery_category = get_the_category( get_the_ID() );
the_title( '<h3>', '</h3>' );
echo "<br>";
<?php foreach ( $gallery_category as $key => $value) { echo $value->category_nicename; } ?>
<?php endif; ?>
<?php endwhile; ?>
You can use the pre-made WordPress function the_category( $separator, $parents, $post_id ) to print the post categories as links.
Further information on the WordPress Codex: Function Reference: the_category
Edit: Print only the names:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories->name );
}
Put this inside While Loop
global $post;
$postcat = get_the_category( $post->ID );

How to display current posts custom taxonomy name inside wordpress loop?

I am currently building a Wordpress site and I am encountering some difficulty with the following..
I am trying to dynamically add a class to a HTML element by displaying the custom taxonomy name of the current post type, to use as the class name. This is all being done within a Foreach loop.
My code is as follows
<?php
$args = array( 'posts_per_page' => -1, 'post_type' => 'staff', 'orderby' => 'menu_order',
'order' => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php $terms = wp_get_post_terms( $post_ID, 'department' ); ?>
<?php global $post; $terms = wp_get_post_terms( $post->ID, 'department'); ?>
<div class="grid-item <?php echo $term->slug; ?> ">
<div class="staff-box">
<?php the_post_thumbnail('staff-member'); ?>
<a href="<?php echo the_permalink(); ?>">
<p class="staff-title"><?php the_title(); ?></p>
<p class="staff-job-title"><?php the_field('staff-job-title'); ?></p>
</a>
</div>
</div>
<?php endforeach;
wp_reset_postdata();?>
This is working using slug; ?> to display the class name however it is only displaying "veterinary-surgeons" on every single class name, when it should be displaying the relevant department on each item...
Hope that makes sense.
Many thanks.
To anyone who is interested I have now solved this using:
<?php $term_list = wp_get_post_terms($post->ID, 'department', array("fields" => "all")); ?>
and by using
<?php echo $term_list[0]->slug ; ?>
as class name.
Thanks
You can solve this problem by this code also,
Put this code inside while loop, 'portfolio_category' is custom taxonomy name
$terms = get_the_terms( $post->ID, 'portfolio_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;

Wordpress Product-Filter with CPT & Custom Taxonomy

I'm building a Site with Wordpress link to site It's a Site with Custom Post Types (product) and Custom Post Taxonomies (product_category).
On the Category page I have to filter the products by Subcategories.
I found the filterable.js with a tutorial.
My Problem: If I click on any subcategory (Filter-Item) the browser (Chrome and Filezilla) doesn't work anymore.
If I click on the filter the browser says "processing request"
after a long loading time the browser gives me an error.
Here's a screenshot of the Browser error:
http://farbstrakt.com/screenshot.png
I think the problem is about how I change the URL.
I'm searching days for any solution but nothing.
I can't find the error
here's my taxonomy-product_category.php
<div class="container middler" id="demo">
<ul class="filter-wrapper clearfix" id="portfolio-filter">
<?php
$args = array(
'show_count' => false,
'child_of' => get_queried_object()->term_id,
'taxonomy' => 'product_category',
'title_li' =>'',
);
$terms = get_categories($args);
$count = count($terms);
if ( $count > 0 ){
echo '<li class="cl-effect-2"><div><span data-hover="Alle">Alle</span></div></li>';
foreach ( $terms as $term ) {
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
echo '<li class="cl-effect-2">
<div>
<a href="#'.$termname.'">
<span data-hover="'.$term->name.'">'.$term->name.'</span>
</a>
</div>
</li>';
}
}
?>
</ul>
<?php
$loop = new WP_Query(array('post_type' => 'product', 'posts_per_page' => -1));
$count =0;
?>
<div id="portfolio-wrapper">
<ul class="grid effect-2 item-wrapper clearfix filter-all" id="grid">
<?php if ( $loop ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'product_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif;
?>
<?php $infos = get_post_custom_values('_url'); ?>
<li class="<?php echo strtolower($tax); ?> all ">
<div>
<figure>
<?php the_post_thumbnail( array(400, 160) ); ?>
<figcaption><?php the_title(); ?></figcpation>
</figure>
</div>
</li>
<?php endwhile; else: ?>
<p class="no-products">NO products
</p>
</ul>
<?php endif; ?>
and thats the short snippet from the code above where i think there must be the error.
<?php
$terms = get_the_terms( $post->ID, 'product_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif;
?>
<?php $infos = get_post_custom_values('_url'); ?>
<li class="<?php echo strtolower($tax); ?> all ">
Would be grateful if you could help me. I have searched a lot and didn't found any solution. I'm a PHP-Newbie. Of course I could use another filter plugin but I want to figure out where's the problem
Thanks
The issue is appending a url with #all means that the browser will look for a element with a id of all . It cant find it on your page, because no element exists with this id.Hence the browser crashes after a few links have been clicked on.
just for reference, a element with a id looks like this:
<div id="someelement" class="not the same thing as id" >content</div>
<div id="anotherelement" class="someelement" >content</div>
In this case, if you append the url of the page with this element on it with #someelement the browser will focus on the first element. This does not look for elements with the class 'someelement'.
If you want to pass query args, you need to use the ?products=all&etc format. But what i think you want to do is actually redirect to a page displaying only the selected term? use /term instead in your href.
Also you are getting a 404 on product_category and any of the terms you had on the page. Part of the issue is you are using $term->name instead of $term->slug which may be different. Did you reset the permalinks when you created the taxonomy?
Also check the settings as described here:
https://codex.wordpress.org/Function_Reference/register_taxonomy
By default WP will create a loop on taxonomy-term.php and archive pages, the loop you run is a second db request and loop so thats extra time added to your server response time. Use the pre_get_posts hook
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Wordpress order posts by post type

I am trying to create a system which fetches post from 2 post types and i want to display the posts order by their post type. I am trying to first show the posts from one post type and then another post type but the query i used mixes it all. Can any one suggest a good solution for this. Here is my code.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array('review','directory'),
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => '4',
'paged' => $paged ,
'tax_query' => array( array(
'taxonomy' => 'Reviews',
'field' => 'id',
'terms' => $cat_id
), ),
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
$product_terms = wp_get_post_terms(get_the_ID(), 'Reviews', array("fields" => "all", "order" => "DESC"));
$postype=get_post_type( get_the_ID() );
if($postype=='review')
{
?>
<div class="review-post">
<h3><?php the_title(); ?></h3>
<div class="review-cat-new">
<?php echo get_the_term_list( $post->ID, 'Reviews', 'Category: ', ', ', '' ); ?>
</div>
<?php
if(get_field('see_it'))
$seeit_text= get_field('see_it');
if(get_field('skip_it'))
$skipit_text= get_field('skip_it');
?>
<div class="see-skip">
<p class="see-it"><span>See it:</span>
<?php echo $seeit_text; ?>
</p>
<p class="skip-it"><span>Skip it:</span>
<?php echo $skipit_text; ?>
</p>
</div>
<?php echo custom_field_excerpt(); ?>
</div>
<?php }
else
{
?>
<div class="review-post">
<h3><?php the_title(); ?></h3>
<div class="review-cat-new">
<?php echo get_the_term_list( get_the_ID(), 'Reviews', 'Category: ', ', ', '' ); ?>
</div>
<?php echo the_field('enter_content_direc'); ?>
<?php
if(get_field('enter_textdirec'))
$text= get_field('enter_textdirec');
if(get_field('enter_linkdirec'))
$textlink= get_field('enter_linkdirec');
?>
<div class="see-skip">
<p class="see-it direc"><span><a target="_blank" style="color:#5D6D71; text-transform: lowercase;" href="<?php echo $textlink;?>"><?php echo $textlink;?> </a></span>
</p>
</div>
</div>
<?php }
endwhile;
echo '<div class="paging">';
wp_pagenavi();
echo '</div>';
endif;
So is there a way so that i can first show the posts form reviews and then directory?
Since Wordpress 4.0 you can just pass the option type (post_type) to the orderby parameter in the WP_Query object.
orderby (string | array) - Sort retrieved posts by parameter. To order by post type just pass:
'type' - Order by post type (available since Version 4.0). ('post_type' is also accepted.)
As seen in the docs https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters , you can use the regular wp query by passing an array to 'orderby' :
$wp_query = new WP_Query(
array(
's' => $search,
'orderby'=> array('type'=>'DESC', 'title'=>'ASC'),
'paged'=>$paged
)
);
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// do your loop stufff here
}
}
Finally i got the solution and it can be done using a filter.
The solution looks like this.
add_filter( 'posts_request' , 'modify_request' );
function modify_request( $query) {
global $wpdb;
if(strstr($query,"post_type IN ('review', 'directory')")){
$where = str_replace("ORDER BY {$wpdb->posts}.post_date","ORDER BY {$wpdb->posts}.post_type",$query);
}
return $where;
}
Order per post types with custom post type order:
add_filter('pre_get_posts', function ($query) {
if ($query->is_search && !is_admin()) :
$query->set('post_type', [ 'cpt-1', 'cpt-2', 'post', 'cpt-3']);
endif;
return $query;
});
add_filter('posts_orderby', function ( $order ) {
if ( ! is_admin() ) :
if ( is_search() && is_main_query() ) :
global $wpdb;
$order = "FIELD( post_type, 'cpt-1', 'cpt-2', 'post' ), {$wpdb->posts}.post_modified DESC";
endif;
// Disable this filter for future queries!
remove_filter( current_filter(), __FUNCTION__);
endif;
return $order;
});
First solution which comes to my mind is to get posts only for first category. Reset query (wp_reset_query()) and get them again for second category. You need to double up your code of course, but I don't see any other solution - it's WordPress - you're very limited.
Second solution is to query database directly like this:
$result = mysql_query('SELECT *
FROM `wp_posts`
WHERE post_type = "page"
OR post_type = "post"
ORDER BY post_type ASC , post_date DESC ');
$posts = array();
if($result) {
while ($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
}
mysql_free_result($result);
echo('<pre>');
var_dump($posts);
echo('</pre>');
die();
Notice that I've used post and page types as these are default, and exist in my WP installation. But you can use different. You can play with this query as much as you want.

Categories