Wordpress hardcode <!--nextpage--> in php - php

In Wordpress we can split a post with <!--nextpage-->, if we put it in the code version of the post-editor. This is working for me. BUT what is, if I want to hardcode this in the themes loop file? How can I get this to work?
Assume I have something like this in my loop file:
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php wp_link_pages(foobarbaz); ?>
Obviously the following solution won't work:
<?php the_excerpt(); ?>
<!--nextpage-->
<?php the_content(); ?>
<?php wp_link_pages(foobarbaz); ?>
I have no clue where I could find the right php function that is executed when <!--nextpage--> gets parsed in the code editor.
The only solution I can think of is creating a new post with just <!--nextpage--> in it and somehow try to hardcode this specific post inside the loop file. But there has to be a much better and cleaner way of doing it...
Any suggestions?

Not sure what you're trying to do but you can control the number of posts that appear on an archive page on the Settings->Reading tab.
If you want to split your content in individual posts, I think the best way to do this is to hook into the 'the_content' filter. I'm not sure if you have criteria where you want to split the post but if it's word count and you don't want to use the_excerpt;, you can write a function (in your theme's functions.php file) like this:
add_filter('the_content', 'your_post_split');
function your_post_split($content) {
//do something with the content and insert <!--nextpage-->
return $content;
}
When you write filters, make sure you always return the variable coming into your function.
If you want to prepend your post with the excerpt text, try this:
function your_post_split($content) {
global $post;
$content = $post->post_excerpt . $content;
return $content;
}
Take <?php the_excerpt; ?> out of your template.

Related

Pagebreak using wp_link_pages in Wordpress

I have a php script that reads a text file of 10000 urls one in each line. The script displays all the urls in a blog post. I want the page to be divided into 10 small pages to facilitate comfortabe browsing. So how to add a pagebreak using <?php wp_link_pages(); ?> function inside a post? Something like this:
<?php
echo "Hi, This is First Page";
wp_link_pages();
echo "Hi, This is Second Page";
?>
That isn't how wp_link_pages(); works. It only displays your the_content in pages if your the_content contains this tag <!--nextpage-->
sudo code
<?php
echo apply_filter ("the_content" , "Hi, This is First Page<!--nextpage-->Hi, This is second Page");
wp_link_pages();
?>
You might want to check out this question as to how to retain the page data if you are creating your own page content and not using the standard content area.
Wordpress PHP - need to retain nextpage paginating tag in custom query
When already in a php block, you can execute a function without opening another php block. According to the documentation, wp_link_pages(); should also be called without echo.
wp_link_pages();
Documentation:
https://codex.wordpress.org/Function_Reference/wp_link_pages
This works just fine:
global $post;
$total = substr_count($post->post_content, '<!--nextpage-->') + 1;

Wordpress database: add value of custom field to beginning of post (and save it combined)

I need to get the value of a custom field (_subtitle) from the post meta and have it saved to the beginning of the post text (post_content).
How can I achieve this? (I tried the update() function but lost the post content before)
You need to use get_post_meta() it might look something like that if you want to place it within a template.
<?php
$subs = get_post_meta(get_the_ID(),'_subtitle',true);
$post_content = get_the_content();
echo $subs.$post_content;
?>
This would be the gist of it, you could also filter the_content(); and place the code within the functions.php

Write textarea value as PHP

I am creating a WordPress plugin where a user can specify a custom loop repeater based on the value of a textarea.
The idea is, a user can use a predefined repeater that ships with the plugin or they can build their own repeater by simply adding html + PHP to the textarea.
Example of a custom repeater:
<h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> - <?php the_time(); ?>
The issue is when I echo/print the textarea value, the WordPress functions within the <?php ?> tags do not execute.
Is what I'm attempting even possible?
Let me explain further...
I am building an installable plugin based on my Ajax Load More script.
This plugin will be only used by site admins and for total control of the display of posts I want to allow them to create a custom repeater.
On the plugin settings page, I want to have a textarea where the admins can add whatever html/php code they want in order to customize the repeater.
My issue is how do I execute the PHP entered within the textarea on the frontend as echoing the value does not work.
Use eval function to change plain text to php code. More about it here
eval('echo "Hello world!"');
will print out "Hello world!" instead of 'echo "Hello world!"'
BUT what if i write to textarea:
die();
One way of doing this is to use the eval() function as Justinas says, but I would strongly recommend you don't use it as I have found it to be unreliable and problematic.
What I would recommend is to write the PHP code in the string you extract from the text area to a file and then include that file where you want the PHP to execute.
So, I would recommend running
<?php file_put_contents($file_path, $contents); ?>
whenever the text area is updated to update the file, then
<?php include_once($file_path); ?>
where you want the code on the front end.
Those wordpress functions will not work unless they are in the Loop, Here is a basic Loop:
<?php if(have_posts()) { ?>
<?php while(have_posts()) { ?>
<?php the_post(); ?>
<?php // custom post content code for title, excerpt and featured image ?>
<?php } // end while ?>
<?php } // end if ?>

Wordpress custom page code

I'm very new to wordpress and was just wondering if anyone
could point me in the direction of a tutorial which will explain
how to add my own code to wordpress pages.
So far all that I've been able to find is that I'm supposed
to copy the page.php from my template and use that as the
template for a page added in wordpress. This however only
gives me control over the body and not the header/nav menu/sidebar.
Am I correct is assuming I must replace the method calls such as
get_header(), etc with the code it would generate and manipulate
it from there? Also is it correct if I change the arguments to
get_template_part( 'content', 'page' ); and add a php page with
the appropriate name for the body?
Way 1 You can do this:
1) add new page
2) add these code to index.php or page.php
<?php
if (is_page('About 3C')) {?>
<div>Display this particular div</div>
<?php }
else { ?>
<div>Display that particular div</div>
<?php }?>
or
<?php
if (is_page('About 3C')) {
<div>Display this particular div</div>
}
else {
<div>Display that particular div</div>
}
?>
assume your added page name is: About 3C
and you can call the database from above code
OR way 2: please refer:
http://line25.com/tutorials/how-to-create-a-wordpress-custom-page-template

Run php functions on selected Wordpress pages

I am trying to customise a Wordpress theme. I have a function in themes/functions.php that I would like to run on some pages.
I need to be to:
Detect the page ID to determine whether the function should execute
Determine which hook to attach the function to (preferably something like page load.
Cheers
The file functions.php is for theme specific functions called inside your theme. If this is a theme specific function then the function call should be in the header (or wherever you want the output of the function to appear) via <?php my_function() ?>.
Hooks are for plugins, not template specific code.
If you are inside The Loop, then you can call <?php the_ID(); ?> as WarrenB said. If you are outside of the loop, then <?php echo $post->ID?> will print the page ID.
To the questions you posed, given you want to select on id #9, run your loop like this:
<?php
query_posts('page_id=9');
if (have_posts()) : while (have_posts()) : the_post();
// Do whatever on post id #9
?>
<?php endwhile; else: ?>
// Do whatever on all the other posts
<?php endif; ?>
If this isn't the answer you're looking for, please add more information to your question.

Categories