- This topic has 10 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
January 7, 2023 at 5:53 am #2486860
George
Is there a filter to add a data attribute to each post inside a custom post type archive?
For example, in there:
<article id="post-2692" class="post-2692 portfolio type-portfolio status-publish has-post-thumbnail hentry portfolio-categories-logo" itemtype="https://schema.org/CreativeWork" itemscope=""> [rest of the markup] </article>I want to add this:
<article class="portfolio" data-post-id="<?php the_ID(); ?>">January 7, 2023 at 8:23 am #2487138David
StaffCustomer SupportHi George,
you could use Child Theme and create your own content.php for that post type and edit the code here:
Or you could hijack the
generate_{$context}_microdatafilter:to hook in your own attributes:
add_filter('generate_article_microdata', function($data){ if ( is_archive() ) { $type = apply_filters( 'generate_article_itemtype', 'CreativeWork' ); $data = sprintf( 'data-post-id="%1$s" itemtype="https://schema.org/%2$s" itemscope', get_the_id(), esc_html( $type ) ); } return $data; });Just change the
is_archive()template tag condition to suitJanuary 7, 2023 at 8:37 am #2487151George
The
content.phpdoesn’t seem to be related with the archives, I tried deleting it just for testing and the archive was still displaying.When I use the following code:
add_filter('generate_article_microdata', function($data){ if ( is_post_type_archive( 'portfolio' ) ) { $type = apply_filters( 'generate_article_itemtype', 'CreativeWork' ); $data = sprintf( 'data-post-id="%1$s" itemtype="https://schema.org/%2$s" itemscope', get_the_id(), esc_html( $type ) ); } return $data; });I don’t see anything added to the markup.
January 7, 2023 at 8:52 am #2487157David
StaffCustomer SupportWhere can i see it ?
January 7, 2023 at 10:19 am #2487238George
It’s just a few random posts. Custom port type archive URL attached.
January 7, 2023 at 5:37 pm #2487458George
Ah, I see, it works. Sorry, I am actually using a content template, though!
January 8, 2023 at 5:09 am #2487757David
StaffCustomer SupportAh, with a content template, there is no filter in the
<article>to do that.
Does it have to be a data-attribute attached to the article tag ?January 8, 2023 at 10:28 am #2488101George
I am just exploring ways of AJAX filtering a custom post type archive with the custom taxonomy terms as buttons. I was able to filter custom HTML markup that was created while building the AJAX Request. This is part of the code:
// Build the HTML for the posts $html = ''; if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $html .= '<div class="portfolio-item">'; $html .= '<h2>' . get_the_title() . '</h2>'; $html .= get_the_post_thumbnail(); $html .= '</div>'; } } else { $html = '<p>No posts found</p>'; }but it seems I need to use some kind of data attribute to filter existing archives. Might be another way, I am not sure, though.
January 8, 2023 at 10:47 am #2488112David
StaffCustomer SupportCould you repurpose your Ajax to use the
id="post-2692"?January 10, 2023 at 1:50 am #2489786George
I will close this ticket and try and do that, David, thanks!
January 10, 2023 at 3:35 am #2489895David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.