How to create wordpress shortcode by this code - wordpress

Sir,
I have this code to show all post of category and thumbnail for 1st post of them.
<?php $recent = new WP_Query(); ?>
<?php $recent->query( 'cat=1&showposts=5' ); ?>
<?php $is_first_post = true; ?>
<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
<ul>
<li>
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail();
$is_first_post = false;
}
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php endwhile; ?>
But I want show this using shortcode. which using category & post number
but I can not make shortcode. Please help me.

// Add Shortcode
function recentpost_shortcode_func() {
$recent = new WP_Query();
$recent->query( 'cat=1&showposts=5' );
$is_first_post = true;
$html = '';
while( $recent->have_posts() ) : $recent->the_post();
$html .='<ul><li>';
if ( $is_first_post && has_post_thumbnail() ) {
$html .=get_the_post_thumbnail();
$is_first_post = false;
}
$html .='<a href="'.get_the_permalink().'">';
$html .=get_the_title();
$html .='</a></li></ul>';
endwhile;
return $html;
}
add_shortcode( 'recentpost', 'recentpost_shortcode_func' );

Related

Wordpress how to display query results in specific div class if total number of posts is < 2

I'm working on a new Wordpress site and am trying to achieve something specific. I'd like to do a query_post and if there is only 1 post available, display that information in a specific div class, otherwise use a different class. Here is a simplified version of the code I am currently using. The one I will end up using, actually has additional query_posts within this:
<?php query_posts('post_type=events&showposts=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary left">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary left">
<?php endif; ?>
<?php else: ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary right">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary right">
<?php endif; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
I hope this makes sense..
You can check the global query after query_posts() like this before you run through the posts and display them:
<?php query_posts( 'post_type=events&showposts=-1' );
global $wp_query;
$classes = array( 'secondary' );
if( $wp_query->post_count == 1 ) :
$classes[] = 'left';
else :
$classes[] = 'right';
endif; ?>
With the above, your code can be shortened to something like this:
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
$type = get_field( 'event_type' );
if ( $type && in_array( 'public', $type ) ) :
$classes[] = 'public';
$action = get_permalink();
else :
$classes[] = 'private';
$action = 'mailto:johnsmith#gmail.com';
endif; ?>
<!-- Output HTML -->
<a href="<?php echo $action; ?>">
<div id="event" class="<?php echo join( ' ', $classes ); ?>">
<?php endwhile;
endif; ?>

Get custom fields without _edit_last, _edit_lock, _wp_page_template and _visual-subtitle

I'm getting custom fields using
<?php $meta = get_post_custom($level_2->ID); ?>
<?php foreach ( $meta as $key => $value) { ?>
<?php echo $key.': '.$value[0].'<br />'; ?>
<?php } ?>
and it is showing
_edit_last: 1<br>
_edit_lock: 1483226440:1<br>
_wp_page_template: page-services.php<br>
Body Repair: ValueBodyRepair<br>
_visual-subtitle: <br>
I need only 4th row Body Repair: ValueBodyRepair<br>
If you would adjust the code this way, you would get only the one you need:
<?php $meta = get_post_custom($level_2->ID); ?>
<?php foreach ( $meta as $key => $value) { ?>
<?php if(substr($key, 0, 1) !== '_'): ?>
<?php echo $key.': '.$value[0].'<br />'; ?>
<?php endif; ?>
<?php } ?>

Is get_template_part() taking into account localization?

I am using get_template_part() to dynamically insert wordpress pages. I have (m)qTranslate installed but I can not get any other language than the default one with get_template_part(). The __() and _e() are also not translated.
get_template_part() code:
$q_config['language'] = $_POST['lang'];
$post_id = $_POST['postId'];
$page_data = get_post( $post_id );
ob_start();
get_template_part( 'page-template/page', $page_data->post_name );
$output .= ob_get_clean();
ob_end_flush();
template code example:
<div>
<?php $the_query = new WP_Query( 'posts_per_page=50' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
if (in_category( get_category_by_slug( 'designs' )->term_id )) {
$termsArray = get_the_terms( $post->ID, "category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
}
?>
<div class="<?php echo $termsString; ?> item">
<span class="fs-<?php echo $post->ID; ?> ?>"></span>
<div><?php the_title();?></div>
<div class="thumbmail">
<?php
$thumb_url_array = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
echo '<img src="'.$thumb_url.'"/>';
?>
</div>
<span class="description name"><?php _e( 'Click to see more', 'sp' ); ?></span>
</a>
</div>
<?php }
endwhile; ?>
</div>
<?php endif; ?>
</div>
In function.php I have:
load_child_theme_textdomain('sp', get_stylesheet_directory().'/languages');
Does anybody know if get_template_part() can get the localization and how?
I have just replaced the mqtranslate (deprecated) plugin with qtranslate-X and the translation is working fine again with get_template_part().

How to list only subpages title and link in a wordpress page div

I have a webblog using wordpress i'm using wp_list_pages() function to get list its Sub Pages Links (excluding main page) in a list, I've tried the below codes but its returning all Pages Sub Page.
I also tried this Argument child_of but its not happning. Please suggest i have tried so many things...
<?php
$output = wp_list_pages('echo=0&depth=1&child_of=0');
if (is_page( )) {
$page = $post->ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
}
echo $output;
?>
This code will list the subpages and siblings of the current page:
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
}
else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div id="submenu">
<ul>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
</ul>
</div>
<?php endif; ?>
U can try this code this code may be useful to u
<?php
global $wpdb, $post;
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) :
foreach ( $child_pages as $pageChild ) :
setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle">
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark"
title="<?php echo $pageChild->post_title; ?>">
<?php echo $pageChild->post_title; ?>
</a>
</h2>
<?php endforeach; endif; ?>

Wordpress post query php custom field conditional

Here's the situation:
In wordpress I'm trying to reset a post WP_Query so that I can rewrite the post link based on whether or not a custom field exists in the post. I'm trying to give the post a NEW link in the custom field.
All I've managed to do here is kill the link entirely. Any and all help is greatly appreciated, I'm pretty green to php.
Here's my WP_Query:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
if ($newLink != '') {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
I think this is what you need. It's hard to tell. I suppose that the first part of the if statement is what runs if there is no custom post meta? I couldn't tell. Here's what the problem was. The if statement ran the first part if there IS a value returned for the custom post meta, otherwise it ran the second part, using the empty string as the href. (The first part runs if the custom value either doesn't exist or is anything but an empty string). Changing the if statement to check if it's empty is better because it will catch it if it doesn't exist (returns false), or if it does exist but is an empty string (declared but not defined).
I've marked what I edited with comments (just one line).
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */ if (empty($newLink)) {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>

Resources