最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - What is Wordpress Way to Add Content Blocks to Post and Reference them as Many to One Relationship?

programmeradmin1浏览0评论

I am using a plugin that uses custom post content types. I think what I am trying to do is a gerneral Wordpress workflow question versus plugin specific. That said the plugin in LearnDash.

So, I have a post type. Call it Topic. I want a Topic to have Exercises, where the exercises will be a content block. I don't need exercises to have a route. That is, no url. Just like adding a div or some block, but where the exercises can then be referenced.

For instance, I might want to have right sidebar on a topic page that lists the exercises for that topic with a url that takes one to the anchor on the page for the exercise.

Or in my navigation menu for a lesson, where you see

Lesson
   Topic1
   Topic2
etc

I might want to display

Lesson
  Topic1
  Exercise1.1
  Topic2
  Exercise2.1
  Exercise2.2

See what I mean? I really have no need for an exercise to be a content type that is routable. I just need to use exercises as blocks that I can then reference as a many to one collective.

So trying to figure out the accepted way to do this with a plugin or by doing some coding.

thanks, Brian

I am using a plugin that uses custom post content types. I think what I am trying to do is a gerneral Wordpress workflow question versus plugin specific. That said the plugin in LearnDash.

So, I have a post type. Call it Topic. I want a Topic to have Exercises, where the exercises will be a content block. I don't need exercises to have a route. That is, no url. Just like adding a div or some block, but where the exercises can then be referenced.

For instance, I might want to have right sidebar on a topic page that lists the exercises for that topic with a url that takes one to the anchor on the page for the exercise.

Or in my navigation menu for a lesson, where you see

Lesson
   Topic1
   Topic2
etc

I might want to display

Lesson
  Topic1
  Exercise1.1
  Topic2
  Exercise2.1
  Exercise2.2

See what I mean? I really have no need for an exercise to be a content type that is routable. I just need to use exercises as blocks that I can then reference as a many to one collective.

So trying to figure out the accepted way to do this with a plugin or by doing some coding.

thanks, Brian

Share Improve this question asked Oct 9, 2019 at 19:48 BrianBrian 3372 silver badges11 bronze badges 3
  • Sounds like you are looking for "reusable blocks". eg. wpbeginner/beginners-guide/… – majick Commented Oct 10, 2019 at 3:32
  • Yeah, I am using blocks in this way, but what I am trying to do is associate each block with the actual post so that in a sidebar I can create a navigation menu. So if I have 3 instances of a block, each with some id like id="topic-1-exerciseblock-1" then while on that page I want a sidebar navigation pointing to each block as an anchor. – Brian Commented Oct 10, 2019 at 6:08
  • gotcha, sorry misread the first time, posted answer instead. – majick Commented Oct 10, 2019 at 8:33
Add a comment  | 

1 Answer 1

Reset to default 0

You could first add the anchor links inside the blocks with something like:

<a class="block-anchor" name="topic-1" title="Topic 1">

<a class="block-anchor" name="topic-1-exerciseblock-1-1" title="Exercise 1.1">

Then create a widget with some jQuery/javascript that pulls all the anchors and lists them. Probably the easiest way to do this would be to put a shortcode in a Text Widget, eg. [block-anchor-list].

add_shortcode('block-anchor-list', 'block_anchor_list');
function block_anchor_list() {
    add_action('wp_footer', 'block_anchor_list_jquery');
    $list = "<div id='block-anchor-list'></div>";
    return $list;
}

function block_anchor_list_jquery() { 
    ?>
    <script>jQuery(document).ready(function() {
        listhtml = '<ul class="block-anchor-list">';
        jQuery('.block-anchor').each() {
             hash = jQuery(this).attr('name');
             anchor = jQuery(this).attr('title');
             listhtml += '<li class="block-anchor-item">';
             listhtml += '<a href="#'+hash+'" title="Jump to '+anchor+'.">'+anchor;
             listhtml += '</a></li>';
        }
        listhtml += '</ul>';
        jQuery('#block-anchor-list').html(listhtml);
    });</script>
    <?php
}

Note this solution relies on jQuery being loaded on the frontend, but could be done with vanilla javascript also.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论