Question

Hi everybody i need to remove the current url of the page from my preview news post type query.

<?php
global $post;$current_id = $post->ID;
            if($lang!=('it_IT')){query_posts(array(
                           'category_name'=> 'newseng',
                           'posts_per_page' => -1,
                           'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,));}
                           else{query_posts(array(
                           'category_name'=> 'news',
                           'posts_per_page' => -1,
                           'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,));} ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
  $url = ( $current_id == $post->ID ) ? '&#35;' : get_permalink(); ?>
<div id="singlenews" class="small-12 medium-6 large-4 columns">
<?php if($lang!=('it_IT')){get_template_part( 'partials/loop', 'newsEng' );}else{get_template_part( 'partials/loop', 'news' );} ?>
                    <a href="<?php echo $url; ?>" title="<?php the_title_attribute(); ?>">
                    <button class="snow"><?php if($lang!=('it_IT')){echo'Read more';}else{echo'Leggi Tutto';} ?></button>
                    </a></div><?php endwhile; ?>
                <?php endif; ?>
                <?php wp_reset_query(); ?>

can anyone help me? i updated the code with the query to let understand that the loop is inside a query...

actual situation works correctly thanks to Nathan

Was it helpful?

Solution

Since you're comparing the current URL to the URL of posts am I correct in thinking this is being used on a post (of any type inc page) and not an archive / category?

If so a more efficient way would be to compare the original post ID and the ID of the post in the loop. What are you trying to increment? Anyway here's my suggestion based on what I've understood so far.

<?php global $post;

// Post ID will change in loop so store existing ID as a variable.
$current_id = $post->ID;

Your posts query goes here...

And then:

if ( have_posts() ) : while ( have_posts() ) : the_post();
    $url = ( $current_id == $post->ID ) ? '#' : get_permalink(); ?>

    <a href="<?php echo $url; ?>">Read Now</a>
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top