WordPress Popular Posts
[resolved] Get permalink from the function loop (5 posts)

  1. Shahin
    Posted 3 years ago #

    <?php if (function_exists("get_mostpopular")) get_mostpopular("range=all&order_by=views&limit=3&stats_comments=0&thumbnail_selection=usergenerated&thumbnail_width=100&thumbnail_height=100&excerpt_length=80&do_pattern=1&pattern_form='{image} {title {summary}'&post_start='<li onclick=location.href=\"". get_permalink() ."\">'&post_end='</li>'"); ?>

    As you can tell the get_permalink() is not the right way to do it since it only points to last(?) post.

    Anyone know how to get the correct permalink?


  2. Hector Cabrera
    Plugin Author

    Posted 3 years ago #

    Hi shahin.ag,

    The post_start and post_end parameters are meant to define the opening and closing tags containing the post content, respectively. The permalink is included already within the post content.

    When you call get_permalink() like that it'll return the URL of the last post queried by WordPress, that's why it can't work as you are using it.

    Why you want to attach the onclick event to the LI when there's a link already on the content?

  3. Shahin
    Posted 3 years ago #

    I want the entire <li> to be a clickable link to the respectively URL of the post.

    Got any suggestions?

    [Moderator Note: Please post code or markup snippets between backticks or use the code button.]

  4. Hector Cabrera
    Plugin Author

    Posted 3 years ago #

    Well, you got two options here: a) hack the plugin's code so it appends the A tag before the LI tag, o b) use Javascript (jQuery would do wonders here) to get it done.

    Since I'm a jQuery fan (which WordPress uses, too), and considering that any changes made to the plugin would be lost once an update is released, this is what I would do:

    <?php if (function_exists("get_mostpopular")) : ?>
    <script type="text/javascript">
    		jQuery(".popular-posts").delegate("ul li", "click", function(){
    			// get href attribute from A tag
    			var target = jQuery(this).children("a:first").attr("href");
    			// load URL
    			window.location.href = target;
    <?php get_mostpopular("range=all&order_by=views&limit=3&stats_comments=0&thumbnail_selection=usergenerated&thumbnail_width=100&thumbnail_height=100&excerpt_length=80"); ?>
    <?php endif; ?>

    I didn't test the code but it should do what you need (verify that jQuery is present on your theme's head section).

  5. Shahin
    Posted 3 years ago #

    You are a genius.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WordPress Popular Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic