• Hi.
    I am trying to run s cript from a post, when user has clicked the <–more–> tag and is sent to the single post view. The script should not execute when the home page is loaded and 10 or more posts are listed. Only if user clicks <—more—> tag
    Can you make an If(….) {} thing?
    Thnak you for all tips.

    Knud

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter knudoboy

    (@knudoboy)

    Well I think I must correct a little. The script should only run when only one post is listed. Regardless of how the user got there.
    I fancy to add it to the content.php file.
    It that possible?

    Moderator bcworkz

    (@bcworkz)

    Editing your theme file is unlikely to work, but it depends on your theme. Most themes get the read more link through the_content() or the_excerpt(). Both of these have filters where you could add an onclick attribute the generated link.

    You could probably also accomplish this through jQuery. There’s likely some sort of jQuery selector that isolates the read more link. Add the .click method to that selection.

    Your initial post made sense to me, you could use the is_home() template tag in the filter callback to decide whether to insert the onclick attribute or not. Adding it instead to the single post like you ask in your second post has me a bit confused. Single posts normally do not have read more links. Do you mean an archive page, such as search results, where only one post is found?

    If the latter, you can get the number of posts returned from the global $wp_query object. You should first check $wp_query->is_main_query() to avoid adding onclick to the wrong items, like widget excerpts. There are other template tags you can check, like is_archive() and is_search() to determine context.

    Thread Starter knudoboy

    (@knudoboy)

    Thank you for all these very clever ideas. I found another getaround. In content.php there is an if-else statement starting like this:

    if ( is_single() ) :

    I put my script there, and after trying about 30 times, I got it working.

    Knud

    hi there, i know you have a lot of info now that they shared to you, just incase your interested for more try to review this. . .https://getcomposer.org/doc/articles/scripts.md

    hey there, we have an info that you may read and apply it if you want, it will help you about your concern,,https://getcomposer.org/doc/articles/scripts.md

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to run a script from a post’ is closed to new replies.