I’m trying to get the_title(); from a wordpress page to use it in a php script
my code:
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'bin-o' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post); ?>
$project_name = the_title();
$post_id = get_page_id('$project_name');
var_dump($project_name);
?>
<a href="<?php echo get_site_url() . '/?p=' . $post_id ?>"><h1><?php the_title() ?></h1>
<?php the_content() ?></a>
The functions.php:
<?php
// Get the id of a page by its name
function get_page_id($page_name){
global $wpdb;
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
return $page_name;
}
?>
The problem is it only gives the_title() When it is printed.
so I can’t use the_title() to use it for a php script cause the_title will return NULL
how can I fix this so I can use the requested title to use this further in the php script
You use
get_the_title()
.Many wordpress in-loop functions have corresponding
get_
versions, which will return the value, instead of echoing it.I’ve used this now:
The project_info get’s all the project info, project_id gets the ID out of the project info and uses that to redirect to the wanted page. so I dont have to use the functions anymore