Adding snippets
-
Hi, I have created the Custom Plugin to add the snippets you have offered us, but I think I am not doing it right (I remove the beginning <?php from the github code but the two snippets do not work for me. I tried them each separately also.
I added the snippet for Ads by Author (I do have Authors extension activated also) but the Authors names are not clickable in the ad listings, https://volusiacounty4sale.com/advert/bronco-great-shape/ (this author has 2 ads)
Then I also tried Custom snippet plugin for the carousel snippet. I tried it with and without the closing <php? tag and I have two car ads active but no carousel displays on the ad
(this is how I added the snippets to the custom snippet plugin
<?php /* Plugin Name: My Custom Codes Plugin URI: https://wpadverts.com/ Description: My various custom code snippets Author: Me Version: 1.0 */ // Paste the snippets below ... /** Plugin Name: WPAdverts Snippets - Related Ads Carousel Version: 1.0 Author: Greg Winiarski Description: Shows a list of related Ads on the Ad details page. */ // The code below you can paste in your theme functions.php or create // new plugin and paste the code there. /** Plugin Name: WPAdverts Snippets - Ads By Author Version: 1.1 Author: Greg Winiarski Description: Links "Posted By" on Ad details page, to page which displays all active ads posted by this author. Note this plugin requires WPAdverts 1.0.5 in order to work. */ // The code below you can paste in your theme functions.php or create // new plugin and paste the code there. add_action( "wp", "ads_by_author_init", 50 ); /** * Registers "Ads By Author" filter and actions. * * It does two things: * - replaces "by John Doe" on Ad details with link to all user Ads * - if current page is default Ads list and GET param "posted_by" is provided * then this function will modify [adverts_list] shortcode to include user * info above Ads list. * * @since 1.0 */ function ads_by_author_init() { add_filter( "adverts_tpl_single_posted_by", "ads_by_author_tpl_single_posted_by", 10, 2 ); if( is_page( adverts_config( 'ads_list_id' ) ) && is_numeric( adverts_request( "posted_by" ) ) ) { remove_shortcode( "adverts_list" ); add_shortcode( "adverts_list", "ads_by_author_list" ); add_filter( "adverts_list_pagination_base", "ads_by_author_pagination_base" ); add_filter( "adverts_list_query", "ads_by_author_query" ); } } /** * Generates HTML for [adverts_list] shortcode * * This function replaces default [adverts_list] callback and * adds author header before the listings. * * @param array $atts Shorcode attributes * @since 1.0 * @return string Fully formatted HTML for adverts list */ function ads_by_author_list( $params = array() ) { $html = ""; $params["search_bar"] = "disabled"; $author = get_user_by( "id", adverts_request( "posted_by" ) ); ob_start(); ?> <style type="text/css"> .ads-by-author.adverts-options { padding: 0.5em 1em; } .ads-by-author.adverts-options .author-logo { float: left; height: 48px; width: 64px; } .ads-by-author.adverts-options .author-logo > img { border-radius: 50%; } .ads-by-author.adverts-options .author-data { line-height: 24px; } </style> <div class="ads-by-author adverts-options"> <div class="author-logo"> <?php echo get_avatar( $author->user_email, 48 ) ?> </div> <div class="author-data"> <strong><?php esc_html_e( $author->display_name ) ?></strong><br/> <?php esc_html_e( date_i18n( get_option( "date_format" ), strtotime( $author->user_registered ) ) ) ?> </div> </div> <?php $html.= ob_get_clean(); $html.= shortcode_adverts_list( $params ); return $html; } /** * Changes pagination base. * * This function adds "posted_by" param to the default * pagination URL so it is possible to paginate over * user submitted ads. * * @see adverts_list_pagination_base filter in wpadverts/includes/shortcodes.php * * @param string $pbase Default pagination base * @since 1.0 * @return string Updated pagination base */ function ads_by_author_pagination_base( $pbase ) { $pbase = get_the_permalink(); $glue = stripos( $pbase, '?' ) ? '&' : '?'; return $pbase . $glue . "posted_by=" . intval( adverts_request( "posted_by" ) ) . '%_%'; } /** * Adds "author" param to [adverts_list] WP_Query. * * This function filters Ads by "author" param (provided in $_GET['posted_by']), * it is applied using "adverts_list_query" filter * * @see adverts_list_query filter in wpadverts/includes/shortcodes.php * * @param string $pbase Default pagination base * @since 1.0 * @return string Updated pagination base */ function ads_by_author_query( $args ) { $args["author"] = intval( adverts_request( "posted_by" ) ); return $args; } /** * Replaces user name with link to all user ads. * * If user who posted an Ad is registered WP user, then the "By John Doe" text * on Ad details page is replaced with link to all user Ads. * * This change is applied using adverts_tpl_single_posted_by filter. * * @see adverts_tpl_single_posted_by filter in wpadverts/templates/single.php * * @param string $pbase Default pagination base * @since 1.0 * @return string Updated pagination base */ function ads_by_author_tpl_single_posted_by( $name, $post_id ) { $post = get_post( $post_id ); $person = get_post_meta($post_id, 'adverts_person', true); if( $post->post_author ) { include_once ADVERTS_PATH . "/includes/class-html.php"; $link = get_permalink( adverts_config( 'ads_list_id' ) ); $glue = stripos( $link, '?' ) ? '&' : '?'; $person = new Adverts_Html( "a", array( "href" => $link . $glue . "posted_by=" . $post->post_author ), $person); } return sprintf( __("by <strong>%s</strong>", "adverts"), $person ); } /** Plugin Name: WPAdverts Snippets - Related Ads Carousel Version: 1.0 Author: Greg Winiarski Description: Shows a list of related Ads on the Ad details page. */ // The code below you can paste in your theme functions.php or create // new plugin and paste the code there. add_action( "adverts_tpl_single_bottom", "related_ads_carousel_tpl_single_bottom", 9000 ); /** * Displays "Related Ads" on the Ad details page. * * This function is executed by adverts_tpl_single_bottom filter. * * @param int $post_id Post ID * @return void */ function related_ads_carousel_tpl_single_bottom( $post_id ) { $postcat = get_the_category( $post_id ); $terms = array(); foreach( $postcat as $cat ) { $terms[] = $cat->term_id; } $args = array( 'post__not_in' => array( $post_id ), 'post_type' => 'advert', 'post_status' => 'publish', 'posts_per_page' => 10, 'paged' => -1, '_tax_query' => array( array( 'taxonomy' => 'advert_category', 'field' => 'term_id', 'terms' => $terms, ), ), ); $loop = new WP_Query( $args ); echo '<h3>' . __( "Related Ads") . '</h3>'; ?> <div class="wpadverts-slick-carousel"> <?php foreach( $loop->posts as $post ): ?> <a href="<?php echo esc_attr( get_permalink( $post->ID ) ) ?>"> <?php $price = get_post_meta( $post->ID, "adverts_price", true ) ?> <?php if( $price ): ?> <span class=""><?php echo esc_html( adverts_get_the_price( get_the_ID(), $price ) ) ?></span> <?php elseif( adverts_config( 'empty_price' ) ): ?> <span class=""><?php echo esc_html( adverts_empty_price( get_the_ID() ) ) ?></span> <?php endif; ?> <?php $image_id = adverts_get_main_image_id( $post->ID ) ?> <div class="advert-img"> <?php if($image_id): ?> <?php $image = get_post( $image_id ) ?> <img src="<?php echo esc_attr( adverts_get_main_image( $post->ID ) ) ?>" class="advert-item-grow" title="<?php echo esc_attr($image->post_excerpt) ?>" alt="<?php echo esc_attr($image->post_content) ?>" /> <?php endif; ?> </div> </a> <?php endforeach; ?> </div> <?php add_action( "wp_footer", "related_ads_carousel_footer", 2000 ); } function related_ads_carousel_footer() { ?> <script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.css"/> <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick-theme.css"/> <script type="text/javascript"> jQuery(document).ready(function(){ jQuery('.wpadverts-slick-carousel').slick({ dots: true, infinite: false, speed: 300, slidesToShow: 4, slidesToScroll: 4 }); }); </script> <style type="text/css"> </style> <?php }
The topic ‘Adding snippets’ is closed to new replies.