Get value from a select option and echo content in the header in WordPress -
in theme options allowing users set page 404 template. (which means can create normal page , choose page 404 template). added page dropdown using wp_dropdown_pages()
.
now want echo noindex,nofollow
meta values page user choose obvious seo reasons. tried not work.
function noindex_404(){ global $post; $option = $_post['404_id']; // $post_val = '2'; if ($post->id == $option){ //if ($post->id == $post_val) // if try direct post value, it's working echo '<meta name="robots" content="noindex,nofollow">'; } } add_action('wp_head', 'noindex_404', 1);
the following html created wp_dropdown_pages()
, page 2 selected under option name of 404_id
option value of 2
<select name="404_id" id="404"> <option value="1">page 1</option> <option value="2" selected="selected">page 2</option> <option value="3">page 3</option> <option value="4">page 4</option> <option value="5">page 5</option> </select>
you can check 404 page using condition.
if ( is_404() ) { echo '<meta name="robots" content="noindex,nofollow">'; }
in case not bound specific page. ever page used 404 function check if 404 page code echo out otherwise not. cheers!
Comments
Post a Comment