• Resolved araiwaa

    (@araiwaa)


    Hi i’m beginer

    first i made follow this content https://goldpenguin.org/blog/create-custom-tag-and-category-buttons-for-blogs-in-breakdance/ Updated September 10, 2023

        
    <?php
    function gpbd_tag_wrap_shortcode( $atts ) {
    global $post;
    $tags = get_the_tags( $post->ID );

    if ( !empty( $tags ) ) {
    $output = '<div class="post-tags">';

    foreach ( $tags as $tag ) {
    $output .= '<a href="' . esc_url( get_tag_link( $tag->term_id ) ) . '">#' . esc_html( $tag->name ) . '</a> ';
    }

    $output = rtrim( $output, ' ' ); // Remove the last comma
    $output .= '</div>';
    return $output;
    }
    }

    add_shortcode( 'tagwrap', 'gpbd_tag_wrap_shortcode' ); // Usage: #Blog Design #Breakdance Builder #UI/UX #Website Development #WordPress

    // Add CSS styles to the header
    function gpbd_tag_wrap_styles() {
    ?>

    <style>
    .post-tags {
    display: block;
    margin: 10px 0;
    font-size: 16px;
    }

    .post-tags a {
    display: inline-block;
    padding: 3px 6px;
    margin-right: 4px;
    background-color: #f2f2f2;
    color: #333;
    text-decoration: none;
    border-radius: 5px;
    }

    .post-tags a:hover {
    background-color: #333;
    color: #fff;
    }
    </style>

    <?php
    }

    add_action( 'wp_head', 'gpbd_tag_wrap_styles' ); // Add CSS to the header of the website

    ?>



    i have error in my website Full Error message: Attempt to read property “ID” on null

    2024-07-31T19:49:43+00:00 Snippet “Custom Post Tags” (#64) was automatically deactivated due to a fatal error. 2024-07-31T19:49:44+00:00 Attempt to read property “ID” on null

    How i can fixit

    Regards,
    Tanapat

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @araiwaa,

    First of all, please remove the closing PHP tag at the end of the snippet ?>. In PHP files, it’s best not to add that at the end of a file/snippet to avoid unwanted output.

    Regarding the error you are running into, that is related to your code trying to access the ID property on the $post variable when that is not available. Due to the way you are loading the code as a shortcode, the ID is not available in that context so it is throwing an error on the line with this code: $tags = get_the_tags( $post->ID );

    To get around this, I suggest adding a check above that line, something like this:

    
    if (empty($post->ID) ) {
    return '';
    }
    
    Thread Starter araiwaa

    (@araiwaa)

    Thank you so much

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Tag & Category Buttons for Blogs in Breakdance’ is closed to new replies.