Wordpress - How to display 10 users only? - php

Sorry I'm newbie with Wordpress, How to display 10 users only and the others inside "see more".
I searched more times before asking but with nothing!
Thanks in advance.
<?php
$post_id = get_the_ID();
$users_liked = get_post_meta( $post_id, '_user_liked', true );
if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>
<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) :
$user = get_user_by( 'id', $user_id );
if ( false === $user ) {
continue;
}
?>
<div class="sort-list-name">
<a href="<?php echo get_author_posts_url($user->ID);?>">
<?php echo get_avatar( $user->ID, 35 ); ?>
</a>
</div>
<?php
endforeach; ?>
<?php } ?>

first off all try to add an index in your loop
<?php
$post_id = get_the_ID();
$i = 0;
$users_liked = get_post_meta( $post_id, '_user_liked', true );
if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>
<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) :
$user = get_user_by( 'id', $user_id );
if ( false === $user ) {
continue;
}
?>
<div class="sort-list-name">
<a href="<?php echo get_author_posts_url($user->ID);?>">
<?php echo get_avatar( $user->ID, 35 ); ?>
</a>
</div>
<?php
if ($i++ == 9){
echo "<a href='#'>see more</a>";
break;
}
endforeach; ?>
<?php } ?>
and then if $i get 9 the loop will be break

Related

Wordpress - get user list of following users

I'm using the plugin "user following system" and all I need to get is a list of users which I follow. I tried it with the snippet below but it always returns an array!
What am I doing wrong?
function pwuf_get_following( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$following = get_user_meta( $user_id, '_pwuf_following', true );
if ( empty( $following ) ) {
return;
}
return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
ob_start(); ?>
<ul>
<?php
$following = get_user_meta( $user_id, '_pwuf_following', true );
foreach( $following as $user ) { ?>
<li><?php echo $user->first_name; ?> <?php echo $user->last_name; ?></li>
<?php } ?>
</ul>
<?php
return ob_get_clean();
}
add_shortcode( 'following', 'pwuf_get_following' );

How To Show Chechbox Result From Metabox To Single Post Wordpress?

Ask to everyone, i have problem. Here i try to use multiple chechbox to my custom post metabox.
<?php
function prodetail() {
add_meta_box('pro_metabox', 'Detail Property', 'pro_metabox', 'property', 'normal', 'default');
}
function pro_metabox() {
global $post;
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) );
$elements = array(
'pool' => 'Pool',
'garage' => 'Garage',
'balcon' => 'Balcon',
'yard' => 'Yard',
'internet' => 'Internet'
);
foreach ( $elements as $id => $element) {
if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) {
$checked = 'checked="checked"';
} else {
$checked = null;
}
?>
<div class="pro-inn">
<div class="procols">
<div class="pro-inn">
<input type="checkbox" name="multval[]" value="<?php echo $id; ?>" <?php echo $checked; ?> />
<?php echo $element;?>
</div>
</div>
</div>
<?php
}
}
function pro_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'elements', $_POST['multval'] );
} else {
delete_post_meta( $post_id, 'elements' );
}
}
add_action('save_post', 'pro_meta', 1, 2);
?>
help me to add code to show this checked result to single.php because my code use foreach just show Array text not show text like Pool Garage Balcon ect.
Thanks
Use this code in your single.php file for your custom post
$meta_value = get_post_meta( $post->ID, 'elements', true );
foreach($meta_value as $key=>$value){
echo $value . ' ';
}
It will show results same as you mentioned in the question ie:
(Pool Garage Balcon ect.)

Show post custom field from function template

I'm using the Avada theme in wordpress. I'm trying to add some post custom fields into the scrollover (shown by mouse hover). I tried to edit the template file that controls the scrollover but without success. I got an error message invalid ">".
I tried to use this code to show the custom field:
<?php echo get_post_meta( $post_id,'the_price',); ?>
The Avada function template is as follows:
if ( ! function_exists( 'avada_render_rollover' ) ) {
function avada_render_rollover( $post_id, $post_permalink = '', $display_woo_price = false, $display_woo_buttons = false, $display_post_categories = 'default', $display_post_title = 'default', $gallery_id = '' ) {
// Retrieve the permalink if it is not set
if ( ! $post_permalink ) {
$post_permalink = get_permalink( $post_id );
}
// Check if theme options are used as base or if there is an override for post categories
if ( 'enable' == $display_post_categories ) {
$display_post_categories = true;
} elseif ( 'disable' == $display_post_categories ) {
$display_post_categories = false;
} else {
$display_post_categories = ! Avada()->settings->get( 'cats_image_rollover' );
}
// Check if theme options are used as base or if there is an override for post title
if ( 'enable' == $display_post_title ) {
$display_post_title = true;
} elseif ( 'disable' == $display_post_title ) {
$display_post_title = false;
} else {
$display_post_title = ! Avada()->settings->get( 'title_image_rollover' );
}
// Set the link on the link icon to a custom url if set in page options
$icon_permalink = ( fusion_get_page_option( 'link_icon_url', $post_id ) != null ) ? fusion_get_page_option( 'link_icon_url', $post_id ) : $post_permalink;
if ( '' == fusion_get_page_option( 'image_rollover_icons', $post_id ) || 'default' == fusion_get_page_option( 'image_rollover_icons', $post_id ) ) {
if( ! Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // link + zoom
$image_rollover_icons = 'linkzoom';
} elseif( ! Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'link';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // zoom
$image_rollover_icons = 'zoom';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'no';
} else {
$image_rollover_icons = 'linkzoom';
}
} else {
$image_rollover_icons = fusion_get_page_option( 'image_rollover_icons', $post_id );
}
// Set the link target to blank if the option is set
$link_target = ( 'yes' == fusion_get_page_option( 'link_icon_target', $post_id ) || 'yes' == fusion_get_page_option( 'post_links_target', $post_id ) ) ? ' target="_blank"' : '';
?>
<div class="fusion-rollover">
<div class="fusion-rollover-content">
<?php if ( 'no' != $image_rollover_icons && 'product' != get_post_type( $post_id ) ) : // Check if rollover icons should be displayed ?>
<?php if ( 'zoom' != $image_rollover_icons ) : // If set, render the rollover link icon ?>
<a class="fusion-rollover-link" href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>>Permalink</a>
<?php endif; ?>
<?php if ( 'link' != $image_rollover_icons ) : // If set, render the rollover zoom icon ?>
<?php
// Get the image data
$full_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' );
if ( ! is_array( $full_image ) ) {
$full_image = array();
$full_image[0] = '';
}
// If a video url is set in the post options, use it inside the lightbox
if ( fusion_get_page_option( 'video_url', $post_id ) ) {
$full_image[0] = fusion_get_page_option( 'video_url', $post_id );
}
?>
<?php if ( 'linkzoom' == $image_rollover_icons || '' === $image_rollover_icons ) : // If both icons will be shown, add a separator ?>
<div class="fusion-rollover-sep"></div>
<?php endif; ?>
<?php if ( $full_image[0] ) : // Render the rollover zoom icon if we have an image ?>
<?php
// Only show images of the clicked post
if ( 'individual' == Avada()->settings->get( 'lightbox_behavior' ) ) {
$lightbox_content = avada_featured_images_lightbox( $post_id );
$data_rel = sprintf( 'iLightbox[gallery%s]', $post_id );
// Show the first image of every post on the archive page
} else {
$lightbox_content = '';
$data_rel = sprintf( 'iLightbox[gallery%s]', $gallery_id );
}
?>
<a class="fusion-rollover-gallery" href="<?php echo $full_image[0]; ?>" data-id="<?php echo $post_id; ?>" data-rel="<?php echo $data_rel; ?>" data-title="<?php echo get_post_field( 'post_title', get_post_thumbnail_id( $post_id ) ); ?>" data-caption="<?php echo get_post_field( 'post_excerpt', get_post_thumbnail_id( $post_id ) ); ?>">Gallery</a><?php echo $lightbox_content; ?>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php if ( $display_post_title ) : // Check if we should render the post title on the rollover ?>
<h4 class="fusion-rollover-title"><a href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>><?php echo get_the_title( $post_id ); ?></a></h4>
<?php endif; ?>
<?php
// Check if we should render the post categories on the rollover
if ( $display_post_categories ) {
// Determine the correct taxonomy
if ( 'post' == get_post_type( $post_id ) ) {
$post_taxonomy = 'category';
} elseif ( 'avada_portfolio' == get_post_type( $post_id ) ) {
$post_taxonomy = 'portfolio_category';
} elseif ( 'product' == get_post_type( $post_id ) ) {
$post_taxonomy = 'product_cat';
}
echo get_the_term_list( $post_id, $post_taxonomy, '<div class="fusion-rollover-categories">', ', ', '</div>' );
}
?>
<?php if ( $display_woo_price ) : // Check if we should render the woo product price ?>
<?php woocommerce_get_template( 'loop/price.php' ); ?>
<?php endif; ?>
<?php if ( $display_woo_buttons ) : // Check if we should render the woo "add to cart" and "details" buttons ?>
<div class="fusion-product-buttons">
<?php woocommerce_get_template( 'loop/add-to-cart.php' ); ?>
<span class="fusion-rollover-linebreak"></span>
<a class="fusion-show-details-button" href="<?php echo post_permalink(); ?>"><?php _e( 'Details', 'Avada' ); ?></a>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
}
Usually to use $post_id you need first to declare global $post

inserting ads between every 7th post of homepage and category wordpress page

I am trying to insert google ads between every 7th posts in my wordpress site with infinite scroll. i searched and found results but that didn't worked for me because the WordPress theme i am using is little complex. the result i found was to add this code
<?php $postnum++; if($postnum%5 == 0) { ?>
YOUR AD CODE HERE
<?php } ?>
after <?php endwhile; ?> in index.php, but in my case the loop is different from the general wordpress theme. the loop i have is like this and i want to know how can i implement the above code in my case.
loop code
static function loop( $template ) {
global $wp_query;
echo '<input type="hidden" id="query-' . $template . '" value="' . urlencode( json_encode( $wp_query -> query ) ) . '" />';
if ( count( $wp_query->posts) > 0 ) {
if( self::is_grid( $template ) ){
?>
<div class="loop-container-view grid">
<?php self::loop_switch( $template , 1 ); ?>
</div>
<?php
}else{
?>
<div class="loop-container-view list" id="contloop">
<?php self::loop_switch( $template , 0 ); ?>
</div>
<?php
}
get_template_part('pagination');
} else {
get_template_part('loop', '404');
}
}
which then call this code, i made it a little shorter
static function get( $post , $template = 'blog_page' ){
$meta = meta::get_meta( $post -> ID , 'settings' );
if( isset( $meta['safe'] ) ){
if( meta::logic( $post , 'settings' , 'safe' ) ){
$classes = ' nsfw';
}else{
$classes = ' ';
}
}else{
$classes = ' ';
}
?>
<!-- post -->
<article id="post-<?php echo $post -> ID; ?>" <?php post_class( 'post ' . $classes , $post -> ID ); ?>>
</article>
<?php
}
loop switch function
static function loop_switch( $template = '' , $grid = 1 ) {
global $wp_query;
if ( !empty( $template ) ) {
$ajax = false;
} else {
$query = array();
$template = isset( $_POST['template'] ) && strlen( $_POST['template'] ) ? $_POST['template'] : exit();
$query = isset( $_POST['query'] ) && !empty( $_POST['query'] ) ? (array)json_decode( urldecode( $_POST['query'] ) ) : exit();
$query['post_status'] = 'publish';
$wp_query = new WP_Query( $query );
$grid = isset($_POST['grid']) ? (int)$_POST['grid'] : 1;
$ajax = true;
}
$template = str_replace( array( '_hot' , '_new' , '_like' ) , '' , $template );
if( $grid == 1 ){
$k = 1;
$i = 1;
$nr = $wp_query->post_count;
if (layout::get_length(0, $template) == 940 ) {
$div = 3;
} else {
$div = 2;
}
foreach ($wp_query->posts as $post) {
$wp_query->the_post();
if ($i == 1) {
if (( $nr - $k ) < $div) {
$classes = 'class="last"';
} else {
$classes = '';
}
echo '<div ' . $classes . '>';
}
self::grid( $post, $template );
if ($i % $div == 0) {
echo '</div>';
$i = 0;
}
$i++;
$k++;
}
if ($i > 1) {
echo '</div>';
}
}else{
foreach( $wp_query->posts as $index => $post ) {
$wp_query->the_post();
if ($index > 0) {
?><!--<p class="delimiter"> </p>--><?php
}
self::get( $post, $template );
}
}
if( $ajax ){
exit();
}
}
and post is called here in front-page.php file
<?php
$wp_query = new WP_Query(array( 'page_id' => options::get_value( 'front_page' , 'page' ) ) );
if( $wp_query -> post_count > 0 ){
foreach( $wp_query -> posts as $post ){
$wp_query -> the_post();
$post_id = $post -> ID;
?>
<article id="post-<?php the_ID(); ?>" <?php post_class() ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
<!-- post meta top -->
<?php
if( meta::logic( $post , 'settings' , 'meta' ) ){
get_template_part( 'post-meta-top' );
}
?>
</header>
<div class="entry-content">
<?php
/* if show featured image */
if( options::logic( 'blog_post' , 'show_featured' ) ){
if( has_post_thumbnail ( $post -> ID ) ){
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ) , 'full' );
?>
<div class="featimg circle">
<div class="img">
<?php
ob_start();
ob_clean();
get_template_part( 'caption' );
$caption = ob_get_clean();
?>
<?php the_post_thumbnail( '600x200' ); ?>
<?php
if( strlen( trim( $caption) ) ){
?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php
}
?>
</div>
</div>
<?php
}
}
?>
</div>
<footer class="entry-footer">
<div class="share">
<?php get_template_part( 'social-sharing' ); ?>
</div>
<div class="excerpt">
<?php the_content(); ?>
<?php wp_link_pages(); ?>
</div>
</footer>
</article>
<?php
}
}else{
/* not found page */
get_template_part( 'loop' , '404' );
}
Try this in front-page.php. You may have to adjust it to add after correct number of posts.
if( $wp_query -> post_count > 0 ){
$postnum = 0;
foreach( $wp_query -> posts as $post ){
$postnum++;
if( $postnum%5 == 0 ) {
echo '<p> ---- insert ad here ---- </p>';
}
$wp_query -> the_post();
$post_id = $post -> ID;
?>
it should replace this bit of code
if( $wp_query -> post_count > 0 ){
foreach( $wp_query -> posts as $post ){
$wp_query -> the_post();
$post_id = $post -> ID;
?>

How to count total number of Rating from the Extend Comment Plugin?

I got a WordPress Rating plugin from "http://www.smashingmagazine.com/2012/05/08/adding-custom-fields-in-wordpress-comment-form/"
I would like to count the total number of rating and print that number under the Blog Post. Is this possible?
I added some customisation in this code.
<?php
// Add fields after default fields above the comment box, always visible
add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );
function additional_fields () {
echo '<p class="comment-form-rating">'.
'<label for="rating">'. __('Rating') . '<span class="required">*</span></label>
<span class="commentratingbox">';
for( $i=1; $i <= 5; $i++ )
echo '<span class="commentrating"><input type="radio" checked name="rating" id="rating" value="'. $i .'"/>'. $i .' Star</span>';
echo'</span></p>';
}
// Save the comment meta data along with comment
add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') )
$rating = wp_filter_nohtml_kses($_POST['rating']);
add_comment_meta( $comment_id, 'rating', $rating );
}
// Add the filter to check if the comment meta data has been filled or not
add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
if ( ! isset( $_POST['rating'] ) )
wp_die( __( 'Error: You did not add your rating. Hit the BACK button of your Web browser and resubmit your comment with rating.' ) );
return $commentdata;
}
//Add an edit option in comment edit screen
add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box() {
add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
}
function extend_comment_meta_box ( $comment ) {
$phone = get_comment_meta( $comment->comment_ID, 'phone', true );
$title = get_comment_meta( $comment->comment_ID, 'title', true );
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
?>
<p>
<label for="rating"><?php _e( 'Rating: ' ); ?></label>
<span class="commentratingbox">
<?php for( $i=1; $i <= 5; $i++ ) {
echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"';
if ( $rating == $i ) echo ' checked="checked"';
echo ' />'. $i .' </span>';
}
?>
</span>
</p>
<?php
}
// Update comment meta data from comment edit screen
add_action( 'edit_comment', 'extend_comment_edit_metafields' );
function extend_comment_edit_metafields( $comment_id ) {
if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;
if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') ) :
$phone = wp_filter_nohtml_kses($_POST['phone']);
update_comment_meta( $comment_id, 'phone', $phone );
else :
delete_comment_meta( $comment_id, 'phone');
endif;
if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') ):
$title = wp_filter_nohtml_kses($_POST['title']);
update_comment_meta( $comment_id, 'title', $title );
else :
delete_comment_meta( $comment_id, 'title');
endif;
if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') ):
$rating = wp_filter_nohtml_kses($_POST['rating']);
update_comment_meta( $comment_id, 'rating', $rating );
else :
delete_comment_meta( $comment_id, 'rating');
endif;
}
// Add the comment meta (saved earlier) to the comment text
// You can also output the comment meta values directly in comments template
add_filter( 'comment_text', 'modify_comment');
function modify_comment( $text ){
$plugin_url_path = WP_PLUGIN_URL;
if( $commenttitle = get_comment_meta( get_comment_ID(), 'title', true ) ) {
$commenttitle = '<strong>' . esc_attr( $commenttitle ) . '</strong><br/>';
$text = $commenttitle . $text;
}
if( $commentrating = get_comment_meta( get_comment_ID(), 'rating', true ) ) {
$commentrating = '<p class="comment-rating"> <img src="'. $plugin_url_path .
'/ExtendComment/images/'. $commentrating . 'star.gif"/><br/>Rating: <strong>'. $commentrating .' / 5</strong></p>';
$text = $text . $commentrating;
return $text;
} else {
return $text;
}
}
It would go something like this. Appending the total for all ratings:
if( !is_admin() )
add_filter( 'the_content', 'content_so_23778986' );
function content_so_23778986( $content )
{
if( !is_single() )
return $content;
# Get all comments from current post
$comms = get_comments( array( 'post_id'=>get_the_ID() ) );
$counter = null;
if( $comms )
{
$total = count( $comms );
# Iterate all comments and add its rating to the counter
foreach( $comms as $c )
{
$rating = (int) get_comment_meta( $c->comment_ID, 'rating', true );
$counter += $rating;
}
# Calculate and print the totals
$counter = '<br />' . $counter / $total . ' de 5';
}
return $content . $counter;
}

Categories