I have a website that I'm trying to populate with links. Right now, if you look at the website from the computer, you'll see that there's a floating box with text, then dots all over the map. If you hover over one of the dots, it will have the name of the site, along with the time period associated.
http://jeremynative.com/onthissite/
<?php
$i = 0;
while ( have_posts() ) : the_post();
$et_location_lat = get_post_meta( get_the_ID(), '_et_listing_lat', true );
$et_location_lng = get_post_meta( get_the_ID(), '_et_listing_lng', true );
$et_location_rating = '<div class="location-rating"></div>';
if ( ( $et_rating = et_get_rating() ) && 0 != $et_rating )
$et_location_rating = '<div class="location-rating"><span class="et-rating"><span style="' . sprintf( 'width: %dpx;', esc_attr( $et_rating * 17 ) ) . '"></span></span></div>';
if ( '' != $et_location_lat && '' != $et_location_lng ) {
?>
et_add_marker( <?php printf( '%1$d, %2$s, %3$s, \'<div id="et_marker_%1$d" class="et_marker_info"><div class="location-description"> <div class="location-title"> <h2>%4$s </h2> <div class="listing-info"><p>%5$s</p></div> </div> ' . $et_location_rating . ' </div> <!-- .location-description --> </div> <!-- .et_marker_info -->\'',
$i,
esc_html( $et_location_lat ),
esc_html( $et_location_lng ),
get_the_title(),
wp_strip_all_tags( addslashes( get_the_term_list( get_the_ID(), 'listing_type', '', ', ' ) ) )
); ?> );
<?php
}
$i++;
endwhile;
rewind_posts();
?>
Try with
<?php
$i = 0;
while ( have_posts() ) : the_post();
$et_location_lat = get_post_meta( get_the_ID(), '_et_listing_lat', true );
$et_location_lng = get_post_meta( get_the_ID(), '_et_listing_lng', true );
$et_location_rating = '<div class="location-rating"></div>';
if ( ( $et_rating = et_get_rating() ) && 0 != $et_rating )
$et_location_rating = '<div class="location-rating"><span class="et-rating"><span style="' . sprintf( 'width: %dpx;', esc_attr( $et_rating * 17 ) ) . '"></span></span></div>';
if ( '' != $et_location_lat && '' != $et_location_lng ) {
?>
et_add_marker( <?php printf( '%1$d, %2$s, %3$s, \'<div id="et_marker_%1$d" class="et_marker_info"><div class="location-description"> <div class="location-title"><h2>%4$s</h2><div class="listing-info"><p>%5$s</p></div> </div> ' . $et_location_rating . ' </div> <!-- .location-description --> </div> <!-- .et_marker_info -->\'',
$i,
esc_html( $et_location_lat ),
esc_html( $et_location_lng ),
get_the_title(),
wp_strip_all_tags( addslashes( get_the_term_list( get_the_ID(), 'listing_type', '', ', ' ) ) ),
get_the_permalink()
); ?> );
<?php
}
$i++;
endwhile;
rewind_posts();
?>
I added the link inside the title and then used get_the_permalink() function to pull the permalink in.
Related
I wanted to enable Gutenberg for My Single Product Description so I followed this guide
https://dev.to/kalimahapps/enable-gutenberg-edit
function activate_gutenberg_product( $can_edit, $post_type ) {
if ( $post_type == 'product' ) {
$can_edit = true;
}
return $can_edit;
}
add_filter( 'use_block_editor_for_post_type', 'activate_gutenberg_product', 10, 2 );
function enable_taxonomy_rest( $args ) {
$args['show_in_rest'] = true;
return $args;
}
add_filter( 'woocommerce_taxonomy_args_product_cat', 'enable_taxonomy_rest' );
add_filter( 'woocommerce_taxonomy_args_product_tag', 'enable_taxonomy_rest' );
function register_catalog_meta_boxes() {
global $current_screen;
// Make sure gutenberg is loaded before adding the metabox
if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
add_meta_box( 'catalog-visibility', __( 'Catalog visibility', 'textdomain' ), 'product_data_visibility', 'product', 'side' );
}
}
add_action( 'add_meta_boxes', 'register_catalog_meta_boxes' );
function product_data_visibility( $post ) {
$thepostid = $post->ID;
$product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product();
$current_visibility = $product_object->get_catalog_visibility();
$current_featured = wc_bool_to_string( $product_object->get_featured() );
$visibility_options = wc_get_product_visibility_options();
?>
<div class="misc-pub-section" id="catalog-visibility">
<?php esc_html_e( 'Catalog visibility:', 'woocommerce' ); ?>
<strong id="catalog-visibility-display">
<?php
echo isset( $visibility_options[ $current_visibility ] ) ? esc_html( $visibility_options[ $current_visibility ] ) : esc_html( $current_visibility );
if ( 'yes' === $current_featured ) {
echo ', ' . esc_html__( 'Featured', 'woocommerce' );
}
?>
</strong>
<a href="#catalog-visibility"
class="edit-catalog-visibility hide-if-no-js"><?php esc_html_e( 'Edit', 'woocommerce' ); ?></a>
<div id="catalog-visibility-select" class="hide-if-js">
<input type="hidden" name="current_visibility" id="current_visibility"
value="<?php echo esc_attr( $current_visibility ); ?>" />
<input type="hidden" name="current_featured" id="current_featured"
value="<?php echo esc_attr( $current_featured ); ?>" />
<?php
echo '<p>' . esc_html__( 'This setting determines which shop pages products will be listed on.', 'woocommerce' ) . '</p>';
foreach ( $visibility_options as $name => $label ) {
echo '<input type="radio" name="_visibility" id="_visibility_' . esc_attr( $name ) . '" value="' . esc_attr( $name ) . '" ' . checked( $current_visibility, $name, false ) . ' data-label="' . esc_attr( $label ) . '" /> <label for="_visibility_' . esc_attr( $name ) . '" class="selectit">' . esc_html( $label ) . '</label><br />';
}
echo '<br /><input type="checkbox" name="_featured" id="_featured" ' . checked( $current_featured, 'yes', false ) . ' /> <label for="_featured">' . esc_html__( 'This is a featured product', 'woocommerce' ) . '</label><br />';
?>
<p>
<a href="#catalog-visibility"
class="save-post-visibility hide-if-no-js button"><?php esc_html_e( 'OK', 'woocommerce' ); ?></a>
<a href="#catalog-visibility"
class="cancel-post-visibility hide-if-no-js"><?php esc_html_e( 'Cancel', 'woocommerce' ); ?></a>
</p>
</div>
</div>
<?php
}
But I am getting issues with server Overload after adding this to my function.php With this Error while trying to publish anything. - "Publishing failed. The response is not a valid JSON response".
Anyone Knows the reason...I really need help
I added a custom download link for my resellers to download product images. But I would like to add target="_blank" rel="noopener noreferrer" attributes to the link.
Here's my custom download link:
// Add a Custom Download File to My Account
add_filter( 'woocommerce_customer_get_downloadable_products', 'my_custom_default_download', 9999, 1 );
function my_custom_default_download( $downloads ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
// Only on checkout page and logged in users
if ( ! is_wc_endpoint_url() || ! is_user_logged_in() )
return;
$wpuser_object = wp_get_current_user();
$allowed_roles = array( 'administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'wholesaler-ex-vat-registered', 'wholesaler-ex-non-vat-registered', 'shop_manager');
if ( array_intersect($allowed_roles, $wpuser_object->roles) ){
$downloads[] = array(
'product_name' => 'Product Images (High Resolution)',
'download_name' => 'Download',
'download_url' => 'https://drive.google.com/drive/folders/',
);
return $downloads;
}
}
Here's the updated my-downloads.php file saved to my child theme:
<?php
defined( 'ABSPATH' ) || exit;
$downloads = WC()->customer->get_downloadable_products();
if ( $downloads ) : ?>
<?php do_action( 'woocommerce_before_available_downloads' ); ?>
<h2><?php echo apply_filters( 'woocommerce_my_account_my_downloads_title', esc_html__( 'Available downloads', 'woocommerce' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></h2>
<ul class="woocommerce-Downloads digital-downloads">
<?php foreach ( $downloads as $download ) : ?>
<li>
<?php
do_action( 'woocommerce_available_download_start', $download );
if ( is_numeric( $download['downloads_remaining'] ) ) {
/* translators: %s product name */
echo apply_filters( 'woocommerce_available_download_count', '<span class="woocommerce-Count count">' . sprintf( _n( '%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce' ), $download['downloads_remaining'] ) . '</span> ', $download ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo apply_filters( 'woocommerce_available_download_link', '' . $download['download_name'] . '', $download ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
do_action( 'woocommerce_available_download_end', $download );
?>
</li>
<?php endforeach; ?>
</ul>
<?php do_action( 'woocommerce_after_available_downloads' ); ?>
<?php endif; ?>
You've got to update the order-downloads.php file not the my-downloads.php file and save it to the child theme.
Here's the solution:
<?php
/**
* Order Downloads.
*
* This template can be overridden by copying it to yourtheme/woocommerce/order/order-downloads.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<section class="woocommerce-order-downloads">
<?php if ( isset( $show_title ) ) : ?>
<h2 class="woocommerce-order-downloads__title"><?php esc_html_e( 'Downloads', 'woocommerce' ); ?></h2>
<?php endif; ?>
<table class="woocommerce-table woocommerce-table--order-downloads shop_table shop_table_responsive order_details">
<thead>
<tr>
<?php foreach ( wc_get_account_downloads_columns() as $column_id => $column_name ) : ?>
<th class="<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<?php foreach ( $downloads as $download ) : ?>
<tr>
<?php foreach ( wc_get_account_downloads_columns() as $column_id => $column_name ) : ?>
<td class="<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php
if ( has_action( 'woocommerce_account_downloads_column_' . $column_id ) ) {
do_action( 'woocommerce_account_downloads_column_' . $column_id, $download );
} else {
switch ( $column_id ) {
case 'download-product':
if ( $download['product_url'] ) {
echo '' . esc_html( $download['product_name'] ) . '';
} else {
echo esc_html( $download['product_name'] );
}
break;
case 'download-file':
echo '' . esc_html( $download['download_name'] ) . '';
break;
case 'download-remaining':
echo is_numeric( $download['downloads_remaining'] ) ? esc_html( $download['downloads_remaining'] ) : esc_html__( '∞', 'woocommerce' );
break;
case 'download-expires':
if ( ! empty( $download['access_expires'] ) ) {
echo '<time datetime="' . esc_attr( date( 'Y-m-d', strtotime( $download['access_expires'] ) ) ) . '" title="' . esc_attr( strtotime( $download['access_expires'] ) ) . '">' . esc_html( date_i18n( get_option( 'date_format' ), strtotime( $download['access_expires'] ) ) ) . '</time>';
} else {
esc_html_e( 'Never', 'woocommerce' );
}
break;
}
}
?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
</section>
I am using the Avada Wordpress theme. I'd like to switch out the logo depending on the page. I found this thread on stackoverflow and found it very useful
The latest Avada theme has different coding for standard, mobile and sticky header logos. On top of that, each has a retina option. That is where I am having the problem. It is not clear to me how to allow this for the retina options, too. The coding in the logo.php file is different than in the example I sited.
You'll see the section. The "standard" string works, but not the mobile, sticky, or retina. Any thoughts? Here's the full logo.pho code I am using.
<?php
/**
* Logo template.
*
* #author ThemeFusion
* #copyright (c) Copyright by ThemeFusion
* #link http://theme-fusion.com
* #package Avada
* #subpackage Core
*/
// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Direct script access denied.' );
}
$logo_opening_markup = '<div class="';
$logo_closing_markup = '</div>';
if ( 'v7' === Avada()->settings->get( 'header_layout' ) && ! Avada()->settings->get( 'logo_background' ) ) {
$logo_opening_markup = '<li class="fusion-middle-logo-menu-logo ';
$logo_closing_markup = '</li>';
} elseif ( 'v7' === Avada()->settings->get( 'header_layout' ) && Avada()->settings->get( 'logo_background' ) && 'Top' === Avada()->settings->get( 'header_position' ) ) {
$logo_opening_markup = '<li class="fusion-logo-background fusion-middle-logo-menu-logo"><div class="';
$logo_closing_markup = '</div></li>';
} elseif ( Avada()->settings->get( 'logo_background' ) && 'v4' !== Avada()->settings->get( 'header_layout' ) && 'v5' !== Avada()->settings->get( 'header_layout' ) && 'Top' === Avada()->settings->get( 'header_position' ) ) {
$logo_opening_markup = '<div class="fusion-logo-background"><div class="';
$logo_closing_markup = '</div></div>';
}
?>
<?php if ( '' !== Avada()->settings->get( 'logo', 'url' ) || '' !== Avada()->settings->get( 'logo_retina', 'url' ) ) : ?>
<?php echo $logo_opening_markup; // WPCS: XSS ok. ?>fusion-logo" data-margin-top="<?php echo esc_attr( Avada()->settings->get( 'logo_margin', 'top' ) ); ?>" data-margin-bottom="<?php echo esc_attr( Avada()->settings->get( 'logo_margin', 'bottom' ) ); ?>" data-margin-left="<?php echo esc_attr( Avada()->settings->get( 'logo_margin', 'left' ) ); ?>" data-margin-right="<?php echo esc_attr( Avada()->settings->get( 'logo_margin', 'right' ) ); ?>">
<?php elseif ( 'v7' !== Avada()->settings->get( 'header_layout' ) ) : ?>
<?php echo $logo_opening_markup; // WPCS: XSS ok. ?>fusion-logo" data-margin-top="0px" data-margin-bottom="0px" data-margin-left="0px" data-margin-right="0px">
<?php endif; ?>
<?php
/**
* The avada_logo_prepend hook.
*/
do_action( 'avada_logo_prepend' );
$logo_anchor_tag_attributes = '';
$logo_anchor_tag_attributes_array = apply_filters(
'avada_logo_anchor_tag_attributes',
array(
'class' => 'fusion-logo-link',
'href' => ( $custom_link = Avada()->settings->get( 'logo_custom_link' ) ) ? esc_url( $custom_link ) : esc_url( home_url( '/' ) ),
)
);
foreach ( $logo_anchor_tag_attributes_array as $attribute => $value ) {
if ( 'href' === $attribute ) {
$value = esc_url( $value );
} else {
$value = esc_attr( $value );
}
$logo_anchor_tag_attributes .= ' ' . $attribute . '="' . $value . '" ';
}
$logo_alt_attribute = apply_filters( 'avada_logo_alt_tag', get_bloginfo( 'name', 'display' ) . ' ' . __( 'Logo', 'Avada' ) );
?>
<?php if ( ( Avada()->settings->get( 'logo', 'url' ) && '' !== Avada()->settings->get( 'logo', 'url' ) ) || ( Avada()->settings->get( 'logo_retina', 'url' ) && '' !== Avada()->settings->get( 'logo_retina', 'url' ) ) ) : ?>
<a<?php echo $logo_anchor_tag_attributes; // WPCS: XSS ok. ?>>
<?php $standard_logo = Avada()->images->get_logo_image_srcset( 'logo', 'logo_retina' ); ?>
<!-- custom logo -->
<?php
//Movie
if (is_page( 'movie' ))
{
$standard_logo['srcset'] = '/wp-content/uploads/2018/08/SRSM-Logo-Default-01.png';
$standard_logo['url'] = $standard_logo['srcset'];
$retina_logo['srcset'] = '/wp-content/uploads/2018/08/SRSM-Logo-Retina-01.png';
$retina_logo['url'] = $retina_logo['srcset'];
$mobile_logo['srcset'] = '/wp-content/uploads/2018/08/SRSM-Logo-Mobile-01.png';
$mobile_logo['url'] = $mobile_logo['srcset'];
$sticky_logo['srcset'] = '/wp-content/uploads/2018/08/SRSM-Logo-Sticky-01.png';
$sticky_logo['url'] = $sticky_logo['srcset'];
}
?>
<!-- standard logo -->
<img src="<?php echo esc_url_raw( $standard_logo['url'] ); ?>" srcset="<?php echo esc_attr( $standard_logo['srcset'] ); ?>" width="<?php echo esc_attr( $standard_logo['width'] ); ?>" height="<?php echo esc_attr( $standard_logo['height'] ); ?>"<?php echo $standard_logo['style']; // WPCS: XSS ok. ?> alt="<?php echo esc_attr( $logo_alt_attribute ); ?>" retina_logo_url="<?php echo esc_url_raw( $standard_logo['is_retina'] ); ?>" class="fusion-standard-logo" />
<?php
if ( Avada()->settings->get( 'mobile_logo', 'url' ) && '' !== Avada()->settings->get( 'mobile_logo', 'url' ) ) {
$mobile_logo = Avada()->images->get_logo_image_srcset( 'mobile_logo', 'mobile_logo_retina' );
?>
<!-- mobile logo -->
<img src="<?php echo esc_url_raw( $mobile_logo['url'] ); ?>" srcset="<?php echo esc_attr( $mobile_logo['srcset'] ); ?>" width="<?php echo esc_attr( $mobile_logo['width'] ); ?>" height="<?php echo esc_attr( $mobile_logo['height'] ); ?>"<?php echo $mobile_logo['style']; // WPCS: XSS ok. ?> alt="<?php echo esc_attr( $logo_alt_attribute ); ?>" retina_logo_url="<?php echo esc_url_raw( $mobile_logo['is_retina'] ); ?>" class="fusion-mobile-logo" />
<?php } ?>
<?php
if ( Avada()->settings->get( 'sticky_header_logo', 'url' ) && '' !== Avada()->settings->get( 'sticky_header_logo', 'url' ) && ( in_array( Avada()->settings->get( 'header_layout' ), array( 'v1', 'v2', 'v3', 'v6', 'v7' ) ) || ( ( in_array( Avada()->settings->get( 'header_layout' ), array( 'v4', 'v5' ) ) && 'menu_and_logo' === Avada()->settings->get( 'header_sticky_type2_layout' ) ) ) ) ) {
$sticky_logo = Avada()->images->get_logo_image_srcset( 'sticky_header_logo', 'sticky_header_logo_retina' );
?>
<!-- sticky header logo -->
<img src="<?php echo esc_url_raw( $sticky_logo['url'] ); ?>" srcset="<?php echo esc_attr( $sticky_logo['srcset'] ); ?>" width="<?php echo esc_attr( $sticky_logo['width'] ); ?>" height="<?php echo esc_attr( $sticky_logo['height'] ); ?>"<?php echo $sticky_logo['style']; // WPCS: XSS ok. ?> alt="<?php echo esc_attr( $logo_alt_attribute ); ?>" retina_logo_url="<?php echo esc_url_raw( $sticky_logo['is_retina'] ); ?>" class="fusion-sticky-logo" />
<?php } ?>
</a>
<?php endif; ?>
<?php
/**
* The avada_logo_append hook.
*
* #hooked avada_header_content_3 - 10.
*/
do_action( 'avada_logo_append' );
?>
<?php
echo $logo_closing_markup; // WPCS: XSS ok.
/* Omit closing PHP tag to avoid "Headers already sent" issues. */
how to query woocommerce shipping method tax price ?
Porblem i don't tax + prive query!
Price query:
<?php if($method->cost > 0) : ?>+<span style="font-weight: normal"><?= $method->cost ?> Ft</span><?php endif; ?>
</label>
</div>
My code is:
<h2>Átvételi mód</h2>
<?php
$packages = WC()->shipping->get_packages();
$first = true;
foreach ($packages as $i => $package) {
$chosen_method = isset(WC()->session->chosen_shipping_methods[$i]) ? WC()->session->chosen_shipping_methods[$i] : '';
$product_names = array();
if (sizeof($packages) > 1) {
foreach ($package['contents'] as $item_id => $values) {
$product_names[$item_id] = $values['data']->get_name() . ' ×' . $values['quantity'];
}
$product_names = apply_filters('woocommerce_shipping_package_details_array', $product_names, $package);
}
/** #var WC_Shipping_Rate[] $available_methods */
$available_methods = $package['rates'];
$show_package_details = sizeof( $packages ) > 1;
$package_name = apply_filters( 'woocommerce_shipping_package_name', sprintf( _nx( 'Shipping', 'Shipping %d', ( $i + 1 ), 'shipping packages', 'woocommerce' ), ( $i + 1 ) ), $i, $package );
?>
<div class="shipping atvetelimod">
<td data-title="<?php echo esc_attr( $package_name ); ?>">
<?php if ( 1 < count( $available_methods ) ) : ?>
<div id="shipping_method row">
<?php
foreach ( $available_methods as $method ) : ?>
<div class="col-lg-6 balraszoveg">
<div class="jobbelvalaszto">
<label class="checkjel">
<input class="legyenelottefeher" type="radio" name="shipping_method[<?= $i ?>]" data-index="<?= $i ?>" id="shipping_method_<?= $i ?>_<?= sanitize_title( $method->id ) ?>" value="<?= esc_attr( $method->id ) ?>" class="shipping_method" <?= checked( $method->id, $chosen_method, false ) ?> />
</label>
</div>
<label class="jobboldaliszoveg" for="shipping_method_<?= $i ?>_<?= sanitize_title( $method->id ) ?>">
<strong><?= $method->get_label() ?></strong><br />
<?php if($method->cost > 0) : ?>+<span style="font-weight: normal"><?= $method->cost ?> Ft</span><?php endif; ?>
</label>
</div>
<?php endforeach; ?>
</div>
<?php elseif ( 1 === count( $available_methods ) ) : ?>
<?php
$method = current( $available_methods );
printf( '%3$s <input type="hidden" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d" value="%2$s" class="shipping_method" />', $index, esc_attr( $method->id ), wc_cart_totals_shipping_method_label( $method ) );
do_action( 'woocommerce_after_shipping_rate', $method, $index );
?>
<?php elseif ( WC()->customer->has_calculated_shipping() ) : ?>
<?php echo apply_filters( is_cart() ? 'woocommerce_cart_no_shipping_available_html' : 'woocommerce_no_shipping_available_html', wpautop( __( 'There are no shipping methods available. Please ensure that your address has been entered correctly, or contact us if you need any help.', 'woocommerce' ) ) ); ?>
<?php elseif ( ! is_cart() ) : ?>
<?php echo wpautop( __( 'Enter your full address to see shipping costs.', 'woocommerce' ) ); ?>
<?php endif; ?>
<?php if ( $show_package_details ) : ?>
<?php echo '<p class="woocommerce-shipping-contents"><small>' . esc_html( $package_details ) . '</small></p>'; ?>
<?php endif; ?>
<?php if ( ! empty( $show_shipping_calculator ) ) : ?>
<?php woocommerce_shipping_calculator(); ?>
<?php endif; ?>
</td>
</div>
<?php }; ?>
I need to customise default design of related articles from jet pack in wordpress to my custom design i am unable to find the error where it was wrong in my code. I had written a hook in functions.php file.
This is my hook in functions.php
function jetpackme_custom_related() {
$posts = '<div class="single-article-popularGi">';
$posts .= '<h1>related</h1><div class="row">';
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) {
$related = Jetpack_RelatedPosts::init_raw()
->get_for_post_id(
get_the_ID(),
array( 'size' => 2 )
);
if ( $related ) {
foreach ( $related as $result ) {
// Get the related post IDs
$title = get_the_title( $result[ 'id' ] );
$link = get_permalink( $result[ 'id' ] );
$image = featuredOrFirstImage($result[ 'id' ], 'blog-post-image');
$category_name = get_the_category( $result[ 'id' ] );
$posts .= '<div class="col-sm-3 col-3"><span class="thumbnail-image">'.$image.'</span></div>';
$posts .= '<div class="post-details col-sm-3 col-3 align-self-center"><div class="entry-cat post-category">'.$category_name.'</div>';
$posts .= '<div class="mostPopularTitle"><h2 class="entry-title"><a class="post-title" href="%s" rel="bookmark">'.$title.'</a></h2>';
$posts .= '<div class="readArticle"><a class="readMorePost" href="'.$link.'">MORE GI></a>';
}
}
}
$posts .= '</div>';
$posts .= '</div>';
// Return a list of post titles separated by commas
if( $related ){
return $posts;
}else{
return false;
}
}
add_action('admin_init', 'jetpackme_custom_related');
I had called it in single.php like this
<?php echo $related = jetpackme_custom_related();?>
But there is nothing displayed and getting error some times which is not displaying nothing. Can anyone Please let me solve it out..
I had changed the way of procedure and it works for me.My code is
$related_posts = array();
$query = array();
$query['showposts'] = 2;// Number of posts to show
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) :
$related = Jetpack_RelatedPosts::init_raw()
->set_query_name( 'theme-custom' ) // optional, name can be anything
->get_for_post_id( get_the_ID(), array( 'size' => $query['showposts'] )
);
if ( $related ) :
foreach ( $related as $result ) :
$related_posts[] = $result[ 'id' ];
endforeach;
endif;
endif;
if ( $related_posts ) {
$query['post__in'] = $related_posts;
$query['orderby'] = 'post__in';
$title = __( 'Related', 'prefix' );
} else {
$query['post__not_in'] = array( $post->ID );
$title = __( 'Related', 'prefix' );
}
I had called the related articles like..
<div class="single-article-popularGi">
<h1><?php esc_attr_e( $title ); ?></h1>
<div class="row">
<?php $related = new WP_Query( $query ); ?>
<?php while ( $related->have_posts() ) : $related->the_post(); ?>
<div class="col-sm-3 col-3">
<div class="thumbnail-image">
<span class="thumb-wrap"><?php the_post_thumbnail('medium', array("class" => "img-fluid")); ?></span>
</div>
</div>
<div class="post-details col-sm-3 col-3 align-self-center">
<?php if ( has_category() ) : ?>
<div class="entry-cat post-category">
<?php the_category(' '); ?>
</div><!-- end .entry-cats -->
<?php endif; // has_category() ?>
<div class="mostPopularTitle">
<?php the_title( sprintf( '<h2 class="entry-title"><a class="post-title" href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
</div>
<i class="postIntro"><?php echo $intro = wp_trim_words( get_field('intro' ), 20,'...' ); ?></i>
<div class="readArticle">
<a class="readMorePost" href="<?php echo get_permalink(); ?>">MORE GI></a>
</div>
</div>
<?php endwhile;
wp_reset_query(); ?>
</div>