Using post title in an array

admin2025-01-07  4

I want to use the the_title() function to get the title of a post and then reference that title in an array. Code is as follows:

    <?php
            $title = array( 
                the_title()
            );
            $args = array(
                'post_type' => array( 'questions' ), 'content' => array( $title )
            );

So I want to take the post title and use the $args variable to find posts that have post type = 'questions' and have the current post's title as their value for the 'content' taxonomy.

Currently it isn't working. The post I'm testing on has title 'Book1'and it works when I change the $args line to:

$args = array(
                'post_type' => array( 'questions' ), 'content' => array( 'Book1' )

But with the code I listed first, it doesn't work...

EDIT: yep, it was simple: just needed to change it to 'content' => $title because $title is already an array. Thanks all!

I want to use the the_title() function to get the title of a post and then reference that title in an array. Code is as follows:

    <?php
            $title = array( 
                the_title()
            );
            $args = array(
                'post_type' => array( 'questions' ), 'content' => array( $title )
            );

So I want to take the post title and use the $args variable to find posts that have post type = 'questions' and have the current post's title as their value for the 'content' taxonomy.

Currently it isn't working. The post I'm testing on has title 'Book1'and it works when I change the $args line to:

$args = array(
                'post_type' => array( 'questions' ), 'content' => array( 'Book1' )

But with the code I listed first, it doesn't work...

EDIT: yep, it was simple: just needed to change it to 'content' => $title because $title is already an array. Thanks all!

Share Improve this question edited Jan 30, 2017 at 7:32 Rob Crews asked Jan 29, 2017 at 3:44 Rob CrewsRob Crews 12 bronze badges 3
  • 1 How is the $args array being used? Also, where is this code being used? Int the loop? Within a function hooked somewhere? Please edit the original question to show this code too. @ngcodex is right about using get_the_title() instead of the_title(), since the latter will echo output immediately. – Dave Romsey Commented Jan 29, 2017 at 20:07
  • $title is already an array - therefore this should be 'content' => $title – Michael Commented Jan 29, 2017 at 21:05
  • Glad to hear that you fixed the problem, Rob. Please post your solution as an answer to your question, then stop by in a couple of days and click the checkmark next to the answer to accept it. This will close the question out of the unanswered question queue. Thank you! – Dave Romsey Commented Jan 30, 2017 at 7:44
Add a comment  | 

2 Answers 2

Reset to default 0

You could use get_the_title() to store the title inside a variable.. the_title() would actually print the title ..

Answer was simple in the end:

<?php
        $title = array( 
            the_title()
        );
        $args = array(
            'post_type' => array( 'questions' ), 'content' => $title
        );

So I just needed to change 'content' => array( $title) to simply 'content' => $title. This was because $title is already an array!

Thanks everyone for the help.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261864a773.html

最新回复(0)