Site logo

[Resolved] Add data attribute to custom post archive article item

Home Forums Support [Resolved] Add data attribute to custom post archive article item

Home Forums Support Add data attribute to custom post archive article item

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #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(); ?>">

    #2487138
    David
    Staff
    Customer Support

    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

    #2487151
    George

    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.

    #2487157
    David
    Staff
    Customer Support

    Where can i see it ?

    #2487238
    George

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

    #2487458
    George

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

    #2487757
    David
    Staff
    Customer Support

    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 ?

    #2488101
    George

    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.

    #2488112
    David
    Staff
    Customer Support

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

    #2489786
    George

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

    #2489895
    David
    Staff
    Customer Support

    You’re welcome

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.