简体   繁体   English

查找当前帖子ID并将其从wordpress查询中的get_permalink中排除

[英]Find current post id and exclude it from the get_permalink inside wordpress query

Hi everybody i need to remove the current url of the page from my preview news post type query. 大家好,我需要从我的预览新闻帖子类型查询中删除页面的当前URL。

<?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 内森感谢实际情况

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? 由于您正在将当前URL与帖子URL进行比较,我是否认为这是在帖子(任何类型的公司页面)而不是存档/类别上使用的?

If so a more efficient way would be to compare the original post ID and the ID of the post in the loop. 如果是这样,一种更有效的方法是比较原始帖子ID和循环中帖子的ID。 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>
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM