如何过滤 Genesis Studiopress AgentPress 列表插件中的循环?
How to Filter Loop In Genesis Studiopress AgentPress Listings Plugin?
我需要帮助创建过滤器以将下面代码第 84 行的 "View Listings" 更改为 "Book Now"。
我需要帮助为子主题 functions.php 文件制作自定义过滤器,这样它就不会在插件更新时不断变回原样。您可以在此处查看该站点:https://webclient.co/running - 该循环位于 运行 游览的主页上。
<?php
class AgentPress_Featured_Listings_Widget extends WP_Widget {
function __construct() {
$widget_ops = array( 'classname' => 'featured-listings', 'description' => __( 'Display grid-style featured listings', 'agentpress-listings' ) );
$control_ops = array( 'width' => 300, 'height' => 350 );
parent::__construct( 'featured-listings', __( 'AgentPress - Featured Listings', 'agentpress-listings' ), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
/** defaults */
$instance = wp_parse_args( $instance, array(
'title' => '',
'posts_per_page' => 10
) );
extract( $args );
echo $before_widget;
if ( ! empty( $instance['title'] ) ) {
echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
}
$toggle = ''; /** for left/right class */
$query_args = array(
'post_type' => 'listing',
'posts_per_page' => $instance['posts_per_page'],
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
);
query_posts( $query_args );
if ( have_posts() ) : while ( have_posts() ) : the_post();
//* initialze the $loop variable
$loop = '';
//* Pull all the listing information
$custom_text = genesis_get_custom_field( '_listing_text' );
$price = genesis_get_custom_field( '_listing_price' );
$address = genesis_get_custom_field( '_listing_address' );
$city = genesis_get_custom_field( '_listing_city' );
$state = genesis_get_custom_field( '_listing_state' );
$zip = genesis_get_custom_field( '_listing_zip' );
$loop .= sprintf( '<a href="%s">%s</a>', get_permalink(), genesis_get_image( array( 'size' => 'properties' ) ) );
if ( $price ) {
$loop .= sprintf( '<span class="listing-price">%s</span>', $price );
}
if ( strlen( $custom_text ) ) {
$loop .= sprintf( '<span class="listing-text">%s</span>', esc_html( $custom_text ) );
}
if ( $address ) {
$loop .= sprintf( '<span class="listing-address">%s</span>', $address );
}
if ( $city || $state || $zip ) {
//* count number of completed fields
$pass = count( array_filter( array( $city, $state, $zip ) ) );
//* If only 1 field filled out, no comma
if ( 1 == $pass ) {
$city_state_zip = $city . $state . $zip;
}
//* If city filled out, comma after city
elseif ( $city ) {
$city_state_zip = $city . ", " . $state . " " . $zip;
}
//* Otherwise, comma after state
else {
$city_state_zip = $city . " " . $state . ", " . $zip;
}
$loop .= sprintf( '<span class="listing-city-state-zip">%s</span>', trim( $city_state_zip ) );
}
$loop .= sprintf( '<a href="%s" class="more-link">%s</a>', get_permalink(), __( 'View Listings', 'agentpress-listings' ) );
$toggle = $toggle == 'left' ? 'right' : 'left';
/** wrap in post class div, and output **/
printf( '<div class="%s"><div class="widget-wrap"><div class="listing-wrap">%s</div></div></div>', join( ' ', get_post_class( $toggle ) ), apply_filters( 'agentpress_featured_listings_widget_loop', $loop ) );
endwhile; endif;
wp_reset_query();
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
return $new_instance;
}
function form( $instance ) {
$instance = wp_parse_args( $instance, array(
'title' => '',
'posts_per_page' => 10
) );
printf( '<p><label for="%s">%s</label><input type="text" id="%s" name="%s" value="%s" style="%s" /></p>', $this->get_field_id('title'), __( 'Title:', 'agentpress-listings' ), $this->get_field_id('title'), $this->get_field_name('title'), esc_attr( $instance['title'] ), 'width: 95%;' );
printf( '<p>%s <input type="text" name="%s" value="%s" size="3" /></p>', __( 'How many results should be returned?', 'agentpress-listings' ), $this->get_field_name('posts_per_page'), esc_attr( $instance['posts_per_page'] ) );
}
}
插件有过滤器'agentpress_featured_listings_widget_loop',所以应该这样做。它进入你的 functions.php 文件或你的函数插件。
参考:https://developer.wordpress.org/reference/functions/add_filter/
参考:http://www.studiopress.com/forums/topic/agent-press-listing/#post-122180
/**
* Filter the loop output of the AgentPress Featured Listings Widget.
*/
function agentpress_featured_listings_widget_loop_filter( $loop ) {
$loop = ''; /** initialze the $loop variable */
$loop .= sprintf( '<a href="%s">%s</a>', get_permalink(), genesis_get_image( array( 'size' => 'properties' ) ) );
$loop .= sprintf( '<span class="entry-title">' . ( get_the_title() ) . '</span>' );
$loop .= sprintf( '<span class="listing-price">%s</span>', genesis_get_custom_field('_listing_price') );
$custom_text = genesis_get_custom_field( '_listing_text' );
if( strlen( $custom_text ) )
$loop .= sprintf( '<span class="listing-text">%s</span>', esc_html( $custom_text ) );
$loop .= sprintf( '<span class="listing-address">%s</span>', genesis_get_custom_field('_listing_address') );
$loop .= sprintf( '<span class="listing-city-state-zip">%s %s, %s</span>', genesis_get_custom_field('_listing_city'), genesis_get_custom_field('_listing_state'), genesis_get_custom_field('_listing_zip') );
// Here is where you change the text:
$loop .= sprintf( '<a href="%s" class="more-link">%s</a>', get_permalink(), __( 'Book Now', 'yourtextdomain' ) );
return $loop;
}
add_filter( 'agentpress_featured_listings_widget_loop', 'agentpress_featured_listings_widget_loop_filter', 10, 1 );
我需要帮助创建过滤器以将下面代码第 84 行的 "View Listings" 更改为 "Book Now"。
我需要帮助为子主题 functions.php 文件制作自定义过滤器,这样它就不会在插件更新时不断变回原样。您可以在此处查看该站点:https://webclient.co/running - 该循环位于 运行 游览的主页上。
<?php
class AgentPress_Featured_Listings_Widget extends WP_Widget {
function __construct() {
$widget_ops = array( 'classname' => 'featured-listings', 'description' => __( 'Display grid-style featured listings', 'agentpress-listings' ) );
$control_ops = array( 'width' => 300, 'height' => 350 );
parent::__construct( 'featured-listings', __( 'AgentPress - Featured Listings', 'agentpress-listings' ), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
/** defaults */
$instance = wp_parse_args( $instance, array(
'title' => '',
'posts_per_page' => 10
) );
extract( $args );
echo $before_widget;
if ( ! empty( $instance['title'] ) ) {
echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
}
$toggle = ''; /** for left/right class */
$query_args = array(
'post_type' => 'listing',
'posts_per_page' => $instance['posts_per_page'],
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
);
query_posts( $query_args );
if ( have_posts() ) : while ( have_posts() ) : the_post();
//* initialze the $loop variable
$loop = '';
//* Pull all the listing information
$custom_text = genesis_get_custom_field( '_listing_text' );
$price = genesis_get_custom_field( '_listing_price' );
$address = genesis_get_custom_field( '_listing_address' );
$city = genesis_get_custom_field( '_listing_city' );
$state = genesis_get_custom_field( '_listing_state' );
$zip = genesis_get_custom_field( '_listing_zip' );
$loop .= sprintf( '<a href="%s">%s</a>', get_permalink(), genesis_get_image( array( 'size' => 'properties' ) ) );
if ( $price ) {
$loop .= sprintf( '<span class="listing-price">%s</span>', $price );
}
if ( strlen( $custom_text ) ) {
$loop .= sprintf( '<span class="listing-text">%s</span>', esc_html( $custom_text ) );
}
if ( $address ) {
$loop .= sprintf( '<span class="listing-address">%s</span>', $address );
}
if ( $city || $state || $zip ) {
//* count number of completed fields
$pass = count( array_filter( array( $city, $state, $zip ) ) );
//* If only 1 field filled out, no comma
if ( 1 == $pass ) {
$city_state_zip = $city . $state . $zip;
}
//* If city filled out, comma after city
elseif ( $city ) {
$city_state_zip = $city . ", " . $state . " " . $zip;
}
//* Otherwise, comma after state
else {
$city_state_zip = $city . " " . $state . ", " . $zip;
}
$loop .= sprintf( '<span class="listing-city-state-zip">%s</span>', trim( $city_state_zip ) );
}
$loop .= sprintf( '<a href="%s" class="more-link">%s</a>', get_permalink(), __( 'View Listings', 'agentpress-listings' ) );
$toggle = $toggle == 'left' ? 'right' : 'left';
/** wrap in post class div, and output **/
printf( '<div class="%s"><div class="widget-wrap"><div class="listing-wrap">%s</div></div></div>', join( ' ', get_post_class( $toggle ) ), apply_filters( 'agentpress_featured_listings_widget_loop', $loop ) );
endwhile; endif;
wp_reset_query();
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
return $new_instance;
}
function form( $instance ) {
$instance = wp_parse_args( $instance, array(
'title' => '',
'posts_per_page' => 10
) );
printf( '<p><label for="%s">%s</label><input type="text" id="%s" name="%s" value="%s" style="%s" /></p>', $this->get_field_id('title'), __( 'Title:', 'agentpress-listings' ), $this->get_field_id('title'), $this->get_field_name('title'), esc_attr( $instance['title'] ), 'width: 95%;' );
printf( '<p>%s <input type="text" name="%s" value="%s" size="3" /></p>', __( 'How many results should be returned?', 'agentpress-listings' ), $this->get_field_name('posts_per_page'), esc_attr( $instance['posts_per_page'] ) );
}
}
插件有过滤器'agentpress_featured_listings_widget_loop',所以应该这样做。它进入你的 functions.php 文件或你的函数插件。
参考:https://developer.wordpress.org/reference/functions/add_filter/
参考:http://www.studiopress.com/forums/topic/agent-press-listing/#post-122180
/**
* Filter the loop output of the AgentPress Featured Listings Widget.
*/
function agentpress_featured_listings_widget_loop_filter( $loop ) {
$loop = ''; /** initialze the $loop variable */
$loop .= sprintf( '<a href="%s">%s</a>', get_permalink(), genesis_get_image( array( 'size' => 'properties' ) ) );
$loop .= sprintf( '<span class="entry-title">' . ( get_the_title() ) . '</span>' );
$loop .= sprintf( '<span class="listing-price">%s</span>', genesis_get_custom_field('_listing_price') );
$custom_text = genesis_get_custom_field( '_listing_text' );
if( strlen( $custom_text ) )
$loop .= sprintf( '<span class="listing-text">%s</span>', esc_html( $custom_text ) );
$loop .= sprintf( '<span class="listing-address">%s</span>', genesis_get_custom_field('_listing_address') );
$loop .= sprintf( '<span class="listing-city-state-zip">%s %s, %s</span>', genesis_get_custom_field('_listing_city'), genesis_get_custom_field('_listing_state'), genesis_get_custom_field('_listing_zip') );
// Here is where you change the text:
$loop .= sprintf( '<a href="%s" class="more-link">%s</a>', get_permalink(), __( 'Book Now', 'yourtextdomain' ) );
return $loop;
}
add_filter( 'agentpress_featured_listings_widget_loop', 'agentpress_featured_listings_widget_loop_filter', 10, 1 );