The_title() WordPress using in php script

I’m trying to get the_title(); from a wordpress page to use it in a php script

my code:

Read More
<?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

Related posts

2 comments

  1. I’ve used this now:

    <?php
      $project_name = trim(ucfirst(get_the_title())); //The title of current post!
      $project_info = get_page_by_title($project_name);
      $project_id = $project_info->ID;
    ?>
    
    <a href="<?php echo get_site_url() . '/?p=' .  $project_id  ?>"><h1><?php the_title() ?></h1>
    <?php the_content() ?></a>
    

    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

Comments are closed.