Ср Фев 09, 2011 7:19 pm |
Start Post: схожие записи определенной категории без плагина Wordpress  |
![]() DK + wordpress |
Зарегистрирован: 18.06.2008
Сообщений: 2425
|
Обратиться по нику
|
DK + |
Ответить с цитатой | | |
|
Вопрос 1 : как исключить определенную категорию, чтобы для нее не выводились похожие записи.
Вопрос 2 : Как сделать чтобы приоритетно сортировка происходила по тайтлу.
собсно сам код.
Код: |
<div class="sample-posts">
<h3>Похожие статьи:</h3>
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
// Rest is the same as the previous code
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
wp_reset_query();
}
?></div>
|
|
|
|
|
|
![]() Codd Опытный |
Зарегистрирован: 13.01.2011
Сообщений: 186
|
Обратиться по нику
|
Codd |
Ответить с цитатой | | |
|
В асе всё довели до ума, может кому пригодится:
Код: |
<?php
$categories = get_the_category($post->ID);
$category_ids = array();
foreach($categories as $individual_category)
$category_ids[] = $individual_category->term_id;
$current_post_title = explode(' ', $post->post_title);
$posts = get_posts( array(
'category' => implode(',' , $category_ids),
'showposts' => -1
) );
$posts_relative_ids = array();
foreach($posts as $a_post){
$posts_relative_ids[$a_post->ID] = 0;
foreach($current_post_title as $word){
if( strpos($a_post->post_title, ' ' .$word. ' ') != FALSE)
$posts_relative_ids[$a_post->ID]++;
}
}
unset($posts_relative_ids[$post->ID]);
arsort($posts_relative_ids); $n = 0;
foreach($posts_relative_ids as $id => $value){
$posts_relative[] = $id;
if(5 == $n++) break;
}
$args=array(
'post__in' => $posts_relative,
'showposts' => 5,
'orderby' => 'title',
'order' => 'ASC'
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
wp_reset_query();
?>
|
|
|
|
|
|