【问题标题】:Creating Links to Custom Post Type's on Wordpress Page在 Wordpress 页面上创建指向自定义帖子类型的链接
【发布时间】:2014-06-08 17:19:41
【问题描述】:

我试图在 Wordpress 页面上包含两件事:自定义帖子列表以及自定义帖子的内容。我希望自定义帖子的标题链接到包含帖子内容的页面部分。

例如:

  • 项目一
  • 项目二
  • 项目三

第一项标题

第一项内容...

第二项标题

第二项内容...

第三项标题

第三项内容...

所以“Item One”会链接到“ITEM ONE HEADING”。这是我正在使用的代码,它显示了自定义帖子列表以及内容,但列表项链接到自定义帖子的页面。

            <ul>
        <?php

            $query = new WP_Query( array( 'post_type' => array( 'drilling' ) ) );

            while ( $query->have_posts() ) : $query->the_post();
                echo '<li><a href="';
                the_permalink();
                echo '">';
                the_title();
                echo '</a></li>';
            endwhile;

        ?>
        </ul>

         <?php wp_reset_query(); ?> 

<?php query_posts( 'post_type=drilling'); ?>

        <?php if (have_posts()): while (have_posts()) : the_post(); ?>

        <!-- article -->
        <section class="service-middle">

            <div class="container">

                <div class="service-middle-content sixteen columns">        

                    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

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

                        <br class="clear">

                        <?php edit_post_link(); ?>

                    </article>
                    <!-- /article -->

                </div> <!--end service-middle-content-->

            </div> <!--end container-->

        </section> <!--end service-middle-->        

        <?php endwhile; ?>

        <?php else: ?>

            <!-- article -->
            <article>

                <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>

            </article>
            <!-- /article -->

        <?php endif; ?>

非常感谢您的帮助! -丹

【问题讨论】:

    标签: wordpress post hyperlink anchor


    【解决方案1】:

    您想使用 HTML 锚点 http://www.w3schools.com/html/html_links.asp

    值得一提的是,您实际上并不需要查询帖子两次。您可以使用 WP get_posts 函数 (https://codex.wordpress.org/Template_Tags/get_posts) 将帖子作为数组获取,然后遍历该数组以生成导航和您的帖子内容。

    希望这会有所帮助!

    <ul>
    <?php
    
        $query = new WP_Query( array( 'post_type' => array( 'drilling' ) ) );
    
        while ( $query->have_posts() ) :
            $query->the_post();
        ?>
    
            <li><a href="#post-<?php echo the_ID(); ?>"><?php the_title(); ?></a></li>
    
        <?php endwhile; ?>
    </ul>
    
    <?php wp_reset_query(); ?> 
    
    <?php query_posts( 'post_type=drilling'); ?>
    
    <?php if (have_posts()): while (have_posts()) : the_post(); ?>
    
    <!-- article -->
    <section class="service-middle">
    
        <div class="container">
    
            <div class="service-middle-content sixteen columns">        
    
                <!-- Anchor Tag -->
                <a name="post-<?php the_ID(); ?>"></a>
    
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
                    <h2><?php echo the_title(); ?> </h2>
                    <?php the_content(); ?>
    
                    <br class="clear">
    
                    <?php edit_post_link(); ?>
    
                </article>
                <!-- /article -->
    
            </div> <!--end service-middle-content-->
    
        </div> <!--end container-->
    
    </section> <!--end service-middle-->        
    
    <?php endwhile; ?>
    
    <?php else: ?>
    
        <!-- article -->
        <article>
    
            <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
    
        </article>
        <!-- /article -->
    
    <?php endif; ?>
    

    【讨论】:

    • 太棒了!非常感谢。非常感谢您的帮助:)
    • 没问题的朋友,如果您还有任何疑问,请告诉我
    猜你喜欢
    • 2023-03-30
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多