How do I search for a word in a wordpress post?

Discussion in 'HTML & Website Design' started by MindReality, Jul 16, 2013.

  1. #1
    How do I search for a word in a wordpress post?

    For example, I would like to include a code that checks whether the word "apple" is found in a wordpress post.

    I am not talking about a search box. I am talking about a code that runs in the index.php

    My index.php contains this code:

    
    <div id="content">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php // Post dates off by default the_date('','<h2>','</h2>'); ?>
        <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>    
        <div class="meta">Posted by <?php the_author() ?> <?php edit_post_link(__('Edit This')); ?> - <a href="http://www.mindreality.com/recform.php" target="page" onClick="window.open('','page','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=450,left=50,top=50,titlebar=yes')">Share this article with friends!</a> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<?php if(function_exists('wp_print')) { print_link(); } ?>
    </div>
    <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
        <div class="main">
            <?php the_content(__('(more...)')); ?>
        </div>
    <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
    <?php
    if (!is_user_logged_in()) { ?>
    <div align="center"><b>Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... <a href="http://www.mindreality.com/view.html">Click Here to View the HIDDEN PART!</a></b></div>
    <?php } ?>
    
    Code (markup):
    As you can see, the sentence "Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... Click Here to View the HIDDEN PART!" only shows up if the user is not logged in.

    How can I also make sure that the sentence only displays if there is a word "[hidepost]" in the post content?
     
    Last edited: Jul 16, 2013
    MindReality, Jul 16, 2013 IP
  2. homer7

    homer7 Well-Known Member

    Messages:
    268
    Likes Received:
    25
    Best Answers:
    4
    Trophy Points:
    125
    #2
    You can use a simple shortcode like this

    function hidepost_shortcode() {
     
        return '<div align="center"><b>Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... <a href="http://www.mindreality.com/view.html">Click Here to View the HIDDEN PART!</a></b></div>';
     
    }
    add_shortcode('hidepost', 'hidepost_shortcode');
    PHP:
    put this code on your functions.php file and it will output your message whenever you use the shortcode [hidepost] on your posts.
     
    homer7, Jul 16, 2013 IP
  3. MindReality

    MindReality Well-Known Member

    Messages:
    202
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Hi,

    I tried the code but it did not work.

    I tried two variations in my functions.php

    
    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar();
    ?>
    function hidepost_shortcode() {return '<div align="center"><b>Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... <a href="http://www.mindreality.com/view.html">Click Here to View the HIDDEN PART!</a></b></div>';}
    add_shortcode('hidepost', 'hidepost_shortcode');
    
    Code (markup):
    
    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar();
    function hidepost_shortcode() {return '<div align="center"><b>Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... <a href="http://www.mindreality.com/view.html">Click Here to View the HIDDEN PART!</a></b></div>';}
    add_shortcode('hidepost', 'hidepost_shortcode');
    ?>
    
    Code (markup):
     
    MindReality, Jul 16, 2013 IP
  4. homer7

    homer7 Well-Known Member

    Messages:
    268
    Likes Received:
    25
    Best Answers:
    4
    Trophy Points:
    125
    #4
    The code worked fine with me (it outputs that message). whenever i place [hidepost] on my post text editor. http://i.snag.gy/vwWeL.jpg
    make sure to put the code before the closing tag ?>
    also you can omit it anyway, its not required only <?php is required once at the beginning of the file (functions.php)
     
    homer7, Jul 16, 2013 IP