Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as PHP by Fernando ( 6 years ago )
function acf_links_content($content){
 
    $max_related      = 6; //Quantidade de posts relacionados
    $aCadaXparagrafos = 9; //Intervalo de parágrafos (a cada X)
    $aCadaXH          = 5; //Intervalo de H2 ou H3 (a cada X)
 
    if(has_tag('norelated')){
        return $content;
    }else{
 
        global $post;
 
        $id = $post->ID;
 
        # Get ACF array
        $posts = get_field('relacionadas');
 
        if($posts == ""){
 
            if(has_tag()){
 
                $tags             = get_the_tags();
                $num_tags         = count($tags);
                $tag              = $tags[0]->slug;
                $the_query        = new WP_Query(
                                                array(
                                                    'tag'          => $tag,
                                                    'post__not_in' => array($id),
                                                )
                                            );
                $posts            = $the_query->posts;
                $limit            = (count($posts) < $max_related)?(count($posts)):6;
                $marcaParagrafo   = "</p>";
                $content_orig     = $content;
                $content          = explode($marcaParagrafo, $content);
 
                if(count($content) < ($limit * $aCadaXparagrafos)){
                    $marcaParagrafo = "</h2>";
                    $content = explode($marcaParagrafo, $content_orig);
                    $aCadaXparagrafosOrig = $aCadaXparagrafos;
                    $aCadaXparagrafos = $aCadaXH;
 
                    if(count($content) < $max_related){
 
                        $marcaParagrafo = "</h3>";
                        $content = explode($marcaParagrafo, $content_orig);
                        $numTags = count($content);
 
                        if($numTags < 2 || $numTags < $max_related){
                            $marcaParagrafo = "</p>";
                            $content = explode($marcaParagrafo, $content_orig);
                            $aCadaXparagrafos = floor(($aCadaXparagrafosOrig-1)/2);
                        }
                    }
                }
 
                $new_content = '';
                $numParagrafos = count($content);
                $acf_i = 0;
                $c = 0;
 
                for ($i = 0; $i < $numParagrafos; $i++) {
 
                    if ($i % $aCadaXparagrafos == 0 && $i != 0) {
 
                        $c++;
                        ob_start();
                        if($c < $limit or $c == $limit) {
                            if($id == $posts[$acf_i]->ID) { } else {
                        setup_postdata($posts[$acf_i]);
                        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($posts[$acf_i]->ID), '218thumb' );
                        $url = $thumb['0']; ?>
 
                            <?php if(is_single()) { ?>
                            <div class="relacionado">
                                <a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content"><img src="<?php echo $url; ?>" class="img-responsive<?php if (is_amp_endpoint()) { echo " relacionado-amp"; } ?>"></a>
                                <span>Veja também</span>
                                <span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content">
                                <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?></a></span>
                            </div>
                            <?php } else { ?>
                            <p><span class="leia">Leia também:</span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>">
                            <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?> <?php echo $limite; ?></a> <?php echo $id; ?></p>
                            <?php } ?>
 
                        <?php
                            }
                        $acf_i++;
                        }
                        $new_content.= ob_get_contents();
                        ob_end_clean();
                    }
                    $new_content .= $content[$i].$marcaParagrafo;
                }
                wp_reset_postdata();
                return $new_content;
            }else{
                return $content;
            }
        }else{
 
            # Validating array to avoid warnings (Luiz)
            if (is_array($posts)){
                $limit = count($posts);
            }else{
                $limit = 0;
            }
 
            # $limit = count($posts);
            $marcaParagrafo = "</p>";
            $numParagrafos = count($content);
            $new_content = '';
            $aCadaXparagrafos = 5;
            $acf_i = 0;
            $c = 0;
 
            $content_orig     = $content;
            $content          = explode($marcaParagrafo, $content);
 
            if(count($content) < ($limit * $aCadaXparagrafos)){
                $marcaParagrafo = "</h2>";
                $content = explode($marcaParagrafo, $content_orig);
                $aCadaXparagrafosOrig = $aCadaXparagrafos;
                $aCadaXparagrafos = $aCadaXH;
 
                if(count($content) < $max_related){
 
                    $marcaParagrafo = "</h3>";
                    $content = explode($marcaParagrafo, $content_orig);
                    $numTags = count($content);
 
                    if($numTags < 2 || $numTags < $max_related){
                        $marcaParagrafo = "</p>";
                        $content = explode($marcaParagrafo, $content_orig);
                        $aCadaXparagrafos = floor(($aCadaXparagrafosOrig-1)/2);
                    }
                }
            }
 
            $new_content = '';
            $numParagrafos = count($content);
 
            for ($i = 0; $i < $numParagrafos; $i++) {
 
                if ($i % $aCadaXparagrafos == 0 && $i != 0) {
 
                    $c++;
                    ob_start();
                    if($c < $limit or $c == $limit) {
                        if($id == $posts[$acf_i]->ID) { } else {
                    setup_postdata($posts[$acf_i]);
                    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($posts[$acf_i]->ID), '218thumb' );
                    $url = $thumb['0']; ?>
 
                        <?php if(is_single()) { ?>
                        <div class="relacionado">
                            <a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content"><img src="<?php echo $url; ?>" class="img-responsive<?php if (is_amp_endpoint()) { echo " relacionado-amp"; } ?>"></a>
                            <span>Veja também</span>
                            <span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content">
                            <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?></a></span>
                        </div>
                        <?php } else { ?>
                        <p><span class="leia">Leia também:</span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>">
                        <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?> <?php echo $limite; ?></a> <?php echo $id; ?></p>
                        <?php } ?>
 
                    <?php
                        }
                    $acf_i++;
                    }
                    $new_content.= ob_get_contents();
                    ob_end_clean();
                }
                $new_content .= $content[$i].$marcaParagrafo;
            }
            return $new_content;
        }
    }
}
add_filter('the_content', 'acf_links_content');

 

Revise this Paste

Your Name: Code Language: