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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路