Open POST in php file

Asked

Viewed 363 times

0

By default know that I can list some wordpress articles on an external page in php using a "require('.. /.. /wp-blog-header.php');".

But by clicking on one of these articles it goes to the default page that opens the full Post.

My question is this::

How do I get the full Post to open in a file (Ex. Postcompleto.php") separate, instead of opening in the single php. ?

1 answer

0

You can use the include method require('../../wp-blog-header.php'); to upload any content. What changes is how you call the content within your external file.

By default when you include wp-blog-header.php it will load the entire back-end of Wordpress with the basic parameters, IE, it loads the home page. From there on it will obey whatever is in your file, so you can create a second query to get the content that interests you:

// Pra buscar um arquivo de posts do tipo "artigos"
$query = new WP_Query( array( 'post_type' => 'artigos' /* etc */ ) );
if ( $query->has_posts() ) : 
    while ( $query->has_posts() ) : 
        $query->the_post();
        // exibe as informações aqui
    endwhile;
endif;

-

// Pra buscar um post pelo ID
$single = get_post( $id );
echo $single->post_title; // imprime o título

Now, to link from within your external php post file you will probably have to filter the Urls that are being displayed. Something like this is a path, but there are others depending on your specific case:

// em functions.php
add_filter( 'post_link', 'altera_link' ); // posts
add_filter( 'page_link', 'altera_link' ); // pages
add_filter( 'post_type_link', 'altera_link' ); // custom post types
function altera_link( $link ) {
    // altera os links de 'example.com' para 'domain.com';
    return str_replace( 'example.com', 'domain.com', $link );
}

but: load wp-blog-header.php in general is bad idea.

I would think about the architecture of this system because there are better ways not to use the WP template system, if that’s the case. There are Wrappers for PHP frameworks (type Steed), or even for javascript front ends (type Nodeifywp) and there is good old Ajax to pull only the content you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.