Answer the question
In order to leave comments, you need to log in
How to optimize the output of recommended reading materials?
There is a site on WP. In the article, after the text, a block of articles recommended for reading is displayed. They are output as follows:
<?php
global $SMTheme;
if ($SMTheme->get( 'layout', 'related' )) { ?>
<div class="related-posts"><p></p><ul>
<?php
$postid=$post->ID;
$showed_posts=array();
$tags=get_the_tags($postid);
$categories = get_the_category($postid);
$post_cnt=0;
$first=0;
if ($tags) {
$tags_ids = array();
foreach($tags as $tag) $tags_ids[] = $tag->term_id;
$args1=array(
'tag__in' => $tags_ids,
'showposts'=>$SMTheme->get( 'layout', 'relatedcnt' ),
'caller_get_posts'=>1);
$posts1=get_posts($args1);
if(count($posts1)>4){
$number1 = 0;
for($i=0; $i<count($posts1); $i++)
{
$item = $posts1[$i];
if($item->ID==$post->ID) $number1 = $i;
}
$arr1 = array();
$array_id = array();
for($i=$number1; $i<count($posts1);$i++)
{
$item = $posts1[$i];
$arr1[] = $item;
$array_id[] = $item->id;
}
for($i=0; $i<$number1;$i++)
{
$item = $posts1[$i];
$arr1[] = $item;
$array_id[] = $item->id;
}
$massiv = array();
for($i=1; $i<3; $i++)
{
$massiv[]= $arr1[$i]->ID;
$r=rand(1,150);
$g=rand(1,150);
$b=rand(1,150);
$thumb_id = get_post_thumbnail_id($arr1[$i]->ID);
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
$usl=get_post_meta($arr1[$i]->ID, '_yoast_wpseo_title', true);
if($usl==null){$ankor=$arr1[$i]->post_title;}
else{$ankor=$usl;}
$prin1='<li><a href="'.get_permalink($arr1[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$ankor.'</span></a></li>';
print $prin1;
}
for($i=0; $i<2; $i++)
{
$arr11=array_reverse($arr1);
$massiv[]= $arr11[$i]->ID;
$r=rand(1,150);
$g=rand(1,150);
$b=rand(1,150);
$thumb_id = get_post_thumbnail_id($arr11[$i]->ID);
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
$usl=get_post_meta($arr11[$i]->ID, '_yoast_wpseo_title', true);
if($usl==null){$ankor=$arr11[$i]->post_title;}
else{$ankor=$usl;}
$prin1='<li><a href="'.get_permalink($arr11[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$ankor.'</span></a></li>';
print $prin1;
}
foreach($massiv as $v){
$massiv12[]=intval($v);
}
}
}
if (!empty($massiv12)){$massiv12=$massiv12;}
else {$massiv12=array(1);}
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$showed_posts[]=$postid;
$args=array(
'category__in' => $category_ids,
'post__not_in' => $massiv12,
'showposts'=>$SMTheme->get( 'layout', 'relatedcnt' ),
'caller_get_posts'=>1);
$posts=get_posts($args);
$number = 0;
for($i=0; $i<count($posts); $i++)
{
$item = $posts[$i];
if($item->ID==$post->ID) $number = $i;
}
$arr = array();
for($i=$number; $i<count($posts);$i++)
{
$item = $posts[$i];
$arr[] = $item;
}
for($i=0; $i<$number;$i++)
{
$item = $posts[$i];
$arr[] = $item;
}
for($i=1; $i<3; $i++)
{
$r=rand(1,150);
$g=rand(1,150);
$b=rand(1,150);
$thumb_id = get_post_thumbnail_id($arr[$i]->ID);
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
$prin='<li><a href="'.get_permalink($arr[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$arr[$i]->post_title.'</span></a></li>';
print $prin;
}
for($i=0; $i<2; $i++)
{
$arr111=array_reverse($arr);
$r=rand(1,150);
$g=rand(1,150);
$b=rand(1,150);
$thumb_id = get_post_thumbnail_id($arr111[$i]->ID);
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
$prin='<li><a href="'.get_permalink($arr111[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$arr111[$i]->post_title.'</span></a></li>';
print $prin;
}
?></ul></div>
<?php } ?>
Answer the question
In order to leave comments, you need to log in
At the same time, if it turns out that the articles are duplicated by tags and simply in order, then the duplicating one is removed from the links in order and the next one is taken.Have you tried reverse logic?
You use the standard functions to access posts in the database, enable the caching backend (Memcached, Redis) and everything will be ok. Another option is to use the Transients API to cache the result of all these requests at once, for example, for 1 day.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question