Set IDs of articles (markup-id: beans-post) to post titles


Hi, maybe I don't completely understand the way the HTML API works, but I had a problem with changing the ID of the article tag inside the loop to a dynamic value (the title of the currently queried post). I'm building a one page website and needed to change the IDs to make them correspond to the anchor links of the nav menu. I'm using a front-page.php template. I first tried:

beans_replace_attribute( 'beans_post', 'id', get_the_ID(), strtolower(get_the_title()) );

This only worked for the first article in the loop, the ID of the others remained unchanged. Then I tried to use the same code within an action hook callback (beans_post_before_markup) to refresh the post object and get the correct ID. This actually works, but I wonder if I should have done it in any other (smarter) way. So what is the best way to target beans markup-IDs that arent' really unique (like 'beans-post')? Thanks, Timo


Hey Timo,

This is a bit of a tricky one so don't bother to much if you didn't find it by yourself πŸ˜‰ Your code above won't work because your get_the_ID() is getting the id of the page globally and using it for all the posts instead of getting each post ids in the post loop. That is the reason it only works with the first one. To modify a post attribute based on its specific post data, you need to use a callback which will be called in the loop. Likely Beans as a filter {$markup_id}_attributes which filter each markup (with markup ID) attributes.

So in your case here is the snippet you are looking for:

add_filter( 'beans_post_attributes', 'example_post_attributes' );

function example_post_attributes( $attributes ) {

  // Change the id attribute.
 $attributes['id'] = strtolower( str_replace( ' ', '-', get_the_title() ) ); // automatically escaped.

 return $attributes;

}

Note that I have added a str_replace to replace spaces with dash in order to have valid ids.

Hope that helps,


Thank you very much, Thierry

That's a nice way to do it. Now I can also set data-attributes dynamically without Javasript (as I did before). Great!

But I didn't find anything about that filter in the documentation. Is there a list of all the filters available? I probaly could use beans_has_filters to find out whether a specific filter exists, but that would be some trial and error guessing.


Hey Timo,

Generally speaking, all the function and filter references are listed in the Code Reference. For most, the ones which are not listed are the following actions and filters in the HTML API:

Filters

  1. {$markup_id}_attributes filters each markup attributes.
  2. {$markup_id}_markup filters each markup tag.
  3. {$output_id}_output filters output content.

Actions

  1. {$markup_id}_before_markup action called before an opening markup.
  2. {$markup_id}_prepend_markup action after before an opening markup.
  3. {$markup_id}_append_markup action called before a closing markup.
  4. {$markup_id}_after_markup action after before a closing markup.

Finding a filter/action markup id {$markup_id}

The dynamic part of the filter {$markup_id} can be found by inspecting (inspector tool) in the front end (in dev mode) and look for the markup data-markup-id attribute.

Finding a filter markup id {$output_id}

The dynamic part of the filter {$output_id} can be found by inspecting (inspector tool) in the front end (in dev mode) and look for HTML comments such as <!-- open output: beans_post_meta_date_prefix -->.

--

This would be worth adding to the documentation and I have made a note of that. The main reason these are not referenced is that 99% of the time you would not use these filters but rather use the API functions such as beans_add_attribute() which are much easier to use. Only in the case of a loop will you use these filters which also requires a solid understanding of WP hooks and fairly good PHP knowledge πŸ™‚

Hope that helps πŸ™‚


Hey Thierry,

thanks a lot, that makes everything clearer. Having it documented (maybe with a note that in most cases it's easier to use the dedicated API functions) would definetely be a good thing!

Write a reply

Login or register to write a reply, it's free!