0

I have this code but I can't get to delete the div with the class otrasObras

<?php
$test = "";
$posttags = get_the_tags();
if ($posttags) {
    foreach($posttags as $tag) {
        $test .= ',' . $tag->name; 
    }
}
$test = substr($test, 1);
query_posts('tag='.$test.'&showposts=-1');
if(have_posts()){
?>
<div class="otrasObras">
    <h3>Contenido relacionado:</h3>
    <ul>
        <?php
        while (have_posts()) : the_post();
        if($post->ID != $idPost):
            ?>
        <li>
            <a href="<?php the_permalink(); ?>"  title="Ver la obra <?php the_title(); ?>">
                <h4><?php the_title(); ?></h4>
            </a>
        </li>
        <?php
        endif;
        endwhile;
        wp_reset_query();
        ?>
    </ul>
</div>
<?php } ?>

I read a bit about this in here http://codex.wordpress.org/Function_Reference/WP_Query but they were using the object syntax.

EDIT: .otrasObras is a module that I want to show only when it has content, and the content are other posts with the same tag, so if there are no more posts with that tag, I don't want it to pass the if(have_posts()) conditional, but this seems to not work, as it gets printed all the time

  • 1
    what are you trying to acheive. Nothing is clear from whatever you have explained so far – terminator Jul 18 '15 at 21:13
  • .otrasObras is a module that I want to show only when it has content, and the content are other posts with the same tag, so if there are no more posts with that tag, I don't want it to pass the if(have_posts()) conditional, but this seems to not work, as it gets printed all the time – Vandervals Jul 19 '15 at 9:27
  • Check this answer. Use post_tag as taxonomy for build in tags :-) – Pieter Goosen Jul 19 '15 at 12:41
  • Just one important note, never ever use query_posts. It breaks the main query object. Related posts relies on this main query object, so you don't just break the main query object, but you also break related posts functions. Use WP_Query or get_posts instead – Pieter Goosen Jul 19 '15 at 13:01
0

this code is with in the loop of single.php , RIGHT? If so then give this code the try , it should work . Problem with your code seems that you are using showpost, rather you should be using posts_per_page

Plus i see this fragments of code like

`$test = ""; 

$test .= ',' . $tag->name; 

$test = substr($test, 1);`

I don't seem to find its use.

And then i see this if($post->ID != $idPost)

$idPost seems to be the id of the single post , whose tags you get initially , right?

Then this is how i will try to solve this problem.

  <?php
$posttags = get_the_tags();
if ($posttags) :
?>
    <div class="otrasObras">
      <h3>Contenido relacionado:</h3>
      <ul>
<?php    foreach($posttags as $tag) :        
        $my_args= array('tag_id'=>$tag->term_id,
                        'posts_per_page'=>2,   
                        );
        $myloop= new WP_Query($my_args); 

        if($myloop->have_posts()): 

            if($myloop->have_posts()):
?>

                    <?php
                    while ($myloop->have_posts()) : $myloop->the_post();                    
                    if(($myloop->post->ID) == $idPost)continue;
                    ?>

                    <li>
                        <a href="<?php the_permalink(); ?>"  title="Ver la obra <?php the_title(); ?>">
                            <h4><?php the_title(); ?></h4>
                        </a>
                    </li>
                    <?php
                    break;                    
                    endwhile;
                    wp_reset_query();
                    ?>

<?php
            endif;
        endif;
    endforeach;
?>
     </ul>
            </div>    
<?php
endif;
?>
  • Check your code here phpcodechecker.com because it is broken – Vandervals Jul 19 '15 at 10:26
  • @Vandervals i missed > in 'tag_id'=>$tag->term_id , anyways i updated the answer . – terminator Jul 19 '15 at 10:39
  • This has no errors but doesn't seem to work either as now the related posts never show.... – Vandervals Jul 19 '15 at 10:53
  • @Vandervals Please see the updated answer – terminator Jul 19 '15 at 11:51
  • @Vandervals plz let me know if it doesn't work, coz i spent a quite a bit time figuring this out – terminator Jul 19 '15 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.