dongli8979 2014-05-26 12:12
浏览 34
已采纳

Wordpress永久链接到特定的博客帖子,而不是当前的URL

I have a loop on my homepage showing 3 recent blog posts, this is being included in from the footer. If I go elsewhere on the site though such as a different blog post, the 'the_permalink' is instead of grabbing the featured 3 blog post links, it's grabbing the link from the page your currently on.

These 3 blog posts will change as new ones are added so I cannot define that the link be only a specific blog post(s).

Here is the code showing the first blog post. Found within footer.php and therefore shows on each page.

            <div id="news-container-1">
                <?php
                // Create a variable to hold our custom Loop results
                $excludes  = array('4135');
                $frontpageposts = get_posts( array( 
                     'numberposts' => 1, // only the 3 latest posts
                     'post__not_in' => $excludes
                ) );

                // Create output only if we have results
                // Customize to suit your HTML markup
                if ( $frontpageposts ) { 

                     foreach ( $frontpageposts as $fppost ) { 
                          // setup postdata, so we can use template tags
                          setup_postdata($fppost);
                          ?>

                          <div <?php post_class(); ?>>
                               <h4><a href="<?php the_permalink(1); ?>"><?php //the_title(); ?>Latest News #1</a></h4>
                               <div class="post-entry">
                                    <?php                                                
                                            $content = $post->post_content;
                                            $searchimages = '~<img [^>]* />~';

                                            /*Run preg_match_all to grab all the images and save the results in $pics*/

                                            preg_match_all( $searchimages, $content, $pics );

                                            // Check to see if we have at least 1 image
                                            $iNumberOfPics = count($pics[0]);

                                            if ( $iNumberOfPics > 0 ) { ?>
                                                <!-- GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE -->
                                                  <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
                                            <?php }
                                    ?>
                                    <?php //the_excerpt(); ?>
                               </div>
                          </div>

                <?php }
                } 
                ?>
            </div>
  • 写回答

1条回答 默认 最新

  • dongsiju1941 2014-05-26 12:28
    关注

    the_permalink() doesn't have any parameters so remove the '1'.

    You need to change the way you're setting up the posts. setup_postdata() is currently being used incorrectly.

    if ( $frontpageposts ) { 
        global $post;
    
        foreach ( $frontpageposts as $post ) { 
            // setup postdata, so we can use template tags
            setup_postdata( $post );
    

    Then at the end of your foreach loop you need to reset the post object.

    Change this:

    <?php }
    } 
    

    To this:

    <?php }
        wp_reset_postdata();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器