• <?php echo $artpost->max_num_pages ?>
    <?php echo $artpost->found_posts; ?>

    i’m getting 3 and 15, How do use these number in javascript or Functions.php

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    Normally, the correct way for JavaScript is to use wp_localize_script(), but in your case I suspect $artpost is not available when the action hook fires. What you can do is echo out some JavaScript assignments within <script> tags as soon as $artpost becomes available.
    <?php echo "<script>var numPages = {$artpost->max_num_pages};</script>"; ?>
    Be sure your JavaScript does not run until after this happens.

    Code in functions.php shares the same PHP namespace as all other code, but timing becomes critical, along with variable scope. Your functions.php code cannot work until $artpost is defined, so it either needs to be called from the template afterwards or be hooked into an action that fires afterwards.

    $artpost also needs to be within the function’s scope. There’s different ways to do this. It can be passed as a parameter, it could be declared global, it could be a static value, these are some of the possibilities.

    • This reply was modified 9 years, 4 months ago by bcworkz. Reason: fix typo
    Thread Starter apollo789

    (@apollo789)

    Can i get example to functions.php?

    Moderator bcworkz

    (@bcworkz)

    Sorry, you haven’t provided enough context for us to provide a working example. Where, when, how is $artpost assigned an object? Where, when, how do you want to use values from this object?

    Depending on these answers, you may not even need anything on functions.php, you could perhaps manage it all on a template page or where ever your loop occurs.

    Thread Starter apollo789

    (@apollo789)

    i want adapt link(this plugin) to my page-template with custom loop
    Link
    Code

    • This reply was modified 9 years, 4 months ago by apollo789.
    Thread Starter apollo789

    (@apollo789)

    But first i need all element ;(

    Moderator bcworkz

    (@bcworkz)

    The problem is the “template_redirect” action fires much too early to use $artpost. Fortunately, as long as you set the javascript vars before the load more posts button is clicked, it all should still work, there’s no need to do this with template_redirect. You still need to enqueue the script and style though.

    Set the following javascript vars just before you reset the query with appropriate values:
    startPage, maxPages, nextLink
    See the plugin’s PHP file for what they used, use equivalent values from your template and set them by echoing out assignments within script tags like I illustrated on my first post. There’s no need to do this on functions.php, do so right on the template where $artpost is in scope.

    Thread Starter apollo789

    (@apollo789)

    Can i get simple example doing that?

    • This reply was modified 9 years, 4 months ago by apollo789.
    Thread Starter apollo789

    (@apollo789)

    thanks, working. Can u check it? Did i correctly modify?! And more way to do that? I want increase knowledge.
    Code

    Moderator bcworkz

    (@bcworkz)

    Well, it’s not quite what I had in mind, though what you did is correct. Especially considering it’s working! 🙂

    TBH, what I had in mind might not have worked, I don’t fully understand what that plugin was doing. I had a good hunch what needed to happen but wasn’t sure. Some of this sort of thing is difficult without access to the server. To be clear, I’m not asking for access. I don’t want it due to liability reasons. It’s against the rules here as well.

    If there’s something specific you’d like explained, just ask, I’ll do my best to explain.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘How to get element from loop?!’ is closed to new replies.