• Resolved snippet24

    (@snippet24)


    I need to replace get_the_title(); by this conditional

    if($modo_vista_proyecto == "cuadricula") { ?>
                        <h2><?php the_title(); ?></h2>
    
                        <?php echo the_content(); 
                        }
                    if ($modo_vista_proyecto == "tabla") { ?>
                        <h2><?php the_title(); ?> oook</h2> 
                    <?php } ?> 

    in the following code:

    <?php if ( $query->have_posts() ) {
                  $post_type_posts = [];
                  $other_posts = []; 
                        while($query->have_posts()) {
                           $query->the_post();
    
                           if( get_field('destacado') == 'destacado' ) {
                              $post_type_posts[] = get_the_title();
                           }else{
                              $other_posts[] = get_the_title();
                           }
                         }
                    }
    
                    $new_loop = array_merge($post_type_posts, $other_posts);
    
                    foreach ($new_loop as $loop) {
                       var_dump( $loop );
                    }  ?>

    but I’m not sure how t achieve it… since is a variable of type array, maybe theres another way? But I do need to merge this later on in a singe loop. I do confess I took this merge idea somewhere else..

    • This topic was modified 3 years, 5 months ago by snippet24.
    • This topic was modified 3 years, 5 months ago by snippet24.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter snippet24

    (@snippet24)

    Ok I’m setting my own function inside the variables, should work

    Thread Starter snippet24

    (@snippet24)

    And no, because is an array :/

    To replace the use of get_the_title() with the conditional that you provided, you can try the following approach:

    1. Replace get_the_title() with get_the_title(get_the_ID()). This will allow you to pass the post ID as an argument to get_the_title() so that you can get the title of the current post in the loop.
    2. Replace var_dump($loop) with the following block of code:
    if($modo_vista_proyecto == "cuadricula") { ?>
      <h2><?php echo get_the_title(get_the_ID()); ?></h2>
      <?php echo the_content(); 
    }
    if ($modo_vista_proyecto == "tabla") { ?>
      <h2><?php echo get_the_title(get_the_ID()); ?> oook</h2> 
    <?php }
    

    This should output the title and content of the current post in the loop, depending on the value of $modo_vista_proyecto.

    It’s also worth noting that the var_dump() function is used for debugging purposes and is not typically used in production code. Instead of using var_dump(), you may want to consider using a more appropriate function to output the data, such as echo or print_r().

    Thread Starter snippet24

    (@snippet24)

    if($modo_vista_proyecto == "cuadricula") { ?>
      <h2><?php echo get_the_title(get_the_ID()); ?></h2>
      <?php echo the_content(); 
    }
    if ($modo_vista_proyecto == "tabla") { ?>
      <h2><?php echo get_the_title(get_the_ID()); ?> oook</h2> 
    <?php }

    Mmm shouldn’t the $loop variable be passed here? otherwise it would repeat the same title over and over… thank you.

    I think you’re right, try this:

    if($modo_vista_proyecto == "cuadricula") { ?>
      <h2><?php echo get_the_title($loop); ?></h2>
      <?php echo the_content(); 
    }
    if ($modo_vista_proyecto == "tabla") { ?>
      <h2><?php echo get_the_title($loop); ?> oook</h2> 
    <?php }
    
    Thread Starter snippet24

    (@snippet24)

    Tried

    <h2><?php echo get_the_title($loop); ?></h2> 

    and for some reason it displays nothing, but if I use instead

    <h2><?php echo the_title($loop); ?></h2>

    it does display each title but at the end of each title it appends the name of the first title like this:

    %name_title1% %name_title1%

    %name_title2% %name_title1%

    %name_title3% %name_title1%

    I also wonder with this way how can I pass the ID to the_content or other custom fields?

    Oh and have a very happy new year!!!

    • This reply was modified 3 years, 5 months ago by snippet24.
    • This reply was modified 3 years, 5 months ago by snippet24.
    • This reply was modified 3 years, 5 months ago by snippet24.
    Thread Starter snippet24

    (@snippet24)

    Okey this is how is done to custom fields:

    <?php the_field('field_name', $mypostid) ;?>

    But why this is not working properly? (help please here)

    <h2><?php echo the_title($loop); ?></h2>

    And for the_content seem to be like this:

    echo get_post_field('post_content', $post_id);
    Thread Starter snippet24

    (@snippet24)

    Ok figured out, here was the issue : (notice should be get_the_ID) :

    $loop_destacados[] = get_the_ID();
                           }else{                          $loop_normales[] = get_the_ID();

    and used as well

    echo get_post_field('post_title', $loop)
    Thread Starter snippet24

    (@snippet24)

    Thanks @miketopher for all the help!! πŸ™‚

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘How to add conditional inside variable of type array in the WordPress Loop?’ is closed to new replies.