Site logo

Archived topic

Add data attribute to custom post archive article item

10 replies · Started by George on January 7, 2023

Viewing posts 1–11 of 11

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(); ?>">

Hi George,

you could use Child Theme and create your own content.php for that post type and edit the code here:

https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/content.php#L12

Or you could hijack the generate_{$context}_microdata filter:

https://github.com/tomusborne/generatepress/blob/4895a2e7595bb809075b375201fd735112f41570/inc/theme-functions.php#L500

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 suit

The content.php doesn'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.

Where can i see it ?

It's just a few random posts. Custom port type archive URL attached.

Ah, I see, it works. Sorry, I am actually using a content template, though!

Ah, 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 ?

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.

Could you repurpose your Ajax to use the id="post-2692" ?

I will close this ticket and try and do that, David, thanks!

You're welcome

This archived topic is closed to new replies.