Extract title and add to post in Wordpress - php

I have a Wordpress site with 500+ posts. I would like to extract the title of each post, wrap it with some span class code, add the title and code to the end of each post (the same post from which it was extracted), and hide its visibility. How would I go about doing this in Wordpress/PHP?
Thanks!
EDIT: #unixmiah, would this be achieved like so?
<?php
global $post;
$args = array( 'numberposts' => 500, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li span class="name hidden"><?php the_title();?></li>
<?php endforeach; ?>
Which file do I insert this into? functions.php? Also, how do I ensure this code will run site-wide?

You can use the code below to accomplish this with wordpress internal functions to get that information in a loop.
For example
<?php
global $post;
$args = array( 'numberposts' => 500, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
your javascript span question seems vague, do you mean to hide it and show it on mouseover?

Related

Wordpress loop using wp_query category_name => category+textstring

I have a wp_query getting all posts in a category with the same slug as the title as the current page.
Where i have become stuck is modifying wp_query category_name => $post->post_name plus a text string.
For example if page was to be called "Long Day" all posts with the category slug "long-day" will be shown. I need to bring in these posts in one loop and in another have post_name plus the text sting eg long-day-today.
This is what i have so far...
<?php
global
$post; $args = array(
'category_name' => $post->post_name
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php echo $cat ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
[EDIT]
I feel like a bit of a fool now, after a little playing i came up with this
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
Which seems to do the trick, if there is a better way of achieving this i would still be interested to hear...
Thanks
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);

Am looking for a way to show random products in products category page using woocomerce

How do I change product in same category randomly? I've been looking but can't seem to find any plugin/script to do this, anyone got an idea on this... thanks
You can use the following code to display the products rows of "CATXXX" :
<?php echo do_shortcode( '[product_category category="CATXXX" per_page="8" columns="4" orderby="rand"]' ) ?>
Also you can treat products like any other post type and query using get_posts(). Limit the number of posts retrieved to 1 and change the orderby parameter to random:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category' => 'CATXXX'
'post_type' => 'product');
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();
You can also use new WP_Query() which would be similar.
You can try this code shortcode
[product_category category="category_slug" per_page="10" orderby="rand"]
Or
<?php echo do_shortcode( '[product_category category="category_slug" per_page="10" orderby="rand"]' ) ?>

Strange behaviours getting all post from wordpress category

today i need to get all the posts from a specific category on wordpress.
The code it's pretty basic, and it's like that:
print_r(get_posts(array('numberposts' => -1, 'category' => 3)));
The id of the category it's obviously 3.
But i receive always the last 5 posts of that category, not all the post present there (something like 60posts).
Anyone know why could happen this strange stuff?
Try something like this:
<ul>
<?php
$args = array( 'numberposts' => -1,'category' => 3 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; wp_reset_postdata();?>
</ul>
If this works for you, all you need to do then is to change the output display(the ul and li)

How to determine category name of returned Posts in WordPress

What i'm trying to do is find out what category the Posts are when they are returned so i can put them in specific divs.
e.g. i have #div1 #div2 and #div3 and i have 3 posts returned each with a different category.
What i want to do is have 1 post each in a div. To do this i need to determine the category.
Here is how i fetch Posts by category but how do i fetch Posts of 3 categories and determine the category of the Posts returned, so i can change the div id?
<?php
//gallery
$args = array( 'category_name' => 'Clubs');
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div1">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Obviously i could duplicate this code twice and just change the category and div names but is that the right way to do this?
To avoid copy-paste, I'd suggest putting your code in a function, which takes the parts you want to change as parameters. Something like this (I added the posts_per_page parameter, since you said you only want one post per div):
<?php
function show_gallery($category, $id) {
$args = array( 'category_name' => $category, 'posts_per_page' => 1);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div<?php echo $id; ?>">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
}
show_gallery('Clubs', 1);
show_gallery('Hearts', 2);
show_gallery('Diamonds', 3);
?>
I haven't tested, but hopefully it works and is enough to get you started.
I'd put the function declaration at the top of your template.

Wordpress get_posts() returning same post all the time

I am just playing around with WordPress and neither I have any idea abt PHP.
I am trying to fetch few random posts using get_posts() function of the WordPress
my code is something like this
<?php
args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
$rand_posts1 = get_posts( $args1);
foreach( $rand_posts1 as $randpost1 ) : ?>
<?php the_title(); ?>
<?php endforeach; ?>
But this code is only returning same post all the 12 times and that is the lastest post.
I am clueless what exactly I am doing wrong.
Can any one help me to correct my mistake or point me about my issue.
Try this one
<?php
$args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
global $post;
//save the current post
$temp=$post;
$rand_posts1 = get_posts( $args1);
foreach( $rand_posts1 as $post ) ://yes this is required, we need $post in setup_postdata
setup_postdata($post); ?>
<?php the_title(); ?>
<?php endforeach;
$post=$temp;//restore current page
?>
That will do it.
Also please take a look at get_posts

Categories