在Wordpress页面上创建自定义帖子类型的链接

时间:2014-04-23 17:49:03

标签: wordpress post hyperlink anchor

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

例如:

  • 第一项
  • 第二项
  • 第三项

ITEM ONE HEADING

第一项内容......

ITEM TWO HEADING

第二项内容......

项目三标题

第三项内容......

所以"第一项"将链接到" 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; ?>

非常感谢您的帮助! -Dan

1 个答案:

答案 0 :(得分:1)

您想使用HTML Anchors 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; ?>