Wordpress - нужна помощь с PHP кодом

Robick
На сайте с 17.08.2007
Offline
173
701

Всем привет!

Есть такой код, прописан в шаблоне категории, он выводит количество записей с тегом "+"

			<?php  

$plus = get_terms(array(
'taxonomy' => 'post_tag', //i guess campaign_action is your taxonomy
'hide_empty' => false, 'parent'=>0
));
foreach ($plus as $plus)
{
if($plus->name == '+')
{
echo $plus->name;
echo $plus->count;
}
}
?>

Сейчас он считает количество записей с тегом '+' по всем категориям, задача, что бы считал только в текущей категории.

Подскажите плз, всю ночь бьюсь :mad:

M
На сайте с 04.10.2011
Offline
90
#1
Robick:
'parent'=>0

Вместо нуля подставьте ИД текущей категории и условие просто =

Сдается, обращаться скайп avdesk-it-kmm Верстка, кодинг - контакты в профиле... VPS от 5€ (https://gmhost.com.ua/?partner=10255)
Robick
На сайте с 17.08.2007
Offline
173
#2
mmkulikov:
Вместо нуля подставьте ИД текущей категории и условие просто =

не, не то (

D
На сайте с 02.02.2010
Offline
143
#3

 $tags_count = $wpdb->get_results($wpdb->prepare("

SELECT DISTINCT terms2.name as name, terms2.term_id as term_id, COUNT(terms2.term_id) as count, terms2.slug as slug
FROM
wp_posts as p1
LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
wp_posts as p2
LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
WHERE
t1.taxonomy = 'category'
AND p1.post_status = 'publish'
AND terms1.term_id = %s
AND t2.taxonomy = 'post_tag'
AND p2.post_status = 'publish'
AND p1.ID = p2.ID
GROUP BY terms2.term_id
ORDER BY count DESC
", get_query_var('cat')));

foreach($tags_count as $tag_count) {

echo $tag_count->name.' ('.$tag_count->count.')<br>';

}
Robick
На сайте с 17.08.2007
Offline
173
#4

divv,

а как задать в этом коде такие данные?

taxonomy => articles_category

post type => articles

tag slug => +

---------- Добавлено 03.03.2019 в 18:58 ----------

Вот полный код страницы

<?php 

/**
* Template Name: Главная
*/
get_header();
?>
<section class="section" style="padding-top: 180px;">
<div class="container">
<div class="title">
<h1>ТОП-Рейтинг ремонт квартир.</h1>
<div class="sub-title">Отзывы о компаниях Москвы 2018</div>
</div>
<style>
#hide {
display: block;
}
#show {
display: none;
}
</style>
<div id="hide" class="content">
<table class="table">
<thead class="table-head">
<tr class="table-head-row">
<th class="table-head-col">Рейтинг</th>
<th class="table-head-col">Логотип </th>
<th class="table-head-col">Название компании </th>
<th class="table-head-col">Голоса (+ | - )</th>
<th class="table-head-col">Голосовать </th>
<th class="table-head-col">Всего голосов</th>
<th class="table-head-col">Оставить отзыв</th>
</tr>
</thead>
<tbody class="table-body">
<?php

$taxonomy = 'articles_category';
$terms = get_terms($taxonomy, array( 'hide_empty' => 0, 'number' => '10', 'hierarchical' => 1, 'order' => 'count','orderby' => 'count'
));
if ( $terms && !is_wp_error( $terms ) ) :
?>
<?php $i = 1; foreach ( $terms as $term ) { ?>
<tr class="table-body-row">
<td class="table-body-col"><?php echo "$i"; ?> </td>
<td class="table-body-col"><img src="<?php the_field('company_logo', $term); ?>" style="max-height: 28px;"> </td>
<td class="table-body-col"> <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></td>
<td class="table-body-col">


<?php
$plus = get_terms(array(
'taxonomy' => 'post_tag', //i guess campaign_action is your taxonomy
'hide_empty' => false, 'parent'=>0
));
foreach ($plus as $plus)
{
if($plus->name == '+')
{
echo $plus->name;
echo $plus->count;
}
}
?>

| <?php
$plus = get_terms(array(
'taxonomy' => 'post_tag',//i guess campaign_action is your taxonomy
'hide_empty' => false, 'parent'=>0
));
foreach ($plus as $plus)
{
if($plus->name == '-')
{
echo $plus->name;
echo $plus->count;
}
}
?>
</td>
<td class="table-body-col"> <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>#rev" target="_blank" rel="noopener noreferrer"> <i class="icon like"></i></a><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>#rev" target="_blank" rel="noopener noreferrer"> <i class="icon dislike"></i></a></td>
<td class="table-body-col"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php printf( '%s', $term->count ); ?></a></td>
<td class="table-body-col"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>#rev" target="_blank" rel="noopener noreferrer"> <i class="icon email"></i></a></td>
</tr>
<?php $i++; } ?>
<?php endif;?>
</tbody>

</table>
<center><p class="showmore"><button class="btn btn-purple show-more" onclick="myFunction();myFunction2()">Показать все компании</button></p></center>
</div>
<div id="show" class="content">
<table class="table">
<thead class="table-head">
<tr class="table-head-row">
<th class="table-head-col">Рейтинг</th>
<th class="table-head-col">Логотип </th>
<th class="table-head-col">Название компании </th>
<th class="table-head-col">Голоса (+ | - )</th>
<th class="table-head-col">Голосовать </th>
<th class="table-head-col">Всего голосов</th>
<th class="table-head-col">Оставить отзыв</th>
</tr>
</thead>
<tbody class="table-body">
<?php

$taxonomy = 'articles_category';
$terms = get_terms($taxonomy, array( 'hide_empty' => 0, 'hierarchical' => 1, 'order' => 'count','orderby' => 'count'
));
if ( $terms && !is_wp_error( $terms ) ) :
?>
<?php $i = 1; foreach ( $terms as $term ) { ?>
<tr class="table-body-row">
<td class="table-body-col"><?php echo "$i"; ?> </td>
<td class="table-body-col"><img src="<?php the_field('company_logo', $term); ?>" style="max-height: 28px;"> </td>
<td class="table-body-col"> <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></td>
<td class="table-body-col">24 | 6 </td>
<td class="table-body-col"> <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>#rev" target="_blank" rel="noopener noreferrer"> <i class="icon like"></i></a><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>#rev" target="_blank" rel="noopener noreferrer"> <i class="icon dislike"></i></a></td>
<td class="table-body-col"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php printf( '%s', $term->count ); ?></a></td>
<td class="table-body-col"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>#rev" target="_blank" rel="noopener noreferrer"> <i class="icon email"></i></a></td>
</tr>
<?php $i++; } ?>
<?php endif;?>
</tbody>

</table>
</div>
<script>
function myFunction() {
document.getElementById("hide").style.display = "none";
}
</script>
<script>
function myFunction2() {
document.getElementById("show").style.display = "block";
}
</script>
</div>
</section>
<section class="section bg-grey" id="feed-all">
<div class="container">
<div class="title">
<h2>Отзывы о компаниях </h2>
</div>
<div class="content">
<div class="feed-item-container">
<?php $loop = new WP_Query( array( 'post_type' => 'articles', 'posts_per_page' => 4 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="feed-item">
<div class="feed-item-logo"><img src=" <?php $terms = get_the_terms( $post->ID, 'articles_category' );
if ( !empty( $terms ) ){
// get the first term
$term = array_shift( $terms );
} ?> <?php the_field('company_logo', $term); ?>" alt=""></div>
<div class="feed-item-content">
<div class="feed-item-content-title"><?php the_title(); ?>
<?php
if( has_tag( '+' ) )
echo '<i class="icon like"></i>';
?>
<?php
if( has_tag( '-' ) )
echo '<i class="icon dislike"></i>';
?>
</div>
<div class="feed-item-content-text"><?php echo $post->post_content; ?></div><span class="feed-item-content-date"><?php echo get_the_date( 'Y-m-d' ); ?> </span>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<button class="btn btn-purple show-more" onclick="window.location.href = '/articles/';">Смотреть все отзывы</button>
</div>
</div>
</section>
<section class="section" id="prices">
<div class="container">
<div class="title">
<h2>Цены на услуги ремонта <br> в ремонтных компаниях Москвы 2018</h2>
</div>
<div class="content">
<?php echo $post->post_content; ?>
</div>
</div>
</section>

<?php get_footer(); ?>
D
На сайте с 02.02.2010
Offline
143
#5

замените t1.taxonomy = 'category'

на

t1.taxonomy = 'articles_category'

post type => articles - тип поста необязательно указывать. если данные таксономии связаны только с ним

tag slug => +

это что?

у меток не может быть двух одинаковых названий, также как и slug

И еще, если это не таксономия category, а произвольная, то get_query_var('cat') нужно заменить на get_queried_object()->term_id

Авторизуйтесь или зарегистрируйтесь, чтобы оставить комментарий