Pagebreak using wp_link_pages in Wordpress - php

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;

Related

Calling wordpress previous_post_link function with an html link

In the midst of Customizing my new Wordpress site I would like to add next and previous portfolio post buttons to the single portfolio post page, and I've found that I can easily do so by pasting the following code:
<div><?php previous_post_link('%link', 'PREV'); ?> | <?php next_post_link('%link', 'NEXT'); ?></div>
Somewhere in the single-flv_portfolio.php page.
The only problem with this solution though is that I'm only able to place the links/buttons in the "portfolio frame" so to speak, and not in the actual content of every single post, which I would like to, for layout purposes.
I've tried pasting the same code within the contents of a portfolio post, using the backend editor, but to no avail. Wordpress, or my specific template (Wizard) seams not allow me using php at all - not even for something small like echoing out the current year in a dynamic copyright function.
Is it possible to maybe create the next and previous portfolio post functions in a custom .php-file, and then call them with an html anchor tag, and if so, how would I go about doing that?
Try something like this in your functions.php file:
function my_next_previous_link($content) {
$content .= 'some html here';
return $content;
}
add_action('the_content','my_next_previous_link');

Shortcode or PHP inside a shortcode in Wordpress

On a php page in wordpress, I have a shortcode for an accordion, that consists of the following, and works perfectly:
<?php echo do_shortcode('[su_accordion]
[su_spoiler title="Feature Locations" style="fancy"] CONTENT GOES HERE
[/su_spoiler]
[/su_accordion]'); ?>
Within the accordion, I want to display an interactive map, which can be displayed using either a shortcode, or PHP. Both options are:
[show-map id='1'], or
<?php build_i_world_map(1); ?>
When I insert either of these into the accordion shortcode, the map does not display. However, outside of the accordion, the second option above (with php) displays successfully.
What am I missing in terms of including this in the shortcode for the accordion?
In your example your outer section should be like
<?php echo do_shortcode('[su_accordion]
[su_spoiler title="Feature Locations" style="fancy"]'. build_i_world_map(1) .'
[/su_spoiler]
[/su_accordion]'); ?>
In wordpress you should call do_shortcode() for shortcode calls.
<?php echo do_shortcode("[show-map id='1']"); ?>
do_shortcode() reference
this is an effective way to include a concatenating inside a shortcode function
<?php echo do_shortcode('[download url='.$next_attachment_url.']'); ?>

Dynamic title depending on page (DRUPAL 7)

Before I made the switch to Drupal (previously just static HTML), I had a div title in my header that changed depending on what page the user was on using the following code:
ABOUT PAGE
<?php
$title = "ABOUT US</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path); ?>
The reason this worked, however, was because I was using static HTML... which means I had every page set to use its own Index.PHP, located at (for the About page, for example) public_HTML/about/index.php. Therefore, I could edit each page individually to my liking by editing its respective Index file...
Now, with Drupal.. instead of having so many Index.PHP files like I did before (which was messy IMO...), I want to have every page use the same page.tpl.php (with the exception of the front page, which uses page--front.tpl.php).
Can I somehow modify the above PHP code to display a different title using IF statements depending on what page the user is on?
For example... (I have no idea how to PHP code so this is just to give you experts an idea of what I would want..)
Ifpage=about, $title=About Us;
Ifpage=contact, $title=Contact Us;
Could this code all be in the same page.tpl.php?
Thank you so much!
Drupal 7 has a title field for each content type. Why don't you use that?
It shows by default and you can change it for every node.
It is this code in your template file.
<?php print render($title_prefix); ?>
<?php if ($title): ?>
<h1 class="title" id="page-title">
<?php print $title; ?>
</h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
$title_prefix (array): An array containing additional output populated by modules, intended to be displayed in front of the main title tag that appears in the template.
$title: The page title, for use in the actual HTML content.
$title_suffix (array): An array containing additional output populated by modules, intended to be displayed after the main title tag that appears in the template.
See https://api.drupal.org/api/drupal/modules!system!page.tpl.php/7 for all the variables available in your page.tpl template file.
If you want to style one part different from the other you could use an extra field for it.
Or if you need to do something with the value (I wouldn't suggest this!!!).
switch ($title) {
case "About Us":
// Do something with the title
break;
case "Contact Us":
// Do something with the title
break;
case 2:
// Do something with the title
break;
}
Here's a simple approach:
Add a new field for the content type
Call it title2
Render the two fields
Use css to inline align them and add styling
Here was my solution since I am unable to position a Drupal-based title block right next to a non-Drupal based logo DIV..
I created a div called .headertitle and positioned/styled it how I want.
My header.php is an PHP include file which is called by each page and the headertitle has a PHP condition..
<?php if(empty($headertitle)) $headertitle = "ASK REAL QUESTIONS, <br><span class='white'>GET FREE ANSWERS.</span>";
echo $headertitle; ?>
That way, if no text is declared for the .headertitle, it takes the default form of "ASK REAL QUESTIONS, GET FREE ANSWERS."
...Now, I had to figure out a way to actually declare text for the .headertitle depending on what page I was on...
I did this by using a custom template for each page (node) by using this file.. "page--node--nodeid.tpl.php".
So, for my ABOUT US page, I created a tpl.php file called "Page--node--7.tpl.php".. for this page, the 7 is the nodeid which is easy to find.
Then, I added the following PHP code to declare text for the .headertitle specifically on the ABOUT US page which looks like this...
<?php $headertitle = "ABOUT<span class='white'>.US</span>"; include('includes/header.php'); ?>
DONE AND DONE.
I'll do that for each page. :)

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 post/page content in code

i am trying to insert a post/page into one of my themes files and it wont display shortcodes or php
i have created a page called home in the wordpress admin-paned and inserted into my code the following:
<div id="home_page"> <!-- echos the content from the page "home" id:43 -->
<?php $home_id = 43;
$home_page = get_page( $home_id );
?>
<?php echo $home_page->post_content; ?>
</div> <!-- end #home_page -->
and non of the shortcodes that i have in the page work.
i installed a php in post or page and tried useing php and it doesnt work.
when i insert
echo do_shortcode('[youtube_sc url=http://www.youtube.com/watch?v=Db3XGpt6nNU]');
directly into the code it works.
does anyone know y this happens?
thank you.
I got an answer in wordpress.stackexchange.com
I Quote:
You need to apply the filter the_content e.g.:
<?php echo apply_filters('the_content',$home_page->post_content); ?>
Also, you don't need custom shortcodes for youtube, just put the URL in the content (but not a hyperlink), and it'll be swapped out for a youtube player at runtime. No plugins or extra code needed thanks to oembed.
Thank You Tom J Nowell
The $home_page->post_content value is the exact post content as stored in the database. Echoing this does not give any of your shortcodes a chance to run.
You should use a "WordPress Loop" to display the content, as this sets things up to allow you to use template tags such as the_title() and the_content() - this will call the processing functions for shortcodes and other functions like wpautop() that "massage" post content for output.
If you don't want to use a Loop, you could output the content using
echo do_shortcode($home_page->post_content);
This will run the post content through the shortcode processor, giving the shortcodes a chance to run.
For more on how WordPress "massages" post content, you can look here: http://codex.wordpress.org/How_WordPress_Processes_Post_Content

Categories