0

Hey i have a list on the side of one of my pages which when clicking on the word it scrolls to its anchor which is set up like:

<div class="guide-section"  id="Measuring_up"> 

then the href looks like this:

<a class="list-group-item" href="#Measuring_up">Measuring up</a>

My question is I have made the guide section dynamic in WordPress using custom post type and linking it which is working how I want to it but the list items are not. is there a way in PHP to get an id from custom fields which I have setup like:

custom field: guide_id value: Measuring_up

Thank You

EDIT: Updated code(infinite list)

<?php
                    if( $bedGuideRight_query->have_posts() ){
                        while( $bedGuideRight_query->have_posts() ){ $bedGuideRight_query->the_post(); ?>
                        <?php global $post;?> 
                <div class="guide-section"  id="<?php echo $post->post_name; ?>">
                    <h3><?php the_title(); ?></h3>
                    <p><?php the_content(); ?></p>

                </div>
                <?php }
                    } else { ?>
                        <div class="item">
                            <div class="testimonial">
                                <h3>No Bed Guides Found</h3>
                            </div>
                        </div>
                    <?php }
                    wp_reset_postdata();
                ?>
            </div>

            <div class="col-sm-3 col-sm-pull-9 sidebar">
                <div class="panel panel-success guide ">
                  <div class="panel-heading"><strong>Guides</strong></div>
                  <div class="panel-body">
                    <p><small>Click on any of the listings below to quickly find the information you need.</small></p>
                  </div>

                  <ul class="list-group">
                  <?php if( $bedGuideRight_query->have_posts() ){
                    while( $bedGuideRight_query->have_posts() ){ $bedGuideRight_query->the_post(); ?>
                    <a class="list-group-item" href="#<?php echo $post->post_name; ?>"><?php the_title(); ?></a>
                    <?php }
                    } ?>
                  </ul>
                </div>
0

What you wanna do is use the name attribute.

<a href="#my-anchor">Link to the anchor</a>

<p name="my-anchor">This is the anchor</p>

Also, I would use something consistent, like the post/page slug to be able to link to each section.

<?php global $post; ?>

<div class="guide-section" name="<?php echo $post->post_name; ?>">

And then for your list use the slug as well. Repeat the while-loop:

<?php if( $bedGuideRight_query->have_posts() ){
   while( $bedGuideRight_query->have_posts() ){
     $bedGuideRight_query->the_post(); ?>
     <a class="list-group-item" href="#<?php echo $post->post_name; ?>"><?php the_title(); ?></a>
   <?php }
} ?>
  • not sure i understand what your correcting as i believe i am already using this method for my anchor points – Jenova1628 Sep 12 '18 at 12:19
  • You set it in a id attribute, it should be set in the name attribute. Also please clarify your question. – Pim Sep 12 '18 at 12:22
  • right I have changed it to name if you look on my edit above. I need all them anchors to work but my php code only allows one, so is there away to set the name to a custom field on WP? – Jenova1628 Sep 12 '18 at 12:24
  • 1
    Changing from name to id seems to work – Jenova1628 Sep 12 '18 at 12:47
  • 1
    Sorry its because i have so many loops of the list :L hahaha stupid me, Thank you for your help with this – Jenova1628 Sep 12 '18 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.