How to show limit posts per page - wordpress-theming

Can I show 5 posts on the first page and 8 posts on the second and following pages?
$pagenum = $paged;
if ($pagenum=='') { $pagenum =1;
query_posts('posts_per_page=5&paged='.$pagenum);
} else query_posts('posts_per_page=8&paged='.$pagenum);
if ( have_posts() ) : while ( have_posts( ) ) : the_post();
its work, but weird on page 2 the post start from eighth
how can i fix it?

On the fly I would say you have to use the offset-parameter on the post query for right pagination.
Pseudo Code
$pagenum = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($pagenum == 1 ) :
query_posts('posts_per_page=5&paged='.$pagenum);
else :
query_posts('posts_per_page=8&paged='.$pagenum.'&offset=5'); //set the offset
endif;
if ( have_posts() ) : while ( have_posts( ) ) : the_post();
Edit: Just saw in the documentation that the offset-paramater ignores/overrides the paged-parameter. If you get into troubles have a look at the provided workaround.

Related

How to remove product vertical thumbnails if i use only one image

when i use only one image in single product the thumbnails is hidden but it leaves white space
like ( margin in css )
i tried to remove it if only the product page has only 1 image
i tried to use this function code but it's not working
\`
add_action( 'woocommerce_product_vertical_thumbnails', 'enable_gallery_for_multiple_vertical_thumbnails_only', 5 );
function enable_gallery_for_multiple_vertical_thumbnails_only() {
global $product;
if( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_id() );
}
if( empty( $product->get_gallery_image_ids() ) ) {
remove_action( 'woocommerce_product_vertical_thumbnails', 'woocommerce_show_product_vertical_thumbnails', 20 );
}
}\`

PhpStorm - A way to insert variable-names in comments in live templates

I'm trying to make this live template:
if( $COND$ ):
$END$
endif; // if( $COND$ )
so that I can automatically add the same condition on the endif (in a comment), as I'm typing the actual if-condition.
However, PhpStorm thinks that $COND$ is a comment, so it doesn't work.
The closest I can come is with this:
if( $COND$ ):
$END$
endif; $COMMENT$ if ( $COND$ )
... so I can press Tab and then set the two comment-slashes manually, and then press Tab again to enter the if-statement. Can it be optimized?

How to put all product attributes below description without shortcode Woocommerce

currently I've used this code and put shortcode and id in every product post.
add_shortcode("product_attributes", "display_product_attributes");
function display_product_attributes( $atts ) {
// Shortcode attribute (or argument)
$atts = shortcode_atts(
array(
'id' => ''
),
$atts, 'product_attributes'
);
if( ! ( isset($atts['id']) && $atts['id'] > 0 ) ) return;
$product = wc_get_product($atts['id']);
ob_start();
do_action( 'woocommerce_product_additional_information', $product );
return ob_get_clean();
}
Can someone share new code that I don't need to use shortcode and it's will automaticly show additional product information in every below product description post?
Example like picture below
combine description and additional information

How to display a div in a post in your category page - wordpress

I have a website where each post is displayed and each post has a description explaining the post.
How can I get my category page (the page that summarises all posts) to show only the small summary of the post and nothing else. At the moment I am using
the_excerpt();
but this limits the text to 55 words, however some are smaller than that, and so other text is displayed in the category that I don’t want.
For example, this is an example post:
http://thestudentbubble.com/hidden-gems/hidden-gems-restaurants/ganges-indian/
As you can see below the description is additional information which I don't want it to appear on the category page.
this is the category page - http://thestudentbubble.com/hidden-gems/hidden-gems-restaurants/
yet the additional information that I don't want to appear still appears.
The text I want to appear is in:
<div class = "bioText">
How can I make only the text in the div appear in the category page?
Thank you for the help!
this is the category loop :
<div class="comment">
<?php
$cats = get_the_category();
$category_id = $cats[0]->cat_ID;
$args = array( 'posts_per_page' => 1, 'category' => $category_id);
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
$this_post_ID = get_the_ID();
$this_post_preview = get_post_meta( $this_post_ID, 'post_preview', true );
echo $this_post_preview;
//the_excerpt();
//the_content();
wp_reset_postdata();
endforeach;
?>
</div>
Ok then, the first thing to do is to create a custom textarea in each post's admin panel. It will appear under the editor. Let's create the .php file that will contain our code, call it post_preview_field.php, and place it in the theme root folder. After doing that, we will need to reference it in our functions.php file. This is done by simply adding the following line:
require_once(get_template_directory().'/post_preview_field.php');
Let's open our post_preview_field.php file and write the code.
<?php
add_action( 'add_meta_boxes', 'post_preview_metabox' );
add_action( 'save_post', 'post_preview_save_postdata' );
function post_preview_metabox() {
$screens = array( 'post' );
foreach ($screens as $screen) {
add_meta_box(
'post_preview_metabox_id',
__( 'Post Preview', 'post_preview_textdomain'),
'post_preview_inner_boxes',
$screen
);
}
}
function post_preview_inner_boxes( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'post_preview_noncename' );
$post_preview_value = get_post_meta( $post->ID, 'post_preview', true );
echo '<label for="post_preview_new_field">';
_e("", 'post_preview_textdomain' );
?>
</label>
<textarea name="post_preview_new_field" id="post_preview_new_field" ><?php echo $post_preview_value; ?></textarea>
<?php }
function post_preview_save_postdata( $post_id ) {
if ( 'post' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
} else {}
if ( ! isset( $_POST['post_preview_noncename'] ) || ! wp_verify_nonce( $_POST['post_preview_noncename'], plugin_basename( __FILE__ ) ) )
return;
$post_ID = $_POST['post_ID'];
$post_preview = ( $_POST['post_preview_new_field'] );
add_post_meta($post_ID, 'post_preview', $post_preview, true) or
update_post_meta($post_ID, 'post_preview', $post_preview);
}
?>
Here we go. We just created a textarea field that appears in every post's panel, can contain some text, is saved every time we save\update the post and is stored in the post's metadata.
Now, all we have to do is access this value in the frontend and echo it. How do we do that?
In the loop, we first assign the post ID to a variable:
$this_post_ID = get_the_ID();
and then assign the data we stored to another:
$this_post_preview = get_post_meta( $this_post_ID, 'post_preview', true );
That's it. We can now echo the preview:
echo $this_post_preview;
I managed to solve it by adding
<!--more-->
after each of the descriptions.

How can I display images similar to Tumblr's photosets?

I'm converting my wife's blog over to Wordpress and I want to display image galleries similar to how they're displayed on Tumblr. Here's an example of the layout:
http://bobbyandmaura.com/post/8195960363/photoset_iframe/bobbyandmaura/tumblr_lp2nebJFEW1qhd8ae/500
I can handle the markup and CSS for displaying the images. What I need help with is understanding how I can create this dynamically. Tumblr is smart enough to dynamically display different quantities of images while still always filling all of the space. Here's another example with fewer image:
http://bobbyandmaura.com/post/6700400507/photoset_iframe/bobbyandmaura/tumblr_ln23gi8EqU1qhd8ae/500
Hopefully I can use math to create a dynamic solution so I don't have to manually create a bunch of different possibilities.
OK. Solved. I got a hack to show WP gallery as Tumblr photoset. It is not as customizable as Tumblr, this will only show the first image as the cover image(larger) and rest images in small grid like set.
You need to edit wp-includes/media.php file. If your theme as any other gallery file, you have to edit that. Edit at your own risk as this is core file of WP. If you update your WP in future, you have to do this again. Sorry, I have no time to write a plugin.
Search for "foreach ( $attachments as $id => $attachment )" in wp-includes/media.php and change the following (replace the foreach loop)
$ccg = 1;
foreach ( $attachments as $id => $attachment ) {
if($ccg == 1)
{
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, large, false, false) : wp_get_attachment_link($id, $size, true, false);
}
else
{
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
}
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( ( $columns > 1 && ++$i % $columns == 1) || $ccg == 1 )
$output .= '<br style="clear: both" />';
$ccg++;
}
Demo gallery can be found at http://ontrip.in/the-ca-cur-badi-forest-resort-gorumara/
You may need to modify your Gallery columns number. You may select the cover image by alternating the image order in Gallery insert box.
My friend Victor Gonzáles developed Aurum to manage proportions and have that nice effect. You can get it here: https://github.com/aficiomaquinas/Aurum-CSS
You then additionally group the images by similar proportions(w/h) with a tolerance of 0.2. So that <0.2 is one group < 0.4 another, < 0.6, etc.
Then have a random chance of spawning them in 1,2,3 columns / row.