Ask your WordPress questions! Pay money and get answers fast! (more info)

Adding functions to a WordPress

  • SOLVED

Hello all,

I am currently using [[LINK href="http://www.webtechideas.com/product/wti-like-post-pro/"]]Wti like post pro[[/LINK]] to give the users of my site the ability to like content.

There is a shortcode that comes with that plugin that allows the users to see the posts that they have "liked'. It's called MyMostLikedPostsShortcode.

What I would like to do is add the following functions to this [[LINK href="http://pastebin.com/a7AeHaDq"]]shortcode on pastebin[[/LINK]]:

-the_excerpt
-the_content
-GetWtiLikePost(); I posted this function on [[LINK href="http://pastebin.com/xqJzVVbv"]]pastebin[[/LINK]] so you can have a look.

I would also like to add numbered pagination to this shortcode as I will be using it for a whole page:

<strong>Some pagination links I've tried to use with WP_Query a few times</strong>

This is the only one I could find that works: http://designphiliconline.blogspot.in/2012/08/wordpress-custom-post-type-pagination.html


http://codex.wordpress.org/Function_Reference/paginate_links
http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin

Thank you all for your help!


Answers (2)

2014-04-27

Dbranes answers:

Please try to replace:

$like_exceprt = $post_data->the_excerpt; //The excerpt code not working
$like_the_content = the_content( $post->post_id); // the content not working


with the following:

$like_exceprt = wp_trim_excerpt( $post_data->post_content );
$like_the_content = apply_filters( 'the_content', $post_data->post_content );


Notice that <em>the_content()</em> just echos the output,and doesn't return it and there's no post field called <em>the_excerpt</em>.

<strong>Update 1:</strong>

To use <em>GetWtiLikePost()</em> in your shortcode, you must have it return the value, so we should use it with the <strong>'put'</strong> input argument:

$most_liked_post .= GetWtiLikePost( 'put' );


but this function works only within the loop, since it contains <em>get_the_ID()</em> and <em>get_the_category()</em> functions.

You should therefore try to replace:

foreach ($posts as $post) {

....your code...

}


with

foreach ($posts as $post) {
global $post;
setup_postdata( $post );

....your code...

}
wp_reset_postdata();


in your shortcode callback function.

<strong>Update 2:</strong>

Since the above setup_postdata() doesn't work for you, then please try the following version of <strong>GetWtiLikePost()</strong>:

[[LINK href="http://pastie.org/9116283"]]http://pastie.org/9116283[[/LINK]]

where I added the second input parameter for the post ID.

Then you can use:

$most_liked_post .= GetWtiLikePost( 'put', $post->post_id );

without <em>setup_postdata()</em> stuff in the foreach loop.

<strong>Update 3:</strong>

Check out this modification for the pagination:

[[LINK href="http://pastie.org/9116986"]]http://pastie.org/9116986[[/LINK]]


Chris comments:

Thank you those worked great! You will be rewarded on your contribution once the pagination is put in :)


Chris comments:

Sorry did you edit this because I only saw the content and the excerpt at first. I will begin working on the rest right now!


Chris comments:

When I add the following to the short code, it takes away the first "liked" post and gives me a link to the current page I am on. :

foreach ($posts as $post) {

global $post;

setup_postdata( $post );

$like_exceprt = wp_trim_excerpt( $post_data->post_content );

$like_the_content = apply_filters( 'the_content', $post_data->post_content );
$most_liked_post .= GetWtiLikePost( 'put' );

}

wp_reset_postdata();


Chris comments:

GetWtiLikePost(); also seems to be no bringing up a zero like count. The function is showing, but it's also acting strange. When I tried to dislike something it reset all of the like count.


Chris comments:

also seems to be bringing up a zero like count.*


Dbranes comments:

I updated the answer, with a new version of <strong>GetWtiLikePost( $arg, $pid )</strong> where the second parameter is optional.

ps: in your test (in the comment above) you used the undefined $post_data.


Dbranes comments:

It's very late so I will check with you tomorrow.

Regarding the pagination, you will need to do add an offset to the hardcoded SQL query, like

LIMIT $offset, $limit

where the $offset is read from a GET parameter called <em>wti_page</em> for example.

Then you need a custom pagination function, similar to the <em>kriesi_pagination( $max_page )</em> function, but you can't use it since it's based on the global $wp_query object.

ps: A shortcut, for few total posts, you might try to select all the rows from the db related to the user, but then filter the rows in the foreach loop to get the pagination.


Dbranes comments:

We could use for example:

$offset = ( 1 < $wti_page ) ? $limit * ( $wti_page -1 ) : 0;


Then with limit = 5 we get:


page offset rows limit
1 0 0 - 4 0,5
2 5 5 - 9 5,5
3 10 10 - 14 10,5
4 15 15 - 19 15,5


if I'm not mistaken or too sleepy - Good night ;-)


Dbranes comments:

I just realised why the setup_postdata didn't work, but now I'm off ;-)


Dbranes comments:

I updated the answer with the pagination part.


Chris comments:

I just tested your 3rd update and for some reason I'm just getting the short code printed out on to the screen and no loop. This is an amazing amount of help and I plan to raise the pot soon!


Dbranes comments:

Happy to hear that. Do you still have it registered via:

add_shortcode( 'most_liked_posts', 'WtiMyMostLikedPostsShortcode2' );


Chris comments:

Yeah I typed it wrong. The pagination works! The like button is still setting a count of 0 for the liked content.


Dbranes comments:

ok great. Did you use the redefined version of GetWtiLikePost(), supporting two input arguments ?


Chris comments:

Yeah I added the GetWtilikePost() pastie to functions.php <blockquote>Update 2:

Since the above setup_postdata() doesn't work for you, then please try the following version of GetWtiLikePost():

http://pastie.org/9116283

where I added the second input parameter for the post ID.

Then you can use:

$most_liked_post .= GetWtiLikePost( 'put', $post->post_id );
</blockquote>


Dbranes comments:

When you check the HTML content of the like buttons, what do you see?


Chris comments:

Hey sorry Dbranes. I added a a 2 to the GetwtiLikePost2();. When I just copied and past the code right in and used GetwtiLikePost(); I got:

<blockquote>Fatal error: Cannot redeclare GetWtiLikePost() (previously declared in wp-content\plugins\wti-like-post\wti_like_post_site.php:7) in \wp-content\themes\Project\functions.php on line 494 </blockquote>

When I changed it to have the 2 on it. I got [[LINK href="http://pastebin.com/GH9tKQW9"]]This[[/LINK]] in the html for the like buttons. See how it's saying nothing has been liked yet?


Dbranes comments:

Hey Corbin

You should then prefix the modified functions, for example <em>GetWtiLikePost_corbin()</em>, to avoid defining the same function twice.

The ajax voting in the free plugin looks strange, it isn't updating the current user_id of the voter.

You can also send me the plugin to check (will PM you).


Chris comments:

Thank you very much Dbranes, I emailed you!


Chris comments:

Thanks a lot Dbranes! You really help me out!

2014-04-27

Bob answers:

rough idea for pagination that may help

instead of this

$query .= " GROUP BY post_id $order_by LIMIT $limit";
$posts = $wpdb->get_results($query);

it should be like this

//get paged variable
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($paged - 1)* $limit; // create offset

//compelte query
$query .= " GROUP BY post_id $order_by LIMIT $offset, $limit";
//get posts
$posts = $wpdb->get_results($query);

//find total returned posts
$total_posts = count($total_result);
// decide how many pages will be there it will be used to create pagination
$max_num_pages = ceil($total_posts / $limit);


and then at the end for pagination we can use [[LINK href="http://codex.wordpress.org/Function_Reference/paginate_links"]]paginate_links[[/LINK]]


$big = 999999999; // need an unlikely integer

echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_num_pages
) );


Hope it will be helpful to you.